[R] Re: Universal legend in plot
ligges at statistik.uni-dortmund.de
Fri Jan 17 14:14:08 CET 2003
Vumani Dlamini wrote:
> Dear R-users:
> I asked a question on how I can have a universal legend in a plot and
> received the following result. I tried using "layout" but I can't seem
> to work on the "empty" plot (where I have to have the legend). I tried
> "oma" but I couldn't improve the quality of the plot, and that I didn't
> know how to specify all the line types using the keyboard, infact it is
> line type 4 (I had _._ and it didn't look nice).
> I am trying to create a graph with 6 panels, but would like to have a
> universal legend as each panel merely denotes a separate stratum. The
> legend has to be at the bottom.
> I use "par(mfrow=c(2,3))" to get the panels, but am not sure how to put
> the legend below the whole graph.
> Thanking you as always
> J.R. Lockwood
> I don't think you can do this without
> which you can use to create a separate graphical area within the plot
> region, where you can put the legend.
> i could be wrong though
> Peter Dalgaard
> You need to look at mtext(....,outer=TRUE), plus par(oma=....) to make
> room for the text in the outer margins.
> Try layout() for finer adjustments (and some drawbacks).
> See ?layout how to set up a 7th figure (legend) of full with below the
> desired six other figures.
> Uwe Ligges
So let's start with an example for layout():
layoutmat <- matrix(c(1,2,3,4,5,6,7,7,7), 3, byrow=TRUE)
fig <- layout(layoutmat, heights = c(1, 1, 0.3))
layout.show(fig) # Ah! That's the setup!
for(i in 1:6) plot(1:10) # some nonsense plots for example
opar <- par(mar=c(0,0,0,0)) # don't need margins for the legend
# set up the a plot without plotting to prepare for legend():
plot(0, axes=FALSE, type="n", xlim=c(-1, 1), ylim=c(-1, 1))
# now we can center the legend with:
legend(0, 0, c("one", "two"), lwd=1, col=c("black", "red"),
xjust = 0.5, yjust = 0.5)
par(opar) # restore old par settings
Peter's suggestion was to use mtext() instead of legend(), which is
somewhat simpler. Example:
par(mfrow = c(2,3), oma = c(4,0,0,0))
for(i in 1:6) plot(1:10)
mtext("Blah, blah 1", 1, outer = TRUE)
mtext("Blah, blah 2", 1, outer = TRUE, line = 2)
Please read the help pages what the different functions (and arguments)
More information about the R-help