【发布时间】:2015-08-19 10:11:29
【问题描述】:
我有一个包含 4 列的数据框。我使用两列 (x,y) 来绘制 x 和 y。第三列(组)用于分组。而且我的第四列(猫)没有使用,但我希望它显示在工具提示中。
这是我的数据框。
library(rCharts)
df <- data.frame(x=c(1:12,1:12),
y=c(0.6, 0.5, 0.3, 0.3, 0.8, 0.99, 0.6, 0.5, 0.4, 0.7, 0.6, 0.8,
0.4, 0.5, 0.7, 0.7, 0.2, 0.01, 0.4, 0.5, 0.6, 0.3, 0.4, 0.2),
group=c(rep("group1",12),rep("group2",12)))
df$cat <- c(rep(c(rep("A",6),rep("B",6)),2))
我使用rCharts中的hPlot函数来创建highcharts条形图。
p <- hPlot(x = "x", y = "y", data = df, type = c("column"),group="group")
p$addParams(dom = "plot1")
p$tooltip(borderWidth=0,
headerFormat="<span style='font-size: 10px'><b>{point.key}</b></span><br/>",
followPointer=TRUE,
followTouchMove=TRUE,
shared = FALSE)
Here,它解释了将额外的数据添加到系列中。似乎直截了当,但我无法让它工作。我已经使用来自jsonlite 的toJSON 和来自RJSONIO 的toJSONArray 尝试了各种事情。但是,它拒绝工作。这是问题的第一部分。
第二部分是使用格式化程序在数据进入后实际显示工具提示。 highcharts reference 建议:
tooltip: {
formatter: function () {
return 'The value for <b>' + this.x +
'</b> is <b>' + this.y + '</b>';
}
}
如何在 R 中做到这一点?
#idea1. maybe like this?
p$tooltip(formatter=function () {
return 'The value for <b>' + this.x +
'</b> is <b>' + this.y + '</b>';
})
#idea2. or like this?
p$tooltip(formatter="function () {
return 'The value for <b>' + this.x +
'</b> is <b>' + this.y + '</b>';
}")
#idea3. or perhaps like this?
p$tooltip(formatter=function () {
"return 'The value for <b>' + this.x +
'</b> is <b>' + this.y + '</b>';"
})
无论如何,它们都不起作用。所以,如果有人有任何见解,我想知道。非常感谢。
PS:someone else before 发布了同样的问题,但没有完整的答案。
【问题讨论】:
标签: r highcharts rcharts