[R] legend for several graphics

Georg Otto georg.otto at tuebingen.mpg.de
Fri Mar 7 11:00:51 CET 2008


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)
>



More information about the R-help mailing list