r - 如何在ggplot2中为geom_text中的条形图四舍五入数据标签?
问题描述
我正在制作一系列条形图,其中百分比值位于每个条形上方。我想将它四舍五入到小数点后 0 位,但它是小数点后 3 位。这是我正在使用的示例代码
g1 <- ggplot(mydata, aes(x=PF.Score))+
geom_bar(color="Blue", fill="skyblue")+
geom_text(stat="count", aes(label=scales::percent(..prop..), group=1), size =3, vjust=-0.3)+
ylab("Count of Suppliers")+
xlab("D&B Score of Suppliers")+
ggtitle("D&B Score distribution of suppliers")+
theme_classic()
有没有办法将这些四舍五入到最接近的整数,以便条形图没有小数点?
解决方案
只需accuracy = 1
在scales::percent
函数内添加
ggplot(iris, aes(x=Sepal.Width))+
geom_bar(color="Blue", fill="skyblue")+
geom_text(stat="count", aes(label=scales::percent(..prop.., accuracy = 1), group=1), size =3, vjust=-0.3)+
ylab("Count of Suppliers")+
xlab("D&B Score of Suppliers")+
ggtitle("D&B Score distribution of suppliers")+
theme_classic()
要拥有 1 位或 2 位小数,您可以使用accuracy = 0.1
或accuracy = 0.01
。有关详细信息,请访问此。
推荐阅读
- javascript - Firebase 存储:this.xhr_.upload.addEventListener 不是函数
- node.js - 如何使用 Node.js 和 express 设置 Google Domains 动态 DNS?
- r - ClusterFuture 与雪块
- javascript - Puppeteer 无法从 Crontab 执行,但在从终端手动执行时工作
- java - 我可以对亚马逊 IOT 按钮进行编程以执行打开的应用程序的击键吗?
- c++ - 使用 lambda 函数修改类
- python - 从html中提取一些文本
- ruby-on-rails - 为什么在类的类名和文件名不匹配的情况下Ruby不会抛出错误?
- python - 为最小交换交换列表元素时出现 Python 超时错误 2
- c# - 在 Unity ReorderableList 上重新排序嵌套属性