Leon Barmuta
2005-Mar-17 00:48 UTC
[R] Varying grid.rect in different panels of a Lattice plot
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 library(lattice) library(grid) 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) trellis.device(theme=col.whitebg()) # 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", panel=myPanel, layout=c(1,2), xlab="", scales = list(x=list(rot = 45, at=as.numeric(Plotdate), labels=format(Plotdate, "%b%Y")))) Regards, Leon ------------------------- 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
Deepayan Sarkar
2005-Mar-17 01:28 UTC
[R] Varying grid.rect in different panels of a Lattice plot
On Wednesday 16 March 2005 18:48, Leon Barmuta wrote:> 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!If I understand you right, you could do something like: myPanel <- function(x, y, groups, subscripts, ...) { grid.rect(x = unit(x[groups[subscripts] == "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", groups = Spawning, panel = myPanel, layout= c(1,2), xlab="", scales = list(x=list(rot = 45, at=as.numeric(Plotdate), labels=format(Plotdate, "%b%Y")))) There's nothing (much) special about 'groups', you could also do myPanel <- function(x, y, flag, subscripts, ...) { grid.rect(x = unit(x[flag[subscripts] == "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", flag = Spawning, panel = myPanel, layout= c(1,2), xlab="", scales = list(x=list(rot = 45, at=as.numeric(Plotdate), labels=format(Plotdate, "%b%Y")))) except that if you had your variables in a data frame (supplied as the 'data' argument) and not in your search path, anything supplied as 'groups' would also be evaluated in the data frame. Deepayan> # Make up a short data set > > library(lattice) > library(grid) > > 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) > > trellis.device(theme=col.whitebg()) > > # 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", > panel=myPanel, > layout=c(1,2), xlab="", > scales = list(x=list(rot = 45, at=as.numeric(Plotdate), > labels=format(Plotdate, "%b%Y")))) > > Regards, > > Leon > > ------------------------- > 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