0

I have a small issue about GGVIS in R studio.

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.

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.

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")
user3577165
  • 85
  • 1
  • 8
  • You forgot to use the `key` argument in `ggvis` to define your "ID" variable (see the last example in the `add_tooltip` documentation). – aosmith Nov 02 '16 at 15:08

1 Answers1

0
    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

Ethan
  • 241
  • 2
  • 8