如何防止小数位数::百分比加小数点

时间:2018-10-30 20:18:32

标签: r ggplot2 axis-labels

这开始于几天前,scales::percent将在其标签中添加一个小数位,而我似乎无法禁用该小数位以在y轴上显示整数值。 enter image description here

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 个答案:

答案 0 :(得分:7)

也许不是您问题的直接答案,但我在类似的设置中使用了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


我认为percent的行为已在scales 1.0.0中更改。请参见NEWS和代码here中的更新。

答案 1 :(得分:4)

只需更新,scales::label_percent(accuracy = 1L)将四舍五入为整数,scales::label_percent(accuracy = 0.1L)将四舍五入至小数点后一位,依此类推。