2013-06-28 2 views
2

아마추어 R 사용자가 여기 있습니다. 이 질문에 답을 얻었는지 묻는 질문이 있지만 나는 좋은 대답을 찾지 못했습니다. 012 "10 평판"이 없기 때문에 이미지를 게시 할 수 없습니다.데이터 프레임의 요소로 스택 막대 플롯 정렬

섭취 경로의 기여도 (내림차순)에 따라 x 변수를 정렬하는 누적 막대 그림이 필요합니다.

Percent<-c(0.4,0.75,0.8, 0.3,0.1,0.6,0.25,0.5) 
Inh<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Inhalation"), Percent=Percent) 

Ing<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Ingestion"),  Percent=1-Percent) 

df<-data.frame(rbind(Inh,Ing)) 
ggplot(df,aes(x=ID,y=Percent,fill=Route))+ geom_bar(stat="identity")+ 
facet_wrap(~Age, scales = "free_x") + 
ylab("Percent Contribution") + 
labs(title = "Route Contribution to Exposure by Age Groups") 
enter image description here

그러나 나는 내가 수동으로 조롱하는 다음과 같이 할 :

Percent<-c(0.1,0.6,0.25, 0.3,0.4,0.75,0.8,0.5) 
Inh<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Inhalation"), Percent=Percent) 

Ing<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Ingestion"),  Percent=1-Percent) 

df<-data.frame(rbind(Inh,Ing)) 
ggplot(df,aes(x=ID,y=Percent,fill=Route))+ geom_bar(stat="identity")+ 
facet_wrap(~Age, scales = "free_x") + 
ylab("Percent Contribution") + 
labs(title = "Route Contribution to Exposure by Age Groups") 

enter image description here

사전에 감사합니다!

업데이트 : Roland 덕분에, 나는 음모가 있습니다! 질문은 명확하게 남아 있습니다. 관심이있는 사람들은 여기 코드와 최종 제품의 :이 데이터를 변경하지 않고 순서를 변경

ggplot(df,aes(x=id2,y=Percent,fill=Route, width=1,order = -as.numeric(Route)))+ 
geom_bar(stat="identity")+ 
facet_wrap(~Age, scales = "free_x") + 
xlab(" ")+ 
ylab("Percent Contribution") + 
theme(axis.text.x = element_blank(), axis.ticks.x= element_blank()) + 
labs(title = "DEHP Route Contribution to Exposure by Age Groups") 

enter image description here

+1

사진의 경우 링크로 넣을 수 있으며 더 높은 평판을 가진 사람이 추가 할 수 있습니다. – agstudy

+0

이미지 또는 이미지를 온라인에 게재하십시오. 즉, 입력 한 내용과 출력하려고하는 것의 모형. –

답변

1

(당신은 당신의 모형에서와 같이). 아이디어는 AgeID의 상호 작용을 제공하는 정렬 된 (Percent에 의해) 요소를 만들고이를 플롯에 사용하지만 ID 값과 일치하도록 축 레이블을 변경하는 것입니다.

enter image description here

df <- df[order(df$Route,df$Percent),] 
df$id2 <- factor(paste(df$ID,df$Age),levels=unique(paste(df$ID,df$Age)),ordered=TRUE) 

ggplot(df,aes(x=id2,y=Percent,fill=Route))+ 
    geom_bar(stat="identity")+ 
    scale_x_discrete(labels = setNames(regmatches(levels(df$id2),regexpr("[[:alnum:]]*",levels(df$id2))),levels(df$id2))) + 
    facet_wrap(~Age, scales = "free_x") + 
    xlab("ID") + 
    ylab("Percent Contribution") + 
    labs(title = "Route Contribution to Exposure by Age Groups") 

는 그러나, 나는 결과 플롯이 복잡하고 읽기 어려운 생각합니다.

+0

@ Roland- 고마워요! 솔루션이 효과적이었습니다. 네,이 플롯은 제시 될 때 약간의 설명이 필요하다고 생각합니다. 나는이 줄거리와 나란히 상자 그림을 가지고왔다 갔다왔다. 추가 제안 사항은 인정 될 것입니다. 다시 한번 감사드립니다. – LeslieKish

0

이해해야 할 기본적인 질문은 주문이 그래프의 속성인지 또는 데이터 자체의 속성인지 여부입니다. R은 플롯 대신 데이터 속성으로 향하기 때문에 플로팅 함수에는 데이터를 만들거나 편집 할 때 수행해야하는 부분을 재정렬하기위한 인수가 없습니다. reorder 함수는 미래의 그래프/분석에 사용할 요소의 수준을 재정렬하는 한 가지 방법입니다.

관련 문제