2012-05-04 2 views
7

저는 X 축에 두 가지 다른 요인으로 분류 된 R-cran에서 박스 플롯을 만들기 위해 노력하고 있습니다. 내 문제는 단지 2 ~ 3 레벨을 가진 두 번째 요소에 레이블을 붙이는 범례를 사용하면서 전체 그래프를 적절하게 스팬하는 +20 레벨의 한 요소에 대한 레이블을 만드는 데 있습니다. 여기 다중 팩터 라벨이있는 R 박스 플롯

는 테스트 스크립트입니다 약을 흉내 내 실제 데이터 세트 : 어떤 도움

d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500)) 
# first factor has 20+ levels 
d$f1<-factor(d$f1) 
# second factor a,b,c 
d$f2<-factor(d$f2) 

boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE) 

# y axis is numeric and works fine 
yts=pretty(d$x,n=5) 
axis(2,yts) 

# I know this doesn't work; what I'd like is to spread the factors out 
# so the each group of three(a,b,c) is labeled correctly 
axis(1,at=seq(1:20)) 

# Use the legend to handle the f2 factor labels 
legend(1, max(d$x), c("a", "b","c"),fill = c("red", "blue","green")) 

감사

답변

13

FWIW하는 ggplot2 솔루션 : 당신의 모두

library(ggplot2) 
ggplot(data = d, aes(x = f1, y = x)) + 
    geom_boxplot(aes(fill = f2), width = 0.8) + theme_bw() 

enter image description here

+0

'ggplot'이라는 음모를 꾸밀 수 있다는 것을 결코 깨닫지 못했습니다! 나는 못생긴 회색 배경 때문에 항상 사용하지 않았다. 보여 주셔서 고마워요. – thelatemail

+0

@thelatemail, themeing을 사용하면 플롯의 거의 모든 요소를 ​​조정/수정할 수 있습니다. –

+0

@thelatemail - 주제에 대한 개요는 [here] (https://github.com/hadley/ggplot2/wiki/Themes)를 참조하십시오. – Chase

5

당신이 3 개 상자의 각 그룹의 중간에 레이블을 원하는 경우,이 같은 시도 :

axis(1,at=seq(2,60,3),labels=1:20,cex.axis=0.7) 

enter image description here

일반화하려면, 이은 다음과 같습니다

groups <- 20 
numbox <- 3 
total <- groups * numbox 
xpoints <- seq(median(1:numbox),total,numbox) 
+0

감사합니다, 내가 찾던 정확히. – Kerry

+0

@ 케리 - 걱정 마세요! 이 경우 upvote (위쪽 화살표)와 대답 (화살표 아래의 눈금 기호)을 모두 선택하는 것을 잊지 마십시오. – thelatemail