在 R 函数 ggplotly 中(来自 plotly 包),如何调整标签内容

Posted

技术标签:

【中文标题】在 R 函数 ggplotly 中(来自 plotly 包),如何调整标签内容【英文标题】:In R function ggplotly ( from plotly package), how to adjust the label content 【发布时间】:2021-12-03 14:55:43 【问题描述】:

当我使用功能ggplotly(来自plotly包)时,如何更改弹出标签内容(删除附图中红色矩形中的标签内容)?

library(tidyverse)
library(plotly)
mdate <- c(44197,44198,44199,44200,44201,44202,44203,44204,44205,44206,44207,44208,44209,44210,44211,44212,44213,44214,44215,44216,44217,44218,44219,44220,44197,44198,44199,44200,44201,44202,44203,44204,44205,44206,44207,44208,44209,44210,44211,44212,44213,44214,44215,44216,44217,44218,44219,44220)
myear <- c(2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020)
amount <- c(45,2,2,87,93,32,31,42,84,93,24,13,12,48,39,33,88,78,15,6,51,55,32,93,14,16,11,84,47,87,40,45,57,76,24,91,11,96,44,82,99,17,75,59,22,89,61,93)

p <- data.frame(mdate,myear,amount) %>% 
  mutate(mdata=as.Date(mdate),
         myear=as.factor(myear)) %>% 
  ggplot(aes(x=as.Date(mdate),y=amount,color=myear))+geom_line()+
  theme_minimal()

ggplotly(p)

【问题讨论】:

是否要编辑默认工具提示? @AnilGoyal 对,这就是我想要的。谢谢! 【参考方案1】:

我们可以将工具提示的规范(即要使用的值)传递给 plotly:

p <- data.frame(mdate,myear,amount) %>% 
  mutate(mdata=as.Date(mdate, origin ="1904-01-01"),
         myear=as.factor(myear)) %>% 
  ggplot(aes(x=mdata, y=amount, color=myear)) +
  geom_line()+
  theme_minimal() 

ggplotly(p, tooltip = c("y", "colour"))

附:虽然 ggplot2 确实允许拼写“颜色”和“颜色”,但情节似乎坚持“颜色”......

【讨论】:

感谢您的帮助,太好了! 我遇到了一个额外的问题:有没有办法改变 ggplotly 工具提示的填充颜色和板颜色?你能帮忙吗,谢谢!

以上是关于在 R 函数 ggplotly 中(来自 plotly 包),如何调整标签内容的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化:使用plotly包的subplot函数将多个ggplot2可视化结果组合在一起形成组合plotly图(Multiple ggplot2 plots with plotly)

R语言数据热力图绘制实战(基于原生R函数ggplot2包plotly包)

r 带有R的GGplot地图来自http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot

R语言ggplot2可视化:使用R原生plot函数为指定曲线下面的区域着色ggplot2可视化在曲线的特定下方添加分割线ggplot2为指定曲线下面的区域着色

R语言可视化R原生plot函数色彩半透明色彩全透明不透明色彩处理ggplot2颜色透明示例

R语言使用ggplot2包使用geom_density()函数绘制基础密度图实战(density plot)