2012-05-31 86 views
3

此代碼獲得在GGPLOT2條形圖

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)) 

產生如下圖分組條的中點值: enter image description here

我怎樣才能獲得的酒吧中點以放置標籤呢?

回答

6

我不知道,如果你的意思是水平或垂直的中點,但也許下面的例子將幫助:

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)) 

enter image description here

的關鍵是position_dodge

+0

太棒了!非常感謝 –