3
votes

I'm trying to add a tooltip to my ggvis plot. I basically want to add the density number when I hover the mouse over the density plot. This is what I have right now:

mtcars %>% ggvis(~wt, fill := "red") %>% 
  layer_densities() %>%  
  add_axis("x", title = "Weight") %>% 
  scale_numeric("x", domain = c(0, 5), nice = FALSE, clamp = TRUE) %>% 
  add_tooltip(function(df){density(df$wt)})

But when I hover, I get this error:

Error in density.default(df$wt) : argument 'x' must be numeric

Thanks!

1

1 Answers

2
votes

I only have a partial answer, but it might help you in the right direction.

The following code will give you a tooltip:

mtcars %>% ggvis(~wt, fill:="red") %>%
  layer_densities() %>%  
  add_axis("x", title = "Weight") %>% 
  add_tooltip(function(data){data$resp_}, "hover")

However, the tooltip contains the same value (i.e.: the first value of the density curve) for each x-value.

With dens <- mtcars %>% compute_density(~wt) you will get a density dataframe.

Hopefully someone else want to improve on this answer to get a complete solution..