[R] Varying grid.rect in different panels of a Lattice plot

From: Leon Barmuta <Leon.Barmuta_at_utas.edu.au>
Date: Thu 17 Mar 2005 - 11:48:37 EST

Dear r-help,

Sleep-deprivation from having 2 youngsters under 2 around the house is fuzzing my brain, so please be gentle if the answer to this query is obvious!

In the example below, I'm trying to use grid.rect to add grey rectangles to the panels of a lattice plot to indicate which months spawning occurred of a (very cute) native Tasmanian fish. The fish in the two lakes spawned at slightly different times, so grid.rect needs to be conditioned on when spawning happened in each lake. However, the panel function I wrote first, and reproduce below, inserts grey rectangles for all dates that spawning occured pooled across the two lakes. So after dreging r-help, I've messed with subscripts and tried fiddling with panel.number, but can't get this to work. Any suggestions most welcome!

# Make up a short data set


Lake <- rep(c("Crescent","Sorell"), each=13) Spawning <- c("Y", rep("N",7), rep("Y",4), rep("N",8), rep("Y",5), "N") Catch <- rpois(26, 30) # fake data
Plotdate <- rep(seq(as.Date("2000/10/1"), by="month", length=13),2)


# panel function that doesn't quite work

myPanel <- function(x, y, ...) {
  grid.rect(x=unit(Plotdate[Spawning == "Y"], "native"),

       just="left", width=unit(31, "native"), # a bit of a fudge
       gp=gpar(col="transparent", fill="light grey"))
       panel.xyplot(x, y, ...)}

xyplot(Catch ~ Plotdate|Lake, type="b",
       layout=c(1,2), xlab="",
       scales = list(x=list(rot = 45, at=as.numeric(Plotdate),
                     labels=format(Plotdate, "%b%Y"))))



Dr Leon A. Barmuta, Senior Lecturer in Zoology School of Zoology & TAFI, University of Tasmania, Private Bag 5, Hobart, Tasmania 7001, Australia
Phone (03) 6226 2785; Fax (03) 6226 2745; International callers replace (03) with +61 3
School of Zoology web page: http://www.scieng.utas.edu.au/zoo/ My web page: http://www.scieng.utas.edu.au/zoo/pagedetails.asp?lpersonId=222

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 17 11:56:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:50 EST