2012-05-31 3 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

Brilliant! 정말 고맙습니다 –