# [R] Legend in the outer margin

Greg Snow Greg.Snow at intermountainmail.org
Mon Apr 10 19:30:04 CEST 2006

```The cnvrt.coords function in the TeachingDemos package may be of help.
Here is an example of possible use (just change the .9 and .7 to where
ever on the page you want the legend):

par(mfrow=c(3,3), mar=c(4,4,0.9,0.5), oma=c(1,2,2,4),cex.main=1.1)

for (i in 1:9){
x <- runif(25,1,10)
y <- 3+ i*x + rnorm(25)
plot(x,y)
}

par(xpd=NA)

tmp <- cnvrt.coords(.9,.7, 'tdev')\$usr

legend(tmp,legend=c("2h-opt Exact","1-shift Exact","2p-opt Exact"),
lty=c("solid","dashed","dotdash"),lwd=c(2,2,2),col=c("red","green","blac
k"),
bty="n",cex=0.8)

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dieter Menne
> Sent: Monday, April 10, 2006 7:40 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Legend in the outer margin
>
> Uwe Ligges <ligges <at> statistik.uni-dortmund.de> writes:
>
> > You get the user coordinates of the plotting region by
> >    par("usr")
> > Now simply make the legend right of that plotting region,
> e.g. with x
> > corrdinates at
>     par("usr") + epsilon
> > and y coordinates at
> >    mean(par("usr")[3:4])
>
> I always found it ugly that this depends on the last plotted
> figure in an array, but I wanted to position my legend
> independent of it at an absolute position in device space.
> What's the best way to achieve this?
>
> par(mfrow=c(3,3), mar=c(4,4,0.9,0.5),
>    oma=c(5,2,5,10),cex.main=1.1)
>
> n=7 # Legend should be positioned independent of n for (i in 1:n){
>   plot(rnorm(20),ylim=c(-3,3))
> }
>
> # ... just the idea
> reset_to_01_coordinates()
> par(xpd=NA)
> leg = legend(0.9,0.5, # << Should be seen as absolute in 0/1
> coords c("2h-opt Exact","1-shift Exact","2p-opt Exact"),
> lty=c("solid","dashed","dotdash"),lwd=c(2,2,2),
> col=c("red","green","black"),
> bty="n",cex=0.8)
>
>
> Dieter
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help