Reputation: 7928
This code works as expected:
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
mtc %>% ggvis(x = ~wt, y = ~mpg, key := ~id) %>%
layer_points() %>%
add_tooltip(all_values, "hover")
but when I add layer_smooths(stroke := "red", se = T)
the code give me an error:
mtc %>% ggvis(x = ~wt, y = ~mpg, key := ~id) %>%
layer_points() %>%
layer_smooths(stroke := "red", se = T) %>%
add_tooltip(all_values, "hover")
Error in eval(expr, envir, enclos) : object 'id' not found
Why? how can I fix it?
Thanks!
Upvotes: 2
Views: 210
Reputation: 762
If I hadn't recognized this as an example from one of the ggvis help pages, I wouldn't have known where mtc came from. The problem seems to be that you set the key property in the ggvis() statement, but layer_smooths() evidently doesn't support it, so you need to move it into layer_points(). I got the visualization to run with the following code:
library(ggvis)
mtc <- mtcars
mtc$id <- seq_len(nrow(mtc))
all_values <- function(x)
{
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
mtc %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_smooths(stroke := "red", se = T) %>%
layer_points(key := ~id) %>%
add_tooltip(all_values, "hover")
However, when you hover over the smooth or the confidence bands, all of the values associated with the variables are labeled 'character(0)' in the tooltip.
Upvotes: 3