【问题标题】:In R/plotly, how to change the tooltip number format?在 R/plotly 中,如何更改工具提示数字格式?
【发布时间】:2022-01-06 00:27:17
【问题描述】:

在 R/plotly 中,如何更改工具提示格式? 如下图,工具提示 'Sales' 显示 '0.230',如何将其转换为 '23.0%' 。谢谢!

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

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    您可以尝试使用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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 2015-09-01
      • 2021-12-30
      • 2020-04-22
      • 2021-04-16
      • 2018-12-07
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多