[R] Legend in the outer margin

Dieter Menne dieter.menne at menne-biomed.de
Mon Apr 10 15:40:15 CEST 2006


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")[2] + 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




More information about the R-help mailing list