label above points with rChart

血红的双手。 提交于 2019-12-10 19:56:52

问题


I have a graph which i create with nPlot, i have two variable for X and Y axis and i want to add a third variable which i could see when I point my dots on the graph. For example, if a have X = age, Y = tall and Z = name, i want to be able to see changes of the tall depending on the age with the the name of the different person above dots.

Here is an example :

library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
df <- data.frame(age, tall, name)
plot <- nPlot(x = "age", y = "tall", data = df, type = "scatterChart")
plot$xAxis(axisLabel = "the age")
plot$yAxis(axisLabel = "the tall")
plot  

回答1:


You can use the tooltipContent option:

library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
df <- data.frame(age = age, tall = tall, name = name)
n1 <- nPlot(age ~ tall ,data = df, type = "scatterChart")
n1$xAxis(axisLabel = "the age")
n1$yAxis(axisLabel = "the tall", width = 50)
n1$chart(tooltipContent = "#! function(key, x, y, e ){ 
  var d = e.series.values[e.pointIndex];
  return 'x: ' + x + '  y: ' + y + ' name: ' + d.name
} !#")
n1

e.series is the particular series the mouse is hovering, e.pointIndex is the index on the values of the series. So d = e.series.values[e.pointIndex] will give the data point for that series which is being hovered over. d.name will then give the name attribute.




回答2:


My version of R (3.0.2) cannot download the rChart package, but I can answer your question with base coding.

> age <- c(1:20)
> tall <- seq(0.5, 1.90, length = 20)
> name <- paste(letters[1:20], 1:20, sep = "")
> df <- data.frame(age, tall, name)
> plot(x = df$age, y = df$tall, xlab = "the age", ylab = "the tall")  
> text(df$age, df$tall, labels = df$name, adj = c(1,-0.3))  #Applies text based on x, y inputs (e.g. age and tall)



来源:https://stackoverflow.com/questions/24304167/label-above-points-with-rchart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!