ggplotly 禁用格式化并将文本函数放在 aes() 中的 geom_line 中删除该行但保持悬停在标签上

问题描述

我学习 r 的原因有很多,但作为我旅程的一部分,我决定使用一些 HHS COVID 数据。我使用此代码创建了 6 个 geom_line 图(3 个实际数据的 7 天平均值,以及该数据的 3 个预测)。

tests_cases_percent <- ggplot() +
  geom_line(data = us_cases,aes(x=date,y=tests_7day/10,color ="Tests (div. by 10)"),size = 1) +
  geom_line(data = us_cases,y=newcases_7day,color = "New cases"),y=per100_7day/coef,color = "% positive"),y=fore_tests/10),color = "green",size = 0.75,lty=3) +
  geom_line(data = us_cases,y=fore_cases),color = "orange",y=fore_per100/coef),color = "magenta",lty=3) +
  scale_color_manual(values = c(
    "% positive" = "magenta","New cases" = "orange","Tests (div. by 10)" = "green")) +
  labs(color = "7-day averages:") +
  scale_x_date(breaks = "2 months",name = NULL) +
  scale_y_continuous(name = NULL,labels = comma,minor_breaks = seq(0,250000,10000),breaks = seq(0,50000),limits = c(0,250000),sec.axis = sec_axis(~.*coef,labels = percent)) +
  ggtitle("COVID-19 Tests,New Cases and Percent Testing Positive in the US") +
  theme_modern_rc() + theme(legend.position = "bottom",panel.grid.major = element_line(color = "darkgray"))


tests_cases_percent

其他人可能不同意,但我喜欢这个结果

Output plot of COVID data

我想将鼠标悬停在框上以使其具有交互性,但是当我使用 ggplotly(tests_cases_percent) 时,我丢失了我设置的大部分格式

Plot minus formatting because of ggplotly

如果我尝试向 geom_line 中的 aes() 添加文本,该行将完全消失

对于下一个情节,第一个 geom_lin 更改为这个

geom_line(data = us_cases,color ="Tests (div. by 10)",text = paste("Tests","\n",date,": ",round(total_outcomes_7day,1),sep = "")),size = 1)

线完全消失,但如果你悬停点仍然在那里,文本框工作。

Plot with line that has disappeared

知道我做错了什么吗?我想不出解决方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)