2012-09-30 4 views
2

ggplo2을 사용하여 likert scale 변수를 분석하고 싶습니다. 이런 종류의 그래픽을 원합니다 (아래). 그러나 스택 막대에 레이블을 추가하는 방법과 각 그룹 변수 및 패싯 변수 (facet_wrap)에 대해 서로 다른 개수와 수단을 삽입하는 방법을 모르겠습니다.facet_wrap을 사용하여 스택 막대 그림에 개수 및 레이블 추가

어떤 도움을 주셔서 감사합니다. 내가 무엇을 얻을

library(ggplot2) 
library(scales) 
library(RColorBrewer) 

ggplot(example,aes(GroupungVar,fill=VarOfInterest)) + geom_bar(position='fill') +  
scale_fill_manual(values = (brewer.pal(5, "Greens"))) + 
facet_wrap(~FacetVar,ncol=1) + coord_flip() + 
scale_y_continuous(labels=percent) + ylab('Percent') 

...

enter image description here

.. 그리고 내가 무엇을 달성하고자하는 (번호 에일 :

데이터 here

내 코드에서 얻을 수 있습니다 데이터 세트와 같지 않음). 각 그룹의 레이블에 개수 (N), 막대에있는 백분율 레이블 및 오른쪽에있는 평균 값 (각 그룹에 대해)을 갖고 싶습니다. 백분율 및 평균값은 플롯의 모든 막대에 대해 사용해야하며, 처음 몇 개에 추가합니다. 단지 의미를 나타냅니다. 나는 아마 오히려 그래프 자체보다, 축 레이블에 넣어 것입니다 샘플 크기

enter image description here

+0

무엇의 평균? 'VarOfInterest'는 숫자가 아닌 값을 가진 요소입니다. – Maiasaura

+0

Mean of VarOfInterest, 값은 1 - 5이며 레이블은 범례에만 사용됩니다. – Maciej

답변

4

:

library(plyr) 
example <- ddply(example,.(FacetVar,GroupungVar), 
      transform, 
      GroupingVar = paste(as.character(GroupungVar)," - (n=",length(GroupungVar),")",sep = "")) 

ggplot(example,aes(GroupingVar,fill=VarOfInterest)) + 
    geom_bar(position='fill') +  
    scale_fill_manual(values = (brewer.pal(5, "Greens"))) + 
    facet_wrap(~FacetVar,ncol=1) + 
    coord_flip() + 
    scale_y_continuous(labels=percent) + 
     ylab('Percent') 

enter image description here

+1

플롯에 라벨을 추가하는 방법을 알고 계십니까? 나는 그걸로 정말로 큰 문제가있다 .... – Maciej

6

나는 R과 함께 밤을 보내고 ggplot2와 나는 내가 원하는 것을 얻는다 :

library('ggplot2') 
library('plyr') 
library('RColorBrewer') 
library(scales) 


label_positions<- function(x) { 
    n<-length(x) 
    wynik<-numeric(n) 
    for (i in 1:n){ 
    if (i==1) { 
     wynik[i]<-0+x[i]/2 
    } 
    else { 
     wynik[i]<-x[i]-(x[i]-x[i-1])/2 
    } 
    } 
    return(wynik) 
} 

exam1<-ddply(example,.(GroupingVar,FacetVar,VarOfInterest), 'nrow') 
exam1.1<-ddply(example,.(GroupingVar,FacetVar),summarise, sr=mean(as.numeric(VarOfInterest),na.rm=T), 
       odch=sd(as.numeric(VarOfInterest,na.rm=T))) 

exam1<-merge(exam1,exam1.1,by.x=c('GroupingVar','FacetVar'),by.y=c('GroupingVar','FacetVar')) 

names(exam1)[4]<-'Count' 

exam2<-mutate(exam1,cumul=ave(Count,list(GroupingVar,FacetVar),FUN=cumsum), 
       N=ave(cumul, list(GroupingVar,FacetVar),FUN=max), 
       CumSumPercent=cumul/N*100, 
       Freq=Count/N*100) 


exam2<-mutate(exam2,cfrq = ave(CumSumPercent, list(GroupingVar,FacetVar), FUN = label_positions)) 
exam2$XLabel<-paste(exam2$GroupingVar,' (N=',exam2$N,')',sep='') 
exam2$PosMean<-105 

p<-ggplot(exam2, aes(x = Etykieta, y = Freq, fill = VarOfInterest)) + 
    geom_bar(stat = 'identity',colour="black") + 
    labs (x = "", y = "Percentage", fill=" ") + 
    scale_fill_brewer(name="Rating", palette="Greens", breaks = rev(levels(exam2$VarOfInterest))) + 
    geom_text(aes(y = cfrq, label=paste(sprintf("%.01f",Freq), "%", sep='')), size=5) + 
    geom_text(aes(y=PosMean,label=paste(sprintf("%.02f",sr),' (',sprintf("%.02f",odch),')',sep='')),size=5)+ 
         facet_wrap(~FacetVar,ncol=1) + 
         coord_flip() + ylab('Procent odpowiedzi') + 
    guides(fill=guide_legend(title=NULL)) + theme_bw() + 
    theme(legend.position="bottom",strip.text.x=element_text(size=15,face='bold'), 
     axis.text.x =element_text(size=12,face='bold'), axis.text.y =element_text(size=12,face='bold'), 
     axis.title.x=element_text(size=15,face='bold'), axis.title.y=element_text(size=15,face='bold'), 
     strip.background=element_rect(colour='black')) 

plot(p) 

그리고 결과

enter image description here

+0

이렇게 많은 일을 해줘서 고마워! 이것은 정말로 나를 도왔다! – zazu

관련 문제