www
www

Reputation: 39174

How to round the hover information when using ggplotly?

How to round the hover information when using ggplotly? In the following example, I created a scatter plot with hover information. I would like to round the numbers in that hover information to integer without changing the actual values.

library(ggplot2)
library(plotly)

p <- ggplot(mtcars, aes(x = wt, y = qsec)) +
  geom_point()

ggplotly(p)

enter image description here

Upvotes: 1

Views: 1566

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84689

Use the text aesthetic to do a custom tooltip:

library(ggplot2)
library(plotly)

p <- ggplot(mtcars, 
            aes(x = wt, y = qsec, 
                text = paste0("wt: ", round(wt), "</br></br>qsec: ", round(qsec)))) +
  geom_point()

ggplotly(p, tooltip = "text")

Upvotes: 4

Related Questions