简体   繁体   中英

ggvis density plot with tooltip?

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!

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 (ie: 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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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