Dear R-users: I asked a question on how I can have a universal legend in a plot and received the following result. I tried using "layout" but I can't seem to work on the "empty" plot (where I have to have the legend). I tried "oma" but I couldn't improve the quality of the plot, and that I didn't know how to specify all the line types using the keyboard, infact it is line type 4 (I had _._ and it didn't look nice). # I am trying to create a graph with 6 panels, but would like to have a universal legend as each panel merely denotes a separate stratum. The legend has to be at the bottom. I use "par(mfrow=c(2,3))" to get the panels, but am not sure how to put the legend below the whole graph. Thanking you as always ############ J.R. Lockwood I don't think you can do this without layout() which you can use to create a separate graphical area within the plot region, where you can put the legend. i could be wrong though ############ Peter Dalgaard You need to look at mtext(....,outer=TRUE), plus par(oma=....) to make room for the text in the outer margins. ############ Try layout() for finer adjustments (and some drawbacks). See ?layout how to set up a 7th figure (legend) of full with below the desired six other figures. Uwe Ligges
"Vumani Dlamini" <dvumani at hotmail.com> writes:> Dear R-users: > > I asked a question on how I can have a universal legend in a plot and > received the following result. I tried using "layout" but I can't seem > to work on the "empty" plot (where I have to have the legend). I tried > "oma" but I couldn't improve the quality of the plot, and that I > didn't know how to specify all the line types using the keyboard, > infact it is line type 4 (I had _._ and it didn't look nice). > > # > I am trying to create a graph with 6 panels, but would like to have a > universal legend as each panel merely denotes a separate stratum. The > legend has to be at the bottom. > > I use "par(mfrow=c(2,3))" to get the panels, but am not sure how to > put the legend below the whole graph. > > Thanking you as alwaysAh, I think I misunderstood what you wanted there. You want to have a 2x3 layout with 5 plots and one empty, just for legend()? Could you not just do a blank plot (e.g. plot(0,0, type="n", axes=F, xlab="", ylab="")) and place the legend onto that? The lty settings are described in help(par). Beware that legend() can do you in rather badly if you mix numeric and character specifications. -p -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Vumani Dlamini wrote:> Dear R-users: > > I asked a question on how I can have a universal legend in a plot and > received the following result. I tried using "layout" but I can't seem > to work on the "empty" plot (where I have to have the legend). I tried > "oma" but I couldn't improve the quality of the plot, and that I didn't > know how to specify all the line types using the keyboard, infact it is > line type 4 (I had _._ and it didn't look nice). > > # > I am trying to create a graph with 6 panels, but would like to have a > universal legend as each panel merely denotes a separate stratum. The > legend has to be at the bottom. > > I use "par(mfrow=c(2,3))" to get the panels, but am not sure how to put > the legend below the whole graph. > > Thanking you as always > > ############ > J.R. Lockwood > > I don't think you can do this without > > layout() > > which you can use to create a separate graphical area within the plot > region, where you can put the legend. > > i could be wrong though > ############ > Peter Dalgaard > > You need to look at mtext(....,outer=TRUE), plus par(oma=....) to make > room for the text in the outer margins. > ############ > Try layout() for finer adjustments (and some drawbacks). > See ?layout how to set up a 7th figure (legend) of full with below the > desired six other figures. > > Uwe LiggesSo let's start with an example for layout(): layoutmat <- matrix(c(1,2,3,4,5,6,7,7,7), 3, byrow=TRUE) fig <- layout(layoutmat, heights = c(1, 1, 0.3)) layout.show(fig) # Ah! That's the setup! for(i in 1:6) plot(1:10) # some nonsense plots for example opar <- par(mar=c(0,0,0,0)) # don't need margins for the legend # set up the a plot without plotting to prepare for legend(): plot(0, axes=FALSE, type="n", xlim=c(-1, 1), ylim=c(-1, 1)) # now we can center the legend with: legend(0, 0, c("one", "two"), lwd=1, col=c("black", "red"), xjust = 0.5, yjust = 0.5) par(opar) # restore old par settings Peter's suggestion was to use mtext() instead of legend(), which is somewhat simpler. Example: par(mfrow = c(2,3), oma = c(4,0,0,0)) for(i in 1:6) plot(1:10) mtext("Blah, blah 1", 1, outer = TRUE) mtext("Blah, blah 2", 1, outer = TRUE, line = 2) Please read the help pages what the different functions (and arguments) are doing! Uwe Ligges