如何防止scales :: percent添加小数点

26

这个问题在几天前开始出现,scales::percent会将小数点加入其标签中,我似乎无法禁用这个小数点来显示y轴上的整数值。 输入图像描述

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)
2个回答

27
也许不是直接回答你的问题,但我在类似的场景中使用了scales::percent_format和它的accuracy参数(“四舍五入的数字”)。{{scales::percent_format}} {{accuracy}}
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))

enter image description here


我认为在scales 1.0.0中,percent的行为已经发生了改变。请参见NEWS和代码here中的更新。


18

更新一下,scales::label_percent(accuracy = 1L)将四舍五入到整数,scales::label_percent(accuracy = 0.1L)将四舍五入到小数点后一位,以此类推。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接