How to prevent scales::percent from adding decimal

丶灬走出姿态 提交于 2019-12-22 05:40:38

问题


This started happening a few days ago, that scales::percent would add a decimal place in its labels, and I can't seem to disable this decimal to display integer values on y-axis.

library(dplyr)
library(ggplot2)

mtcars %>% 
  count(cyl) %>% 
  mutate(prop = n / sum(n)) %>% 
  ggplot(aes(x = cyl, y = prop)) + 
  geom_point() + 
  scale_y_continuous(labels = scales::percent)

回答1:


Perhaps not a direct answer to your question, but I have used scales::percent_format and its accuracy argument ("Number to round to") in similar settings.

mtcars %>% 
    count(cyl) %>% 
    mutate(prop = n / sum(n)) %>% 
    ggplot(aes(x = cyl, y = prop)) + 
    geom_point() + 
    scale_y_continuous(labels = scales::percent_format(accuracy = 5L))


I think the behaviour of percent was changed in scales 1.0.0. See NEWS and updates in code here.



来源:https://stackoverflow.com/questions/53072282/how-to-prevent-scalespercent-from-adding-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!