[R] Error bars within xyplot, panel = function(x,y, ....)

David Afshartous dafshartous at med.miami.edu
Mon Jun 16 23:18:42 CEST 2008




All,


I'm trying to adapt some code provided by Deepayan Sarkar from a previous
thread (https://stat.ethz.ch/pipermail/r-help/2005-October/081571.html) on
this topic.

## This code produces a graph w/o error bars:

xyplot(Y ~ Hr, data, groups=DRUG,
    panel=function(x,y,...){
          panel.xyplot(x,y,...,  type=c("g", "l")  )
          panel.points(x,y,..., pch=16, type='p', col='black', cex=1)
       },
auto.key = list(space = "top",  text = c( "D","P"), points = FALSE, lines =
TRUE, columns=2), par.settings = list(superpose.line = list(lty = c(1,5),
col=c('black', 'black') ) ) )


## this code uses the functions provided by Deepayan Sarkar to include the
## error bars for the same data:

xyplot(Y ~ Hr,
        groups=DRUG,
        data=data,
        ly = data$lower,
        uy = data$upper,
        prepanel = prepanel.ci,
        panel = panel.superpose,
        panel.groups = panel.ci,
        type="b",
        auto.key = list(space = "top",  text = c( "D","P"), points = FALSE,
lines = TRUE, columns=2),
par.settings = list(superpose.line = list(lty = c(1,5), col=c('black',
'black') ) )
        )

Is it possible to write the second version in the format of the first, i.e.,
using panel = function(x,y, ...){    } ?

Thanks,
David

############################################################################
### load the following:

Hr = c(0,1,2,3,4,5,0,1,2,3,4,5)
DRUG = rep(c("D", "P"), each=6)
Y = c(1,2,2,2,2,1,3, 4, 4,4, 4, 3)
data = data.frame(Hr, DRUG, Y)
data$lower = data$Y - .5
data$upper = data$Y + .5


prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
    x <- as.numeric(x)
    ly <- as.numeric(ly[subscripts])
    uy <- as.numeric(uy[subscripts])
    list(ylim = range(y, uy, ly, finite = TRUE)) }

panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) {
    x <- as.numeric(x)
    y <- as.numeric(y)
    ly <- as.numeric(ly[subscripts])
    uy <- as.numeric(uy[subscripts])
    panel.arrows(x, ly, x, uy, col = "black",
                 length = 0.25, unit = "native",
                 angle = 90, code = 3)
    panel.xyplot(x, y, pch = 16, ...)}



More information about the R-help mailing list