[英]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
function 之內添加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
。 有關詳細信息,請訪問此。
您的 y 軸是計數,但您的條形 label 是百分比。 這就是為什么他們不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.