用 rChart 标记上面的点 [英] label above points with rChart
问题描述
我有一个用 nPlot 创建的图形,我有两个用于 X 轴和 Y 轴的变量,我想添加第三个变量,当我在图形上点点时可以看到该变量.例如,如果 a 有 X = 年龄,Y = 身高和 Z = 姓名,我希望能够通过点上方不同人的姓名看到身高随年龄的变化.
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.
这是一个例子:
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
推荐答案
您可以使用 tooltipContent
选项:
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
是鼠标悬停的特定系列,e.pointIndex
是系列值的索引.所以 d = e.series.values[e.pointIndex]
将给出被悬停的那个系列的数据点.d.name
然后会给出 name
属性.
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.
这篇关于用 rChart 标记上面的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!