R 工具提示数据点

7
我有一个与 R Shiny 相关的问题。 我想要一个工具提示,当我把鼠标放在数据点上时,可以显示具体信息。 有没有人有做法呢?
欢迎提供示例代码。

1
你尝试过什么?看看rCharts http://ramnathv.github.io/rCharts/。第一个例子似乎是你要找的。 - Jake Burkhead
1
所有支持rCharts工具提示的js库。您可能还想看一下SVGAnnotation,它允许在网格图形中使用工具提示。 - Ramnath
2个回答

6
我看到过Ramnath V在他的NYTimes图形示例中使用rCharts实现此功能,rCharts建立在Shiny之上。 您可以在这里查看完全可复制且明确描述的示例。 以下是您想要的代码片段:
require(rCharts)
p1 <- rPlot(SOG ~ yearID, data = team_data, type = 'point', 
  size = list(const = 2), color = list(const = '#888'), 
  tooltip="function(item){return item.SOG +'\n' + item.name + '\n' + item.yearID}"
)
p1$print('chart1')

请注意他如何使用Javascript函数作为rPlot的tooltip参数。

另一种选择

您还可以尝试将元素包装在tags$div()中。

虽然不完全符合您的要求,在这个相关问题中,Joe Cheng建议使用类似的方法来处理UI.R。(区别在于该示例中的工具提示是静态文本。)

假设您有一个sliderInput

tags$div(title="this static text will show up in the tooltip",
    sliderInput(  # parameters here
    )
)

希望能帮你进一步前行。

我们应该在 rCharts 0.4.2 中使用 "#!function(item){return ...}!#" - Stéphane Laurent

0

现在你也可以使用 ggvis 包来完成这个操作。请参见 http://ggvis.rstudio.com/

以下是你在 server.R 中需要使用的代码:

library(ggvis)
df %>% ggvis(~x, ~y) %>% layer_points() %>% 
    add_tooltip(function(x) paste0(names(x), ": ", 
                format(x), collapse = "<br />"), "hover") %>%
    bind_shiny("plot_id")

然后在ui.R中,要放置图表,您可以使用:

ggvisOutput("plot_id")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接