几年后,我有了答案。
即使使用简单的自定义格式化程序函数,这些包装函数hPlot()
似乎也不支持额外的工具提示变量。根据问题中的数据集,请参阅下面的工作示例。
require(rCharts)
# create data frame
df <- data.frame(x = c(1:5), y = c(5:1),
z = c("A", "B", "C", "D", "E"),
name = c("K", "L", "M", "N", "O"))
# Plot using hPlot() approach
h1 <- hPlot(x = "x", y = "y", data = df, type = "scatter", group = "z")
h1$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
formatter = "#! function(){return 'X: ' + this.point.x + '<br>Y: ' + this.point.y + '<br>Z: ' + this.point.z + '<br>Name: ' + this.point.name;} !#")
h1

工具提示在上面的示例中不起作用,因为数组中的变量没有命名。见str(h1)
。
# Plot using manual build
h1 <- rCharts:::Highcharts$new()
dlev <- levels(factor(as.character(df$z)))
for(i in 1:length(dlev))
{
h1$series(data = toJSONArray2(df[df$z==dlev[i],,drop=F], json = F,names=T), name = dlev[i],type = c("scatter"), marker = list(radius = 3))
}
h1$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
formatter = "#! function(){return 'X: ' + this.point.x + '<br>Y: ' + this.point.y + '<br>Z: ' + this.point.z + '<br>Name: ' + this.point.name;} !#")
h1

这是有效的,因为数组变量names=T
在开头的行中使用命名h1$series...
。见str(h1)
。
这种解决了工具提示问题,但命名数组可能还有其他问题。例如,它破坏了闪亮应用环境中的东西。hPlot()
不使用命名数组一定是有原因的。