下面的代码不能正常工作, 当指针悬停在一个点上时, 它应该显示Num ......
iris$Sepal.Length.j <- jitter(iris$Sepal.Length)
iris$Sepal.Width.j <- jitter(iris$Sepal.Width)
iris$Num <- 1:nrow(iris)
iris %>% group_by(Species) %>%
ggvis(~Sepal.Length.j, ~Sepal.Width.j, opacity:=0.5) %>%
layer_points(fill=~Species) %>% layer_smooths(stroke=~Species) %>%
add_tooltip(html=function(x) {
if (!is.list(x)) return()
x$Num}, on="hover")
这是一个可能的错误还是我被误解了? 下面的代码工作正常。
iris %>% group_by(Species) %>%
ggvis(~Sepal.Length.j, ~Sepal.Width.j, opacity:=0.5) %>%
layer_points(fill=~Species) %>% layer_smooths(stroke=~Species) %>%
add_tooltip(html=function(x) {
if (!is.list(x)) return()
x$Sepal.Width}, on="hover")
答案 0 :(得分:1)
这里有几件事情要发生。首先是关于 ggvis 使用的数据列。
从add_tooltip
帮助页面的“示例”部分:
从客户端发送到服务器的数据仅包含数据列 在情节中使用的。如果您想获取其他数据列, 你应该使用一个键来排列一行中的项目 在数据中。
在您的第二个图中,Sepal.Width
用于绘图,因此可用于工具提示。但是您从未在绘图代码中使用Num
,因此无法在工具提示中使用它。要将Num
保留在图表的数据列中,您需要指明这是您的key
列。
这是一个简单的解决方法 - 只需将key := ~Num
添加到layer_points
。
分组和工具提示还存在一些其他微妙问题,其中一些问题在this answer中列出。在您的情况下,如果您在layer_points
之后和layer_smooth
之前进行分组,我认为工具提示可以按照您的意愿运作。
所以你的代码看起来像:
iris %>%
ggvis(~Sepal.Length.j, ~Sepal.Width.j, opacity:=0.5) %>%
layer_points(fill=~Species, key := ~Num) %>%
group_by(Species) %>%
layer_smooths(stroke=~Species) %>%
add_tooltip(html=function(x) {
if (!is.list(x)) return()
x$Num}, on="hover")
答案 1 :(得分:0)
没有名为&#34; Num&#34;在你的情节的输入数据集中 - 这就是为什么它不起作用。你的第二个例子是有效的,因为&#34; Sepal.Width&#34;是虹膜数据中的一列。