Is there a simple way to put a legend outside the plot area for a simple plot? I found... (at http://www.harding.edu/fmccown/R/) # Expand right side of clipping rect to make room for the legend *par(xpd=T, mar=par()$mar+c(0,0,0,4))* # Graph autos (transposing the matrix) using heat colors, # put 10% of the space between each bar, and make labels # smaller with horizontal y-axis labels barplot(*t*(autos_data), main="Autos", ylab="Total", col=*heat.colors(3), space=0.1, cex.axis=0.8, las=1, names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8*) # Place the legend at (6,30) using heat colors legend(*6, 30,* names(*autos_data*), cex=*0.8*, fill=*heat.colors(3)*); But I do not understand *par(xpd=T, mar=par()$mar+c(0,0,0,4))* Anyway it does not work for me when I change...> y1 <- runif(100, max=1, min=0) > y2 <- rnorm(100) > ylim <- c(min(y1, y2), max(y1,y2)) > plot(x, y1, pch=1, ylim=ylim) > points(x, y2, pch=2) > legend("topleft", legend=c("Uniform", "Normal"), pch=c(1,2))To> par(xpd=T, mar=par()$mar+c(0,0,0,4)) > plot(x, y1, pch=1, ylim=ylim) > points(x, y2, pch=2) > legend(6, 30, legend=c("Uniform", "Normal"), pch=c(1,2))I do not get to see the legend at all. I am confused.... cheers Worik [[alternative HTML version deleted]]
Hi, On Aug 29, 2010, at 8:00 PM, Worik R wrote:> Is there a simple way to put a legend outside the plot area for a > simple > plot? > > I found... (at http://www.harding.edu/fmccown/R/) > > # Expand right side of clipping rect to make room for the legend > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > # Graph autos (transposing the matrix) using heat colors, > # put 10% of the space between each bar, and make labels > # smaller with horizontal y-axis labels > barplot(*t*(autos_data), main="Autos", ylab="Total", > col=*heat.colors(3), space=0.1, cex.axis=0.8, las=1, > names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8*) > > # Place the legend at (6,30) using heat colors > legend(*6, 30,* names(*autos_data*), cex=*0.8*, > fill=*heat.colors(3)*); > > But I do not understand > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > Anyway it does not work for me when I change... > > >> y1 <- runif(100, max=1, min=0) >> y2 <- rnorm(100) >> ylim <- c(min(y1, y2), max(y1,y2)) >> plot(x, y1, pch=1, ylim=ylim) >> points(x, y2, pch=2) >> legend("topleft", legend=c("Uniform", "Normal"), pch=c(1,2)) > > To > >> par(xpd=T, mar=par()$mar+c(0,0,0,4)) >> plot(x, y1, pch=1, ylim=ylim) >> points(x, y2, pch=2) >> legend(6, 30, legend=c("Uniform", "Normal"), pch=c(1,2)) > > I do not get to see the legend at all. >I think you want to set xpd to NA (rather than TRUE as in your example) according to the docs for par(). "A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region." I have a hind-sight wish that it had been defined originally with "plot", "fig" and "dev" instead of FALSE, TRUE and NA because I am forgetful and I *always* have to look it up. Using the mar argument is a way to manage the margin size around the plot region within the device region. It is handy when you want to plan for extra annotations along the sides of the figure. In your example the new value of par is set to its original except on the right hand side where it picks up 4 extra lines. mar = par()$mar + c(0,0,0,4) Cheers, Ben [[alternative HTML version deleted]]
On Sun, Aug 29, 2010 at 20:00, Worik R <worikr@gmail.com> wrote:> Is there a simple way to put a legend outside the plot area for a simple > plot? > > I found... (at http://www.harding.edu/fmccown/R/) > > # Expand right side of clipping rect to make room for the legend > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > # Graph autos (transposing the matrix) using heat colors, > # put 10% of the space between each bar, and make labels > # smaller with horizontal y-axis labels > barplot(*t*(autos_data), main="Autos", ylab="Total", > col=*heat.colors(3), space=0.1, cex.axis=0.8, las=1, > names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8*) > > # Place the legend at (6,30) using heat colors > legend(*6, 30,* names(*autos_data*), cex=*0.8*, fill=*heat.colors(3)*); > > But I do not understand > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > Anyway it does not work for me when I change... > > > > y1 <- runif(100, max=1, min=0) > > y2 <- rnorm(100) > > ylim <- c(min(y1, y2), max(y1,y2)) > > plot(x, y1, pch=1, ylim=ylim) > > points(x, y2, pch=2) > > legend("topleft", legend=c("Uniform", "Normal"), pch=c(1,2)) > > To > > > par(xpd=T, mar=par()$mar+c(0,0,0,4)) > > plot(x, y1, pch=1, ylim=ylim) > > points(x, y2, pch=2) > > legend(6, 30, legend=c("Uniform", "Normal"), pch=c(1,2)) >It looks like (6,30) is way outside of the plot area, which is why you don't see the legend. I like to use the values of par()$usr (the xlim and ylim of the current plot) to set the legend position. This way it is always relative to the plotting area. Try this: par(mar=c(5.1, 4.1, 4.1, 6.1) ) plot(1:10) legend( par()$usr[2], mean(par()$usr[3:4]), legend=c("Uniform", "Normal"), pch=c(1,2), xpd=TRUE, xjust=0, yjust=0.5) And play around with xjust and yjust to get the legend where you want it. Hope that helps> > I do not get to see the legend at all. > > I am confused.... > > cheers > Worik > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Try using 'layout' to create the area for the legend; that way you don't have to worry about how much space to leave. Here is an example where I was putting the legend on the bottom of the chart: pdf('plot.pdf', width=62, height=40) layout(rbind(1, 2), height=c(7, 1)) # put legend on bottom 1/8th of the chart ....plotting commands...... # setup for no margins on the legend par(mar=c(0, 0, 0, 0)) plot.new() legend('center', legend=sprintf("%s [%s]", commOrder$label, commMost[as.character(commOrder$comm)]), col=colToUse, lty=1, lwd=20, ncol=8, cex=1.5) dev.off() On Sun, Aug 29, 2010 at 8:00 PM, Worik R <worikr at gmail.com> wrote:> Is there ?a simple way to put a legend outside the plot area for a simple > plot? > > I found... (at http://www.harding.edu/fmccown/R/) > > # Expand right side of clipping rect to make room for the legend > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > # Graph autos (transposing the matrix) using heat colors, > # put 10% of the space between each bar, and make labels > # smaller with horizontal y-axis labels > barplot(*t*(autos_data), main="Autos", ylab="Total", > ? col=*heat.colors(3), space=0.1, cex.axis=0.8, las=1, > ? names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8*) > > # Place the legend at (6,30) using heat colors > legend(*6, 30,* names(*autos_data*), cex=*0.8*, fill=*heat.colors(3)*); > > But I do not understand > *par(xpd=T, mar=par()$mar+c(0,0,0,4))* > > Anyway it does not work for me when I change... > > >> y1 <- runif(100, max=1, min=0) >> y2 <- rnorm(100) >> ylim <- c(min(y1, y2), max(y1,y2)) >> plot(x, y1, pch=1, ylim=ylim) >> points(x, y2, pch=2) >> legend("topleft", legend=c("Uniform", "Normal"), pch=c(1,2)) > > To > >> par(xpd=T, mar=par()$mar+c(0,0,0,4)) >> plot(x, y1, pch=1, ylim=ylim) >> points(x, y2, pch=2) >> legend(6, 30, legend=c("Uniform", "Normal"), pch=c(1,2)) > > I do not get to see the legend at all. > > I am confused.... > > cheers > Worik > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?