[R] Lattice xyplot(): adding a legend

David Winsemius dwinsemius at comcast.net
Thu Oct 20 07:42:58 CEST 2016


> On Oct 19, 2016, at 4:04 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> 
> On Wed, 19 Oct 2016, Rich Shepard wrote:
> 
>> I did read that but mis-applied what I read. Tried auto.key but that did
>> not work as desired. Now I know to learn how to apply 'key'.
> 
>  Almost there after another careful reading Section 9.2.3 ff in the book.
> Here's the command to produce the plot:
> 
> rainbyday <- xyplot(rain$amount ~ raindate, data = rain, main = "Area Precipitation",

I am getting annoyed, exhausted, and frustrated reading code like that. Never, ever, ... ever,  use the "$" operator in a formula.

Use the 'data' argument and the 'formula' as they are supposed to be used.



>    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),
>    y = list(at=c(min(rain$amount), max(rain$amount)))),
>    pch = 20,
>    col = c("black","red","dark green","dark blue","dark goldenrod","purple"),

> cl <- colors()
 
> which(cl=="dark goldenrod")
integer(0)
> which(cl=="darkgoldenrod")
[1] 75

Admittedly the error message was completely opaque.


>    key = simpleKey(text = levels(rain$station)[1:6],



>      x = 0.2, y = 0.6, corner = c(0, 0), points = TRUE))
> 
>  My question is how to pass pch = 20 to the key. simpleKey accepts only the
> logical TRUE as an argument and this produces a plot with filled circles for
> the data but unfilled circles for the key. I find no example of specifying
> pch for points in the key in either the book or ?xyplot, and I'm sure there
> is a way of having the key symbols match both pch and color as the data
> symbols.
> 

rainbyday <- xyplot(amount ~ raindate, data = rain, 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),
   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],
# Note: the need to "pick" the colors from a palette with a vector.
   key = simpleKey( text=as.character( unique(rain$station )), 
               col = c("black","red","darkgreen","darkblue","darkgoldenrod","purple"),
               pch = 20,
     x = 0.2, y = 0.6, corner = c(0, 0), points = TRUE)); print(rainbyday)


>  Is use of Rows() the solution?

I am unaware of any 'Rows" function.

> 
> Rich
> 
> ______________________________________________
> 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