Hello, I have used layout() to produce to 2 plots on a page, leaving a plotting space above them. I would like "Legend", which will actually be a real legend, to be centered above the two graphs. Right now I am only able to position "Legend" above the second graph that I create... obviously I am stuck in its margin space. Is there a way to draw the legend in the empty plotting space above? Or is there a way to center the legend in the margin space above the two graphs. I am open to using par(mfrow=c(2,2)) rather than layout(), but it currently produces the same result. Thanks, John layout(matrix(c(0,0,1,2),2,2, byrow=TRUE), widths=c(1,1), heights=c(0.5,2)) layout.show(2) par(mar=c(3,3,5,1.5)) plot(1:10,1:10) par(mar=c(3,1.5,5,1.5)) plot(1:20,1:20) par(xpd=TRUE) legend(0, 23, c("Legend")) [[alternative HTML version deleted]]
On 05/03/2010 05:15 PM, John Poulsen wrote:> Hello, > > I have used layout() to produce to 2 plots on a page, leaving a plotting space above them. I would like "Legend", which will actually be a real legend, to be centered above the two graphs. Right now I am only able to position "Legend" above the second graph that I create... obviously I am stuck in its margin space. > > Is there a way to draw the legend in the empty plotting space above? Or is there a way to center the legend in the margin space above the two graphs. > > I am open to using par(mfrow=c(2,2)) rather than layout(), but it currently produces the same result. >Hi John, Try using par(xpd=NA) that allows you to draw anywhere on the device, and then place your legend relative to the current plotting area. See the help page for zoomInPlot (plotrix) for an example. Jim
Another solution I've used is to set up an additional layout space and put the legend in there with no graph. You print a blank dummy graph and then add the legend to the "blank" layout panel like so: if (floatLegend) { # We want to float the legend independently # so we have to add it here as the only visible component of a # dummy graph. legText <- yourLegendNames # create a blank graph -- automatically scales -1 to +1 on both axes op <- par(mar=plotMargins) tsFake <- barplot(0,0, axes=FALSE) legend(x=1, y=0, legend=legText, # set fill, angle, density to match your real graph scheme xjust=1, yjust=0.5) par(op) } Hope that helps. -- Patrick Lenon Database Engineer Frontier Science and Technology Foundation (608)441-2947