如何在 ggplot2 中为 geom_text 中的条形图舍入数据标签?
How to round off data labels for a bar plot in geom_text in ggplot2?
我正在制作一系列条形图,其中百分比值位于每个条形图上方。我想将其四舍五入到小数点后 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()
有没有办法将它们四舍五入到最接近的整数,这样条形图就没有小数点了?
只需在 scales::percent
函数中添加 accuracy = 1
,例如
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
。有关详细信息,请访问 this.
你的 y 轴是计数,但你的条形标签是百分比。这就是为什么它们不匹配。
我正在制作一系列条形图,其中百分比值位于每个条形图上方。我想将其四舍五入到小数点后 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()
有没有办法将它们四舍五入到最接近的整数,这样条形图就没有小数点了?
只需在 scales::percent
函数中添加 accuracy = 1
,例如
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
。有关详细信息,请访问 this.
你的 y 轴是计数,但你的条形标签是百分比。这就是为什么它们不匹配。