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

标签 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)

最佳答案

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

关于r - 如何防止小数位数::百分比加小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072282/

相关文章:

r - ggplotly 和 facet_wrap 的 X 轴错误

r - 轴标签中的数学表达式

python - cartopy set_xlabel set_ylabel(不是刻度标签)

r - 在 ggplot2 图表中实现谷歌字体

r - ivot_wider 合并同一行上的信息

ggplot2 - 使用 ggarrange 添加行和列标题

r - 密谋:add_trace循环

image - 图像上的刻度线是否有可能以像素的“边缘”为中心?

sql - knitr sql block 没有将数据保存到变量中

随机生成 3 种不同的颜色