Nelson Villoria
2008-Sep-04 13:35 UTC
[R] Adding a legend to R graph device with several plots (no to individual plots!)
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
Duncan Murdoch
2008-Sep-04 13:58 UTC
[R] Adding a legend to R graph device with several plots (no to individual plots!)
On 9/4/2008 9:35 AM, Nelson Villoria wrote:> 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?The likely reason you didn't get a reply is because you used smartlegend(), which I believe comes from gplots. This means only people who know gplots well will answer. To do this using legend(), try this: par(xpd=NA) legend(locator(), c("1995","2006"), lty=2:1) This will allow you to place the legend manually whereever you like on the page. If you want to code a particular position for re-use later, run locator() on its own, and save the location. (You will probably want to use grconvertX and grconvertY to change these into ndc coordinates. For example, this looks okay to me: par(xpd=NA) legend(grconvertX(0.44, "ndc", "user"), grconvertY(0.55, "ndc", "user"), c("1995","2006"), lty=2:1) You might want to wrap this in a nice "mlegend()" function to save some typing. Duncan Murdoch
Monica Pisica
2008-Sep-05 17:05 UTC
[R] Adding a legend to R graph device with several plots (no to individual plots!)
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