[R] adding error bars to lattice plots

Daniel E. Bunker deb37 at columbia.edu
Thu Oct 12 18:48:05 CEST 2006


Dear R users,

About a year ago Deepayan offered a suggestion to incorporate error bars 
into a dotplot using the singer data as an example 
<<http://finzi.psych.upenn.edu/R/Rhelp02a/archive/63875.html>>.

When I try to utilize this code with a grouping variable, I get an error 
stating that the subscripts argument is missing.  I have tried to insert 
them in various ways, but cannot figure out where they should go.

Deepayan's original code follows, with additions from me for factor, 
grouping and by variables.

(Note that I could use xYplot (Dotplot), but I need my response variable 
on the vertical axis.)

Any suggestions would be greatly appreciated.

Thanks, Dan

prepanel.ci <- function(x, y, lx, ux, subscripts, ...) {
        x <- as.numeric(x)
        lx <- as.numeric(lx[subscripts])
         ux <- as.numeric(ux[subscripts])
         list(xlim = range(x, ux, lx, finite = TRUE))
     }

panel.ci <- function(x, y, lx, ux, subscripts, pch = 16, ...) {
         x <- as.numeric(x)
         y <- as.numeric(y)
         lx <- as.numeric(lx[subscripts])
         ux <- as.numeric(ux[subscripts])
         panel.abline(h = unique(y), col = "grey")
         panel.arrows(lx, y, ux, y, col = 'black',
                  length = 0.25, unit = "native",
                  angle = 90, code = 3)
         panel.xyplot(x, y, pch = pch, ...)
     }

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

# add factor, grouping and by variables
singer.ucl$fac1=c("level1","level1", "level2", "level2")
singer.ucl$by1=c("two","one")
singer.ucl$group1=c(rep(letters[1],4),rep(letters[2],4))

## show the data frame
singer.ucl

# Deepayan's original example
with(singer.ucl,
      xyplot(voice.part ~ median,
             lx = lower, ux = upper,
             prepanel = prepanel.ci,
             panel = panel.ci),
         horizontal=FALSE)

# with by variable, works fine
with(singer.ucl,
      xyplot(voice.part ~ median|by1,
             lx = lower, ux = upper,
             prepanel = prepanel.ci,
             panel = panel.ci))

# with groups, fails for lack of subscripts.
with(singer.ucl,
      xyplot(voice.part ~ median, groups=group1,
             lx = lower, ux = upper,
             prepanel = prepanel.ci,
             panel = panel.ci))


# what I need, ultimately, is something like this, with error bars:

with(singer.ucl,
      dotplot(median~fac1|by1, groups=group1))



More information about the R-help mailing list