Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggvis density plot with tooltip?

Tags:

r

ggvis

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!

like image 238
Ignacio Avatar asked Aug 30 '26 02:08

Ignacio


1 Answers

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..

like image 58
Jaap Avatar answered Aug 31 '26 19:08

Jaap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!