[R] plot and legend

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Aug 19 12:52:51 CEST 2005


On Thu, 2005-08-18 at 18:19 +0200, svenknueppel at reilich.net wrote:
> Hello,
> 
> I would like make a plot with a legend. How can I take the legend
> outside of the plot frame?
> 

Does this do what you want?

## change the plotting parameters and store defaults
## mar sets 2 more lines in the margin than default at the top
## xpd = TRUE stops clipping to the plot region only
oldpar <- par(mar = c(5, 4, 6, 2) + 0.1, xpd = TRUE)

## do a plot
plot(1:10)

## now build a legend, the y co-ordinate pushes the legend outside the
## plotting region - x & y are on same scale as your axes
## xjust = 0.5 centres the legend on the x coordinate
legend(x = 5.5, y = 11.5, "legend text", pch = 1, xjust = 0.5)

##restore the defaults
par(oldpar)

It would be nice if legend allowed the use of "top" etc. to relate to
the device if the user wanted. It would simplify this kind plot
arrangement.

HTH

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list