绘图:添加覆盖多个帧的图例

时间:2015-06-09 18:30:25

标签: r graphics graph plot

我们如何编写覆盖多个帧的图例?

使用xpd参数,图例可以退出其框架但仍无法进入下一帧。

par(mfrow=c(1,2))
plot(rnorm(120), rnorm(120))
legend(x = -0.1, y=0.1, legend = "Legend that covers both plots", text.col="red", cex=2, box.col="red", xpd=TRUE)
plot(rnorm(120), rnorm(120))

enter image description here

一个虚拟解决方案是在每个帧上添加相同的图例,以便它们彼此完美互补。但这将是一个真正的痛苦。

2 个答案:

答案 0 :(得分:1)

使用mtext并不是那么好,但也许!

op=par( mfrow = c( 1, 2 ), oma = c( 1.3, 0, 0, 0 ) )
plot(rnorm(120), rnorm(120))
plot(rnorm(120), rnorm(120))
mtext("Legend that covers both plots", side=1, cex=1.5, col="red",outer = T,
       xpd=TRUE)
mtext("Legend that covers both plots", line = -3,cex=1.5, col="red",outer = T)
par(op)

答案 1 :(得分:0)

我经常在这种情况下使用layout

op <- par(mar=c(3,3,1,1))
layout(matrix(c(3,1,3,2),2,2), widths=c(4,4), height=c(1.5,4))
layout.show(3)
plot(rnorm(120), rnorm(120))
plot(rnorm(120), rnorm(120))
par(mar=c(0,3,1,1))
plot(1, t="n", axes=FALSE, xlab="", ylab="")
legend("center", legend = "Legend that covers both plots", text.col="red", cex=2, box.col="red", xpd=TRUE)
par(op)

enter image description here