Home > Software design >  In R function ggplotly ( from plotly package), how to adjust the label content
In R function ggplotly ( from plotly package), how to adjust the label content

Time:10-16

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)?

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)

enter image description here

CodePudding user response:

We can pass specifications for the tooltip (i.e. 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"))

enter image description here

P.S. While ggplot2 does allow both spellings "color" and "colour" plotly seems to insist on "colour"...

  • Related