[R] Legend outside plot => grid not limited to plot

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 1 23:29:58 CEST 2008


On Fri, 1 Aug 2008, Peter Mueller wrote:

> Hi dear R users,
>
> i would like to have:
> 1. the legend outside of the plot and
> 2. add a grid to the plot.
>
> For 1. I found a example in the R mailinglist archive [1].
> 1: http://tolstoy.newcastle.edu.au/R/help/06/01/19423.html
> The total example code would be:
>
>> ?par
>> par(xpd=T, mar=par()$mar+c(0,0,0,4))
>> plot(1,1)
>> legend(1.5,1,"point",pch=1)
>> grid(NULL,NULL,lwd=1)
>
> But the problem now is that the grid is not limited to the plot area. 
> Thge grid is drawn all over the image :-( . (Try the example if you 
> don't believe me :-) ).

Since it is what you asked for, R believed you (and so do we).

> How do I have to correct the code?

Don't use a global setting of xpd (and what's wrong with par("mar")?).

par(mar=par("mar")+c(0,0,0,4))
plot(1,1)
op <- par(xpd=TRUE)
legend(1.5,1,"point",pch=1)
par(op)
grid(NULL,NULL,lwd=1)

> Thanks for any hints,
> Peter


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list