[英]Obtaining midpoint values of grouped bars in ggplot2 barchart
這段代碼
library(ggplot2)
test=read.table(text=
"group fillcd percent
1 1 73.2
1 2 73.8
1 3 78.6
2 1 78.1
2 2 95.6
2 3 95.5
", header=TRUE, sep="" )
test$fill <- factor(test$fillcd, labels=c("XX", "EE", "BB"))
test$text=paste(test$percent,"%")
ggplot(data=test,
aes(group, percent, fill=fill)) +
geom_bar(stat="identity",position="dodge")+
coord_flip()+
geom_text(data = test, aes(y = percent, x = group, label = text ))
生成以下圖表:
如何獲得條形的中點以在那里放置標簽?
我不確定你的意思是水平還是垂直中點,但是下面的例子可能會有所幫助:
ggplot(data=test,
aes(group, percent, fill=fill)) +
geom_bar(stat="identity",position=position_dodge(width = 0.9))+
coord_flip()+
geom_text(data = test, aes(y = percent/2, x = group, label = text ),
position = position_dodge(width = 0.9))
關鍵是position_dodge
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.