2013-06-07 7 views
0

여러 그림을 하나의 그림으로 연결하여 (24 개 특정) 그림 전체에 범례를 추가하는 방법을 알 수 없습니다. 여러 그림에 범례 추가 R

par(mfrow = c(4,6)) 

for(i in 1:24){ 
x <- rep(0,3) 
y <- rnorm(3, 3) 
par(family = "Garamond") 
col_vec <- c("darkblue", "gray65", "maroon4") 
plot(x,y, xaxt = 'n', xlab = '', ylab = '', xaxt='n', bty = "n", ylim = c((min(y) - 1.5),(max(y) + 1.5)), col = col_vec, pch = 19, cex =.8) 
abline(h=y[2], lty=2, col = "gray89")} 

title("Effect Size", outer = TRUE, line = -2, cex = 2) 
legend("topleft", c("Treatment 1", "Control", "Treatment 2"), col = col_vec, pch = 15) 

사람은 좌측 측면보다는 각 플롯 또는 플롯의 모든, 즉 좋은 것에 범례를 추가하는 방법을 알고있는 경우

; 전설의 명령없이 점에 유의, 위의 코드를 다음 그림이 생성

enter image description here

+0

초기 레이아웃에서 공간을 만들 수 있고,'plot.new()'를 사용하여 새로운 그리기 영역을 설정하고 거기에'legend()'를 추가 할 수 있습니다. – baptiste

+0

라고 말하면 격자 또는 ggplot2는 전체 프로세스를 간단하게 만듭니다 – baptiste

답변

0

여기 ggplot2 한 제안의를

library(plyr) 
xy <- rdply(24, data.frame(x=0, y=rnorm(3,3), id=1:3)) 

library(ggplot2) 

ggplot(xy, aes(x, y, colour=factor(id))) + 
    facet_wrap(~.n, scales="free", ncol=4) + 
    geom_point() + 
    annotate("segment", x=-0.1, xend=-0.1, y=-Inf, yend=+Inf) + 
    scale_x_continuous(breaks=NULL, expand=c(0,0), lim=c(-0.1, 0.1)) + 
    scale_y_continuous(expand=c(0,0.1)) + 
    theme_minimal() + 
    theme(strip.text.x = element_blank(), 
     axis.title.x = element_blank(), 
     axis.title.y = element_blank()) + 
    scale_colour_manual("", labels=c("Treatment 1", "Control", "Treatment 2"), 
         values=c("darkblue", "gray65", "maroon4")) 

enter image description here

+0

우수, 많은 감사합니다! –

1

기본 그래픽을 위해 내가 대신 layout를 사용하는 것이 좋습니다 것입니다 par(mfrow=c(4,6))입니다. 그렇게하면 전설을 배치 할 공간이 추가로 생기므로 plot.new()을 사용하여 최종 패널 영역으로 이동하고 거기에 범례를 배치하십시오.