如何在rCharts中向hPlot添加自定义工具提示

时间:2014-05-01 06:33:03

标签: javascript r rcharts

我可以使用以下代码制作出色的hPlot:

h1<-hPlot(x = "MedianAge", 
      y = "AnnualPopulationGrowth", 
      data = df, 
      type = "bubble", 
      size="AdolescentFertilityRate", group = "continent")
h1$title(text='Median Age vs Annual Population Growth in 2010')
h1

但是,我的data.frame df中有一个字段,名为&#34; Name&#34;,我要显示。我尝试添加以下行:

h1<-hPlot(x = "MedianAge", 
  y = "AnnualPopulationGrowth", 
  data = df, 
  type = "bubble", 
  size="AdolescentFertilityRate", group = "continent")
h1$title(text='Median Age vs Annual Population Growth in 2010')
h1

然而,这不起作用。这一行的变化如下:

h1$tooltip(formatter("#! function(d) {return d.Name} !#"))

也不行。任何有助于在工具提示中弹出国家名称的帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个最小的例子:

library(rCharts)
# data
df <- data.frame(x = 1:10, y = rnorm(10), s = rnorm(10), z = letters[1:10])

# create plot object
p <- hPlot(y ~ x, data = df, size = "s", type = "bubble")

# fix data format
p$params$series[[1]]$data <- toJSONArray(df, json = F)

# add tooltip formatter
p$tooltip(formatter = "#! function() {return(this.point.z);} !#")

# show
p