legend for several graphics
On Wed, 2008-03-05 at 15:28 +0100, Georg Otto wrote:
Hi, I am trying to generate a figure of 9 plots that are contained in one device by using par(mfrow = c(3,3,)) I would like to have 1 common legend for all 9 plots somewhere outside of the plotting area (as opposed to one legend inside each of the 9 plots, which the function legend() seems to generate by default). Any hint how to do this?
Here's one way:
op <- par(mfrow = c(3,3), ## split region
oma = c(5,0,4,0) + 0.1, ## create outer margin
mar = c(5,4,2,2) + 0.1) ## shrink some margins
plot(1:10, main = "a", pch = 1:2, col= 1:2)
plot(1:10, main = "b", pch = 1:2, col= 1:2)
plot(1:10, main = "c", pch = 1:2, col= 1:2)
plot(1:10, main = "d", pch = 1:2, col= 1:2)
plot(1:10, main = "e", pch = 1:2, col= 1:2)
plot(1:10, main = "f", pch = 1:2, col= 1:2)
plot(1:10, main = "g", pch = 1:2, col= 1:2)
plot(1:10, main = "h", pch = 1:2, col= 1:2)
plot(1:10, main = "i", pch = 1:2, col= 1:2)
## title
mtext("My Plots", side = 3, outer = TRUE, font = 2, line = 1, cex = 1.2)
## draw legend
legend(-12.5, -6, legend = c("Type 1", "Type 2"), pch = 1:2, col = 1:2,
ncol = 2)
par(op)
I had to fiddle by hand with the legend x and y locations to get it
roughly centred. There has to be better way - probably something to do
with reseting the plot region, but I can't recall how to do that now. If
there is, I'm sure someone will tell me what I overlooked.
Is this what you were looking for?
G
Best, Georg
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%