1

I have this piece of code. How can I please easy change hover text diff to newlabel ? Here is my site http://webcovid19.online/

 ggplotly(
        ggplot(my_data, aes(x=date, y=diff)) + 
        geom_bar(stat='identity',fill='red')  +
        scale_y_continuous(labels = comma) 
    )
Andrew
  • 309
  • 1
  • 2
  • 11
  • please check https://stackoverflow.com/questions/40598011/how-to-customize-hover-information-in-ggplotly-object/40598524 – mccandar Dec 07 '20 at 12:14
  • yes, I was checking this post already before, but from there is not very clear for me how to do it in my case. – Andrew Dec 07 '20 at 12:22

1 Answers1

2

You can customize the text in the tooltip adding text to your aes() in ggplot. Here, you can include both date and diff with whatever text label you want (or just include diff alone). Within ggplotly, you can include tooltip = "text" to refer to this text with hover.

library(plotly)
library(scales)

ggplotly(
  ggplot(my_data, aes(x=date, y=diff, text = paste("Date:", date, "\nnewlabel:", diff))) + 
    geom_bar(stat='identity', fill='red')  +
    scale_y_continuous(labels = comma),
  tooltip = "text"
)
Ben
  • 28,684
  • 5
  • 23
  • 45
  • muchas gracias Ben – Andrew Dec 07 '20 at 19:55
  • Hi Ben, Is it possible to change text size of hover text ? I found something here via hoverlabel , but not sure how to do it in my case. https://stackoverflow.com/questions/38076722/change-size-of-hover-text-in-plotly – Andrew Dec 09 '20 at 22:20
  • 1
    Hi Andrew - if you save your `ggplotly()` object as `gg`, then you can change the `style` such as: `style(gg, hoverlabel = list(font = list(size = 24)))`...it is tricky with all the lists involved in this, but it is more clearly described [here](https://plotly-r.com/controlling-tooltips.html). – Ben Dec 09 '20 at 22:35
  • Excellent ! I use this way ...`%>% style (hoverlabel = list(font=list(size=20)))` – Andrew Dec 09 '20 at 23:05