0

複数のプロットを 1 つの図 (具体的には 24) に連結していますが、図全体に凡例を追加する方法がわかりません。

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)

各プロットまたはすべてのプロットではなく、左側に凡例を追加する方法を誰かが知っていれば、それは素晴らしいことです。凡例コマンドがない場合、上記のコードは次の図を生成することに注意してください。

ここに画像の説明を入力

4

2 に答える 2

0

ggplot2 に関する 1 つの提案を次に示します。

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"))

ここに画像の説明を入力

于 2013-06-07T19:24:29.640 に答える