简体   繁体   English

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

[英]In R function ggplotly ( from plotly package), how to adjust the label content

When I use function ggplotly (from plotly package), how to change the pop label content ( remove the label content in the red rectangular in attached image )?当我使用函数 ggplotly (来自 plotly 包)时,如何更改 pop 标签内容(删除附加图像中红色矩形中的标签内容)?

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)

在此处输入图片说明

We can pass specifications for the tooltip (ie which values to use) to 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"))

在此处输入图片说明

PS While ggplot2 does allow both spellings "color" and "colour" plotly seems to insist on "colour"... PS虽然ggplot2确实允许拼写“颜色”和“颜色”,但情节似乎坚持“颜色”......

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

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