# Re: [R] legend for several graphics

From: Georg Otto <georg.otto_at_tuebingen.mpg.de>
Date: Fri, 07 Mar 2008 11:00:51 +0100

Thanks a lot, John, Gavin; Hadley and Greg, for your helpful comments and suggestions. I finally achieved what I wanted using the suggested method from Gavin with corrections from Greg.

Out of curiosity (and interest to learn): Hadley, how would you simplify that code using lattice or ggplot and how would you automatically draw the legend?

Best,

Georg

"Greg Snow" <Greg.Snow_at_imail.org> writes:

>
> My modification of your example is:
>
> library(TeachingDemos)
> 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)
> tmp1 <- cnvrt.coords( 0.5, 0, input='plt' )\$tdev # save location for
> mtext
> 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,
>
> at=tmp1\$x)
> ## draw legend
> par(xpd=NA)
> tmp2 <- cnvrt.coords( tmp1\$x, 0.05, input='tdev' )\$usr # get location
> for legend
> legend(tmp2\$x, tmp2\$y, legend = c("Type 1", "Type 2"),
> pch = 1:2, col = 1:2, ncol = 2, xjust=0.5, yjust=0.5)
> par(op)
>

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. Received on Fri 07 Mar 2008 - 10:05:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Mar 2008 - 10:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.