Re: [R] how to apply a panel function to each of several data series plotted on the same graph in lattice

From: David Wyllie <David.Wyllie_at_ndm.ox.ac.uk>
Date: Fri, 18 Jun 2010 12:49:11 +0100

Hi  

Thanks, but maybe I'm not making myself clear enough here. I think I'm already doing what you suggest, below the line # plot with fits .. in the original mail.  

I have made a little progress. I wish to apply a panel function to each of a,b,and c. When plotting
xyplot(a + b + c ~ x, type = 'l'),
in the below example the 'x' passed to the panel function is a numeric vector of 300 length, with no way of telling whether the data came from a,b, or c.  

I have found a discussion here
http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg94720.html which is helpful.  

One can view the same data in a 'long format' where the property of being 'a' 'b' or 'c' is in a third column, 'grp'.  

## code example starts here  

# i.e instead of generate mock data; this comprises three data series, a,b,c with means of about 5,35, and 105.
test <- data.frame(

a = runif(100, min=0, max=10), 
b = 30+runif(100, min=0, max=10), 
c = 100+runif(100, min=0, max=10), 
x = 1:100)
 

# we do one can have the data in three columns,
# we arrange long so we can use groups and superpose
test2a<-data.frame (
y=runif(100, min=0, max=10), x=1:100, grp=rep('a',100)) test2b<-data.frame (
y=30+runif(100, min=0, max=10), x=1:100, grp=rep('b',100)) test2c<-data.frame (
y=100+runif(100, min=0, max=10), x=1:100, grp=rep('c',100)) test2<-rbind(test2a, test2b, test2c)  

library(lattice)
# illustrate data the former way

xyplot(a + b + c ~ x, data = test, type = "p", auto.key=TRUE)

# or equivalently

xyplot(y~ x|grp, data = test2, type = "p", auto.key=TRUE) ## now the problem, which is to fit trends through the data
# the latter way, one can as described do http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg94539.html
xyplot(y~x, groups=grp,data=test2,

       type='l',
       panel = panel.superpose,
       panel.groups = function(x, y, ..., lty) {
               panel.xyplot(x, y, ..., lty = lty)
               panel.abline(h = mean(y), lty=3, ...) 
 }
)
# and this succeeds. however, when we then use the panel function
# Deepayan Sarkar code from: http://www.mail-archive.com/r-help@r-project.org/msg39654.html
 panel.smooth.spline <- function(x, y,
            w=NULL, df, spar = NULL, cv = FALSE, 
            lwd=plot.line$lwd, lty=plot.line$lty,col, col.line=plot.line$col, 
            type, horizontal=FALSE,... ) 
{ 
       x <- as.numeric(x) 
          y <- as.numeric(y)
 
          ok <- is.finite(x) & is.finite(y) 

          if (sum(ok) < 1) 
              return() 
          if (!missing(col)) { 
              if (missing(col.line)) 
                  col.line <- col 
          } 
          plot.line <- trellis.par.get("plot.line") 
          if (horizontal) { 
              spline <- 
                  smooth.spline(y[ok], x[ok], 
                                w=w, df=df, spar = spar, cv = cv, ...) 
              panel.lines(x = spline$y, y = spline$x, col = col.line, 
                  lty = lty, lwd = lwd, ...) 
          } 
          else { 
              spline <- 
                  smooth.spline(x[ok], y[ok], 
                                w=w, df=df, spar = spar, cv = cv, ...) 
              panel.lines(x = spline$x, y = spline$y, col = col.line, 
                  lty = lty, lwd = lwd, ...) 
          } 
      }
 

# and try

xyplot(y~x, groups=grp,data=test2,

       type='l',
       panel = panel.superpose,
       panel.groups = function(x, y, ..., lty) {
               panel.xyplot(x, y, ..., lty = lty)
               panel.smooth.spline( ...) 
 }
)  

# then we generate an error 'error using packet 1', and I'm not sure why.
 

Thanks
David  

David Wyllie, PhD
University Research Lecturer
The Jenner Institute
Oxford University
Old Road Campus Research Building
Roosevelt Drive
Oxford OX3 7DQ
UK  

fax +44 (0)1865 617608
office +44 (0) 1865 617627
lab +44 (0) 1865 617649
mobile 07988 550815  

>>> Dennis Murphy <djmuser_at_gmail.com> 6/18/2010 12:18 pm >>> Hi:

Here's a simple example:

a <- rnorm(100)
b <- rnorm(100, 2)
c <- rnorm(100, 5)
x <- 1:100

library(lattice)
xyplot(a + b + c ~ x, type = 'l')

HTH,
Dennis



R-help_at_r-project.org mailing list
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.
Received on Fri 18 Jun 2010 - 11:51:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jun 2010 - 11:10:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive