简体   繁体   English

带工具提示的ggvis密度图?

[英]ggvis density plot with tooltip?

I'm trying to add a tooltip to my ggvis plot. 我正在尝试为我的ggvis图添加tooltip 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. 但是,工具提示包含每个x值的相同值(即:密度曲线的第一个值)。

With dens <- mtcars %>% compute_density(~wt) you will get a density dataframe. 使用dens <- mtcars %>% compute_density(~wt)您将获得密度数据帧。

Hopefully someone else want to improve on this answer to get a complete solution.. 希望其他人想要改进这个答案,以获得完整的解决方案..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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