ggvis density plot with tooltip?

荒凉一梦 提交于 2020-01-15 02:47:19

问题


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:


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



来源:https://stackoverflow.com/questions/24493278/ggvis-density-plot-with-tooltip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!