[R] Lattice xyplot(): adding a legend

David Winsemius dwinsemius at comcast.net
Wed Oct 19 21:20:16 CEST 2016


> On Oct 19, 2016, at 10:48 AM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> 
>  After reading ?xyplot and pages 161-162 in Deepayan's book I'm still not
> getting the syntax correct to add a legend to a scatterplot. The data are
> attached as filename rain.dput.
> 
>  Without the legend (and testing interactively) the data plot but not the
> legend. Sourcing the script opens the display but neither data nor legend
> appear. I know that I'll need to adjust the legend's position so it fits in
> the empty space at the top, left center, but I need to see where it is
> first.
> 
>  I tried adding the legend to the xyplot() command with no difference in
> the results.
> 
>  In both cases R displays this error:
> 
> source("plot-rain-by-day.R")
> Error in as.graphicsAnnot(legend) (from plot-rain-by-day.R#16) :
>  argument "legend" is missing, with no default
> 
>  I've not found an example to follow so pointers to how to add the legend
> are needed.


> 
>  The script:
> 
> # This scatter plot has rain amounts by day for each station, distinguished by color.
> 
> # load rain data file
> rain
> 
> # Create a factor from the date
> raindate <- as.factor(rain$date)
> 
> # Save original plotting parameters
> opar <- par(xpd=NA,no.readonly=T)
> 
> # Prepare the plot of the data
> rainbyday <- xyplot(rain$amount ~ raindate, main="Area Precipitation", ylab="Daily Total Amount (in)", xlab="Date", scales=list(x=list(at=c(1,8,15,22,29,36,43,50,57,62), rot=90)), pch=as.numeric(rain$station), col=c("black","red","dark green","dark blue","dark goldenrod","dark goldenrod"))
> 
> # Add a legend
> legend(x=0.83, y=-1.65, pch=as.numeric(rainbyday), col=c("black","red","dark green","dark blue","dark goldenrod","dark goldenrod"))


The `legend` function is used with base graphics plotting (as is the par function). I think you might first read ?Lattice since it makes reference to the fact that you can update trellis structures after they have been created.  The commands to add "legend" features to lattice plots are found under the argument items with names "key". Search in ?xyplot for the strings 'legend' and 'key'. It would probably be easier to incorporate these in a new plot call. There are several examples (well, four anyway)  at the ?xyplot page. Since you mentioned having Sarkar's book, the place to look for more examples is chapter 9 "Labels and Legends".


> # Plot it
> plot(rainbyday)
> 
> # Reset display parameters
> par(opar)
> 
> TIA,
> 
> Rich<rain.dput>______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list