[R] how to add legend to time series plot

Jason Turner jasont at indigoindustrial.co.nz
Thu Apr 15 22:03:42 CEST 2004


Uwe Ligges wrote:
> 
> I guess it is clipped in this case. Use par("usr") after ts.plot() to 
> see what the user coordinates are, and whether (1, -0.3) is a sensible 
> location to place the legend.
> 
> Uwe Ligges

Just to add to Uwe's good suggestion -- what I usually do is

opar <- par(no.readonly=TRUE)
par(usr=c(0,1,0,1))

Then pick legend co-ordinates, knowing that x and y on the plot canvas 
now range from 0 to 1.  For somewhere near the top-right corner...

legend(0.8,0.8, ...)
par(opar)  ## when done with the plot, to restore the co-ords.

Another choice is instead of the "par" calls...

legend(locator(1), ...)

and click on the plot where you want the legend.

Thirdly, the Hmisc package has a function largest.empty(), which can 
find the "best" place to put a legend in a plot automagically.

Cheers

Jason




More information about the R-help mailing list