2016-12-09 1 views
0

나는 다음과 같은 데이터가 있습니다채우기 위치에 따라 geom_bar 라벨을 추가

set.seed(1) 
df <- data.frame(type=c("A","B","D","A","C","D","B","C","D","E"), 
       count=as.integer(runif(10,100,200)), 
       sample=c(1,1,1,2,2,2,3,3,3,3),stringsAsFactors=F) 
df$type <- factor(df$type,levels=unique(sort(df$type))) 

내가 geom_bar를 사용하여 플롯 :

require(ggplot2) 
ggplot(df,aes(x=sample,y=count,fill=type))+geom_bar(stat="identity",position="dodge")+theme(legend.position="none") 

enter image description here

내 질문이 방법으로 df$type를 추가됩니다 각 막대 위에 레이블을 붙이시겠습니까?

추가 :

분명히
geom_text(aes(label=type,hjust=0),position=("dodge")) 

+0

가능한 중복을 [위치 \의 _dodge의 폭 인수가 무엇입니까 ?] (http://stackoverflow.com/questions/34889766/what-is-the-width-argument-in-position-dodge) – cuttlefish44

답변

1

작동하지 않습니다이 작동합니다 :의

ggplot(df,aes(x=sample,y=count,fill=type))+ 
    geom_bar(stat="identity",position="dodge")+ 
    geom_text(aes(label=type,hjust=0, vjust=-0.1),position=position_dodge(width = 1))+ 
    theme(legend.position="none") 

enter image description here

+0

감사합니다. 나는 hjust로 놀았으나 바를 중심으로 레이블을 붙일 수는 없다. (hjust = 0.5는 가깝지만 충분하지 않다.) 어떤 생각? – dan

+0

@dan 너는 너의 원한 위치에 원본이 있기 위하여 너비와 hjust을 둘 다 바꿀 수있다. –

+0

레이블을 가운데에 맞추려면'width = 0.9'를 사용하고'hjust = 0'을 제거하십시오 –

관련 문제