[R] Lattice xyplot(): adding a legend [RESOLVED]

Rich Shepard rshepard at appl-ecosys.com
Thu Oct 20 20:56:06 CEST 2016

On Thu, 20 Oct 2016, David Winsemius wrote:

> Why are you creating this factor? The `date` column has the desirable
> properties associated with the "Date" class. Axis labeling will be
> correct.

David, et al.:

   Because originally I mis-understood the parameters; it's gone now.

> rainbyday2 <- xyplot(amount ~ date, data = rain, main = "Area Precipitation",
>   ylab = "Daily Total Amount (in)", xlab = "Date",
>   scales = list(x=list(at=rain$date[ c(1,8,15,22,29,36,43,50,57,62)], rot = 90),
>   y = list(at=c(min(rain$amount), max(rain$amount)))),
>   pch = 20,
>   col = c("black","red","dark green","dark blue","dark goldenrod","purple")[rain$station],
> # Needed to use the 'station'-factor to properly index the colors. Otherwise there is no 1-1 relationship.
>   key = list( text=list(levels(rain$station)),
>               fill = c("black","red","darkgreen","darkblue","darkgoldenrod","purple"),
>               pch = 16,
>     x = 0.2, y = 0.6, corner = c(0, 0), points = TRUE)); print(rainbyday2)

   I was getting close to this by stripping the xyplot() code to basics and
adding one element at a time. I removed the scales component and let xyplot
assign the date and quantity labels by default. You filled in the last piece
with the station factor.

   The symbols for the key using 'fill' are all black circles. Changing that
option to 'col' displays both station name and circles in the stated colors.
That makes it much easier to identify individual data points.

   Next time I'll do much better on my own and will research my saved
threads, search for mail list archived threads, and more closely read the
help pages for functions.

> Reading the archives shows that this is not the first time this problem of
> getting fills with the `key` parameter for xyplot has caused problems for
> you. Back in 2013 Richard Heiberger and S Ellison were your
> correspondents. I found that using `simpleKey` was unsuccessful due to
> problematic defaults.

   And that was the last time I used R for a project. Not all my projects
require R, GRASS, PostgreSQL, or other technical software so I am not a
constant user of any, other than LaTeX.

Many thanks for your patient help,


More information about the R-help mailing list