简体   繁体   English

ggvis中的工具提示(R和闪亮)

[英]tooltip in ggvis (R and shiny)

I have a small issue about GGVIS in R studio. 我在R studio中有一个关于GGVIS的小问题。

I want to plot something and have more information on each point when I move my cursor on it. 我想绘制一些东西,并在将光标移到每个点上时获得更多信息。 Thus, I am using GGVIS package and the add_tooltip() function to do it. 因此,我正在使用GGVIS包和add_tooltip()函数来执行此操作。

However when I run the code below, I obtain the plot but not the additional info when I move my cursor on the points. 但是,当我运行下面的代码时,将光标移到这些点上时会得到该图,但没有获得其他信息。

Furthermore, I want to use the separate function (tooltip_test) because my real code is a bit more complex and the function would help me a lot. 此外,我想使用单独的函数(tooltip_test),因为我的真实代码稍微复杂些,并且该函数会对我有很大帮助。

library(ggvis)    
test <- data.frame(ID=1:10, TIME=1:10, COUNTS=rep(1:2,5), EXTRA=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
    tooltip_test <- function(x) {
      if (is.null(x)) return(NULL)
      if(is.null(x$ID)) return(NULL)

      sub_test = test[test$ID == x$ID, ]
      paste0("Category: ", sub_test$EXTRA)
    }

    test %>%
      ggvis(x= ~TIME, y= ~COUNTS) %>%
      layer_points() %>%
      add_tooltip(tooltip_test, "hover")
    library(ggvis)    
    test <- data.frame(ID=1:10, TIME=1:10, COUNTS=rep(1:2,5), EXTRA=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
    tooltip_test <- function(x) {
      if (is.null(x)) return(NULL)
     paste0('Category: ', test$EXTRA[x$ID])
    }

test %>%
  ggvis(x= ~TIME, y= ~COUNTS, key := ~ID) %>%
  layer_points() %>%
  add_tooltip(tooltip_test, "hover")

this should be sufficient for you,u forgot to add ID as key in ggvis implementation 这对您应该足够了,您忘了在ggvis实现中添加ID作为键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM