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

David Wyllie David.Wyllie at ndm.ox.ac.uk
Fri Jun 18 13:49:11 CEST 2010

```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

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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: David Wyllie.vcf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100618/369e9f48/attachment.pl>
```