In R/plotly, how to change the tooltip number format?

In R/plotly, how to change the tooltip format? As below image, the tooltip 'Sales' show '0.230', how to transfer it to '23.0%' . Thanks!

library(ggplot2)
library(plotly)

mydata <- data.frame(
  category=LETTERS[1:5],
  sales=c(0.234,0.23,0.17,0.4,0.35)
)

p <- ggplot(mydata,aes(x=category,y=sales))+
  geom_point()

ggplotly(p,tooltip=c('y','x'))

enter image description here

1 answer

  • answered 2021-11-29 05:26 Park

    You may try using scales::percent

    library(scales)
    p <- mydata %>%
      mutate(sales = scales::percent(sales)) %>%
      ggplot(aes(x=category,y=sales))+
      geom_point()
    
    ggplotly(p,tooltip=c('y','x'))
    

    enter image description here

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum