[R] Adding a legend to R graph device with several plots (no to individual plots!)

Monica Pisica pisicandru at hotmail.com
Fri Sep 5 19:05:58 CEST 2008


Hi Nelson,

I don't know if you got your answer or not but here it is my low-tech solution for what is worth. I like to do a layout as I want my graphs to be and add some space where I "plot" an invisible graph with dummy data and add the legend there. In this case I decided to put the legend on middle top of the layout. 

Because you used lapply to do your graphs it seems that I have to change the numbers in the p.names parameter because somehow it establishes the order of your graphs as well. I usually use the simple for loop because you never have loads of graphs in one page, so it is safe enough. 

I hope this helps a little.

Monica

##### code starts

l <- layout(matrix(c(rep(1, 2), seq(2,5)), 3,2, byrow = T), c(3,3,3), c(1,3,3,3))
layout.show(l)
par(mar = c(1,1,1,1))

plot(seq(1,10), seq(1,10), type = "n", axes = F)
smartlegend(x= "center", y="top", c("1995","2006"), lty=2:1)
par(mar = c(4,4,3,2))

p.names <- c("2","3","4","5")    ### change p.names definition
lapply(p.names, function(x) {
d1 <- density(rnorm(100,0,1))
d2 <- density(rnorm(100,.3,1.2))
plot(range(d1$x, d2$x), range(d1$y, d2$y), type = "n", xlab = "", ylab = "") 
## change xlab, ylab in plot
lines(d1, col = "black", lty = 2)
lines(d2, col = "black", lty = 1)
})
mtext("Learning R the hard way", side = 1, line=-1.5, outer=TRUE)

#### code ends

------------------------------
Message: 16
Date: Thu, 4 Sep 2008 09:35:42 -0400
From: "Nelson Villoria" 
Subject: [R] Adding a legend to R graph device with several plots (no
to individual plots!)
To: r-help at r-project.org
Message-ID:

Content-Type: text/plain; charset=ISO-8859-1

Dear Users, I already posted this question: it either went unnoticed,
or it is to basic (if this is so, please sent me a hint).
I would like to know if there is a way to add a common
legend to an arrangement of plots. In the example below, I get four
plots in my device. each one has a density for 1995 and one for 2006.
I have found that using legend or smartlegend I can add a legend to
each plot, but I am looking for something in the spirit of mtext. That
is, putting the legend anywhere I want on the device. In my situation
I have:

par(mfrow=c(2,2), ann = FALSE)
p.names <- c("1","2","3","4")
lapply(p.names, function(x) {
d1 <- density(rnorm(100,0,1))
d2 <- density(rnorm(100,.3,1.2))
plot(range(d1$x, d2$x), range(d1$y, d2$y), type = "n", xlab = NULL,
ylab = NULL)
lines(d1, col = "black", lty = 2)
lines(d2, col = "black", lty = 1)
})
mtext("Learning R the hard way", side = 1, line=-1.5, outer=TRUE)
smartlegend(x= "center", y="top", c("1995","2006"), lty=2:1)

the last line puts the legend on the lower-right plot. I'd like to put
it in the middle - or top of the device. Any suggestion?

Thanks!

Nelson




_________________________________________________________________


50F681DAD532637!5295.entry?ocid=TXT_TAGLM_WL_domore_092008


More information about the R-help mailing list