[R] error bars in lattice xyplot *with groups*

Mike Lawrence Mike.Lawrence at Dal.Ca
Tue Jun 6 05:20:08 CEST 2006


Hi all,

I'm trying to plot error bars in a lattice plot generated with xyplot. Deepayan
Sarkar has provided a very useful solution for simple circumstances
(https://stat.ethz.ch/pipermail/r-help/2005-October/081571.html), yet I am
having trouble getting it to work when the "groups" setting is enabled in
xyplot (i.e. multiple lines). To illustrate this, consider the singer data
generated by the above linked solution previously submitted:

#####################
library(lattice)
singer.split <-
    with(singer,
         split(height, voice.part))

singer.ucl <-
    sapply(singer.split,
           function(x) {
               st <- boxplot.stats(x)
               c(st$stats[3], st$conf)
           })

singer.ucl <- as.data.frame(t(singer.ucl))
names(singer.ucl) <- c("median", "lower", "upper")
singer.ucl$voice.part <-
    factor(rownames(singer.ucl),
           levels = rownames(singer.ucl))

#now let's split up the voice.part factor into two factors,
singer.ucl$voice=factor(rep(c(1,2),4))
singer.ucl$range=factor(rep(c("Bass","Tenor","Alto","Soprano"),each=2))

#here's Deepayan's previous solution, slightly modified to depict
#  the dependent variable (median) and the error bars on the y-axis
#  and the independent variable (voice.part) on the x-axis
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 = pch, ...)
}


#this graph works
xyplot(median ~ voice.part,
	data=singer.ucl,
	ly = singer.ucl$lower,
	uy = singer.ucl$upper,
	prepanel = prepanel.ci,
	panel = panel.ci,
	type="b"
)

#this one does not (it will plot, but will not seperate the groups)
xyplot(median ~ voice,
	groups=range,
	data=singer.ucl,
	ly = singer.ucl$lower,
	uy = singer.ucl$upper,
	prepanel = prepanel.ci,
	panel = panel.ci,
	type="b"
)

####################################

Any suggestions?

-- 

Mike Lawrence

Mike.Lawrence at Dal.Ca

"The road to Wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
- Piet Hein



More information about the R-help mailing list