我已經使用構建了一個條形圖ggplot2,我正在顯示每個條形圖上的值。我的問題是我無法顯示每個條形上尾隨值的零。
這是我的資料集:
data <- data.frame(product=c('product1',
'product2',
'product3',
'product4',
'product5',
'product6',
'product7',
'product8'),
test=c(0.60,
0.80,
0.50,
0.70,
0.40,
0.30,
0.20,
0.10))
該圖是使用以下代碼生成的:
ggplot(data, aes(product,test))
geom_col()
geom_bar(stat="identity", col = "grey", fill="black")
coord_flip()
labs(title="Product comparisons")
theme(text=element_text(size=20))
theme(axis.text.y=element_text(size=12))
ylim(NA, 0.90)
geom_text(aes(label=round(test, digits=2)), nudge_y= -0.05, color="white", size = 6)
結果:

只顯示第一個小數,但我需要在每個標簽后顯示零。我嘗試使用該label=round(test, digits=2)命令執行此操作,但這并沒有解決它。可以做些什么來解決這個問題?
uj5u.com熱心網友回復:
嘗試一下!
ggplot(data, aes(product,test))
geom_col()
geom_bar(stat="identity", col = "grey", fill="black")
coord_flip()
labs(title="Product comparisons")
theme(text=element_text(size=20))
theme(axis.text.y=element_text(size=12))
ylim(NA, 0.90)
geom_text(aes(label=format(round(test, digits = 2), nsmall = 2)), nudge_y= -0.06, color="white", size =3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358875.html
上一篇:在R中繪制資料幀串列
