[R] xyplot: getting data into the panel function

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Apr 4 14:59:16 CEST 2006



Søren Højsgaard wrote:
> In xyplot, I would like to get the "data into the panel function" in the following sense: Consider
> 
> xyplot(scc~time|cowidp, data=cow.s,type=c("l"),
>   panel=function(x,y,subscripts,...){
>     panel.xyplot(x,y,...)
>     vvv<-cow.s[which(!is.na(cow.s[subscripts,"mastreat"])),"time"]
>     panel.abline(v=vvv,col="red",lwd=2)
>   }
> )
>  
> If I want to use a different dataset, say cow.2, I would have to modify the code twice in the panel function. Is there a way of avoiding this?
>  
> Best regards
> Søren
> 
> 	[[alternative HTML version deleted]]
> 
> 

Hi, Søren,

I usually do this by creating a wrapper function:

plot.cow <- function(formula = ssc ~ time | cowidp, data, ...) {
   xyplot(formula, data = data, type = "l",
          panel = function(x, y, subscripts, time, mastreat, ...) {
            panel.xyplot(x, y, ...)
            vvv <- time[which(!is.na(mastreat[subscripts])]
            panel.abline(v = vvv, col = "red", lwd = 2)
          },
          time = data$time,
          mastreat = data$mastreat)
}

plot.cow(cow.s)
plot.cow(cow.2)

HTH,

--sundar




More information about the R-help mailing list