Plot: Add legend that overlay several Frames

131 views Asked by At

How can we write a legend that overlay several frames?

Using the xpd parameter, the legend can exit its frame but still cannot enter the next frame.

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

One dummy solution would be to add the same legend on each frame so that they perfectly complement each other. But that would be a real pain.

2

There are 2 answers

0
Robert On BEST ANSWER

Using mtext is not so good but maybe!

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)
0
Marc in the box On

I often use layout for these kind of situations:

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