Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rCharts Highcharts将额外数据添加到工具提示中_R_Highcharts_Rcharts - Fatal编程技术网

rCharts Highcharts将额外数据添加到工具提示中

rCharts Highcharts将额外数据添加到工具提示中,r,highcharts,rcharts,R,Highcharts,Rcharts,我有一个4列的数据框。我使用两列(x,y)来绘制x和y。第三列(组)用于分组。我的第四列(cat)未使用,但我希望它显示在工具提示中 这是我的数据框 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,

我有一个4列的数据框。我使用两列(x,y)来绘制x和y。第三列(组)用于分组。我的第四列(cat)未使用,但我希望它显示在工具提示中

这是我的数据框

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))
,它解释了如何将额外的数据添加到序列中。看起来很直截了当,但我一直没能让它发挥作用。我从
jsonlite
RJSONIO
使用
toJSON
尝试了各种各样的东西。但是,它拒绝工作。这是问题的第一部分

第二部分是在数据输入后使用格式化程序实际显示工具提示。报告建议:

    tooltip: {
        formatter: function () {
            return 'The value for <b>' + this.x +
                '</b> is <b>' + this.y + '</b>';
        }
    }
工具提示:{
格式化程序:函数(){
返回“+this.x”的“值”+
'is'+this.y+';
}
}
在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>';"
})
#idea1。也许是这样?
p$工具提示(格式化程序=函数(){
返回“+this.x”的“值”+
'is'+this.y+';
})
#想法2。还是像这样?
p$工具提示(formatter=“function(){
返回“+this.x”的“值”+
'is'+this.y+';
}")
#想法3。或者像这样?
p$工具提示(格式化程序=函数(){
“返回“+this.x”的“值”+
'是'+this.y+''
})
不管怎样,它们都不起作用。所以,如果有人有任何见解,我想知道。非常感谢


PS:相同的问题由发布,但没有完整的答案。

,您好,您必须将格式化程序函数置于“#!…!#”之间,如下所示:

p$tooltip(formatter="#!function () {
  return 'The value for <b>' + this.x +
    '</b> is <b>' + this.y + '</b>';
}!#")
p$工具提示(格式化程序=“#!函数(){
返回“+this.x”的“值”+
'is'+this.y+';
}!#")

@Ramnath可能在某个地方回答了一个类似的问题。

因此,
hPlot()
不能用于在工具提示中显示其他变量。请参见下面的工作示例:

library(rCharts)

d <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
                y=c(0.7,0.8,0.7,0.3,0.5,0.2,0.4,0.4,0.5,0.5,
                    0.3,0.2,0.3,0.7,0.5,0.8,0.6,0.6,0.5,0.7),
                type=factor(c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")),
                pop=c("Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2",
                      "Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2"))

hp <- hPlot(x = "x", y = "y", data = d, type = c("column"), group = "type")
hp$plotOptions(column = list(stacking = "normal", pointPadding = 0, groupPadding = 0, borderWidth = 0))
hp$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
          formatter = "#! function(){return this.point.x + '<br>' + this.point.pop + '<br>' + this.point.type + ': ' + this.point.y;} !#")
hp

library(rCharts)

d <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
                y=c(0.7,0.8,0.7,0.3,0.5,0.2,0.4,0.4,0.5,0.5,
                    0.3,0.2,0.3,0.7,0.5,0.8,0.6,0.6,0.5,0.7),
                type=factor(c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")),
                pop=c("Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2",
                      "Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2"))

hp <- hPlot(x = "x", y = "y", data = d, type = c("column"), group = "type")
hp$plotOptions(column = list(stacking = "normal", pointPadding = 0, groupPadding = 0, borderWidth = 0))
hp$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
          formatter = "#! function(){return this.point.x + '<br>' + this.point.pop + '<br>' + this.point.type + ': ' + this.point.y;} !#")
hp
hm <- rCharts:::Highcharts$new()
dlev <- levels(d$type)
for(i in 1:length(dlev))
{
  hm$series(data = toJSONArray2(d[d$type==dlev[i],,drop=F], json = F,names=T), name = dlev[i],type = c("column"), marker = list(radius = 3))
}
hm$plotOptions(column = list(stacking = "normal", pointPadding = 0, groupPadding = 0, borderWidth = 0))
hm$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
          formatter = "#! function(){return this.point.x + '<br>' + this.point.pop + '<br>'+ this.point.type + ': ' + this.point.y;} !#")
hm