简体   繁体   English

如何在ggplot2中将百分比四舍五入到小数点后两位

[英]How to round percentage to 2 decimal places in ggplot2

My code produces this graph:我的代码生成此图:

在此处输入图片说明

Code:代码:

ggplot(areas, aes(x = RETAILER, y = Difference), label=sprintf("%0.2f", round(areas$Difference, digits = 2))) +
geom_bar(stat = "identity", aes(fill = RETAILER), colour = "black") +
scale_y_continuous("Percentage", labels = percent_format()) +
geom_text(aes(label = paste(Difference * 100, "%"),
              vjust = ifelse(Difference >= 0, -1.5, 1.5))) +
theme_classic()

I have found code to allow the data labels to be positioned correctly above or below each bar, but I cannot seem to get the values to round to 2 decimal places.我找到了允许将数据标签正确定位在每个条形上方或下方的代码,但我似乎无法将值四舍五入到小数点后两位。

I have tried round(areas, 2) and sprintf("%0.2f") among other things, but nothing I try seems to work and I'm sure that I'm missing something simple.我已经尝试过round(areas, 2)sprintf("%0.2f")等等,但我尝试的任何东西似乎都不起作用,而且我确信我错过了一些简单的东西。

Where am I going wrong?我哪里错了?

UPDATE:更新:

With Roland's "encouragement", I have now managed to almost resolve this;在罗兰的“鼓励”下,我现在几乎解决了这个问题; the problem now is that I have now lost the percentage signs:现在的问题是我现在丢失了百分比符号:

在此处输入图片说明

How do I get them back?我如何让他们回来?

Updated code:更新代码:

ggplot(areas, aes(x = RETAILER, y = Difference)) +
  geom_bar(stat = "identity", aes(fill = RETAILER), colour = "black") +
  scale_y_continuous("Percentage", labels = percent_format()) +
  geom_text(aes(label = sprintf("%0.2f", areas$Difference * 100, "%"),
                vjust = ifelse(Difference >= 0, -1.5, 1.5))) +
  theme_classic()

Recent versions of ggplot2/scales packages offer arguments to the scales::percent function, for instance to round percentages to integers: ggplot2/scales包的最新版本为scales::percent函数提供了参数,例如将百分比四舍五入为整数:

scale_y_continuous(labels = scales::percent_format(accuracy = 1L))

This is roughly equivalent to round(x, 0L) : Note the 1 instead of 0 .这大致相当于round(x, 0L) :注意1而不是0

Or for two digits:或两位数:

scale_y_continuous(labels = scales::percent_format(accuracy = 3L))

There is a near-duplicate question.有一个几乎重复的问题。 Check Henrik's answer here: How to prevent scales::percent from adding decimal在此处检查 Henrik 的答案: 如何防止 scales::percent 增加小数

geom_text(label=percent(Difference,.11))

aes(label=paste(round(Difference*100,digits= 2),"%",sep=""))

I use the percent function from formattable within geom_text .我用的是percent函数从formattablegeom_text So in your case I would do like so:所以在你的情况下,我会这样做:

geom_text(aes(label = formattable::percent(Difference)),
          vjust = ifelse(Difference >= 0, -1.5, 1.5))

you can define digits within percent , but the default is 2, which is what you're looking for.您可以在percent内定义digits ,但默认值为 2,这正是您要查找的。 The % symbols remains, like you desire. % 符号保留,如您所愿。 Plus, you can do stuff with it as it is still numeric underneath and not character like other solutions out there.另外,你可以用它做一些事情,因为它下面仍然是numeric ,而不是像其他解决方案那样的character

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM