隐藏绘图提示颜色痕迹 [英] Hiding plotly tooltip color traces

查看:44
本文介绍了隐藏绘图提示颜色痕迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是否可以在已通过交互方式与图进行交互的ggplot图表上隐藏工具提示的颜色轨迹?在下面的示例中,我希望工具提示来自此:

Is there anyway to hide the tooltip color traces on a ggplot chart which has been made interactive w/ plotly? In the example below I would like my tooltip to go from this:

对此:

library(plotly)
library(tidyverse)

## fake data
dat <- data.frame(date = seq(as.Date("1910/1/1"), as.Date("1910/1/10"), "days"),
                  pred = 1:10,
                  pred2 = 31:40,
                  ci_low = seq(0, 9, 1),
                  ci_upper = seq(2, 11, 1))

## plot
p1 <- dat %>% 
  ggplot(aes(x = date, y = pred)) +
  geom_line(color = "red", aes(group = 1, text = paste("date:", date, "\npred:", pred, "\npred2:", pred2, "\nci_low:",  ci_low, "\nci_upper:", ci_upper))) +
  geom_line(aes(group = 1, x = date, y = pred2), color = "green") +
  geom_ribbon(aes(group = 1, x = date, ymin = ci_low, ymax = ci_upper), alpha = 0.2, linetype = 0)

## plotly-fy
ggplotly(p1, tooltip = c("text")) %>%
  layout(hovermode = "x unified") %>%
  style(hoverinfo = "skip", traces = 2)

推荐答案

希望有更好的方法,但这是使用隐藏跟踪的解决方法:

Hopefully there is a better way but here is a workaround using a hidden trace:

library(plotly)
# library(tidyverse)

## fake data
dat <- data.frame(date = seq(as.Date("1910/1/1"), as.Date("1910/1/10"), "days"),
                  pred = 1:10,
                  pred2 = 31:40,
                  ci_low = seq(0, 9, 1),
                  ci_upper = seq(2, 11, 1))

## plot
p1 <- dat %>% 
  ggplot(aes(x = date, y = pred)) +
  geom_point(color = NA, aes(group = 1, text = paste("date:", date, "\npred:", pred, "\npred2:", pred2, "\nci_low:",  ci_low, "\nci_upper:", ci_upper))) +
  geom_line(aes(group = 1, x = date, y = pred), color = "red") +
  geom_line(aes(group = 1, x = date, y = pred2), color = "green") +
  geom_ribbon(aes(group = 1, x = date, ymin = ci_low, ymax = ci_upper), alpha = 0.2, linetype = 0)

## plotly-fy
ggplotly(p1, tooltip = c("text")) %>%
  layout(hovermode = "x unified") %>%
  style(hoverinfo = "skip", traces = c(2:4))

这篇关于隐藏绘图提示颜色痕迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆