我将多个图表连接成一个图形(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)
如果有人知道如何将图例添加到左侧而不是每个图或所有图中,那将是很好的;请注意,如果没有legend命令,上面的代码将生成下图:
答案 0 :(得分:1)
对于基本图形,我建议使用layout
代替par(mfrow=c(4,6))
。这样你就可以留下额外的空间来放置图例,使用plot.new()
移动到最后的面板区域并将图例放在那里。
答案 1 :(得分: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"))