Hi all,

I've read the emails of Dan, Deepayan and Sundar about adding error bars to
the lattice plots (
https://stat.ethz.ch/pipermail/r-help/2006-October/114883.html), but I still
have the problem when I want to adding error bars to barchart. I tried both
the solution of Deepayan and Sundar but without luck. Here is my code (I
changed prepanel.ci and panel.ci a little to plot bars vertically):


###################### Sundar's solution ###############################
prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
    y <- as.numeric(y)
    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,
                      groups = NULL, pch = 16, ...) {
   x <- as.numeric(x)
   y <- as.numeric(y)
   ly <- as.numeric(ly[subscripts])
   uy <- as.numeric(uy[subscripts])
   par <- if(is.null(groups))"plot.symbol" else "superpose.symbol"
   sym <- trellis.par.get(par)
   col <- sym$col
   groups <- if(!is.null(groups)) {
     groups[subscripts]
   } else {
     rep(1, along = x)
   }
   ug <- unique(groups)
   for(i in seq(along = ug)) {
     subg <- groups == ug[i]
     y.g <- y[subg]
     x.g <- x[subg]
     ly.g <- ly[subg]
     uy.g <- uy[subg]
     panel.abline(h = unique(y.g), col = "grey")
     panel.arrows(ly.g, y.g, uy.g, y.g, col = 'black',
                  length = 0.25, unit = "native",
                  angle = 90, code = 3)
     panel.barchart(x.g, y.g, pch = pch, col = col[i], ...)
   }
}


all = barchart(
  Score ~ Methods | Score.Name * RNA.Type,
  data = benchmark,
  box.ratio = 1.2,
  xlab = 'Methods',
  ylab = 'Percentage',
  groups = Seq.Number,
  layout = c(2, 5), # 2 columns per row
  between = list( y = 0.5, x = 0 ),
#  par.settings = list(fontsize=list(text=8)),
  ## specify the colors used for bars
  par.settings = list(fontsize=list(text=8), superpose.polygon = list(border
= 'black', col = c('white', 'gray', 'black'))),
  par.strip.text = list(cex=0.9),
  auto.key = list(space = 'top', columns = 3, cex = 0.7),
#  key = key.variety,
#  index.cond = list(c('tRNA', '5S rRNA', 'SRP RNA', 'RNase P', '16S
rRNA')),
#  index.cond = list(rep(1,6)),
#  ylim = my.ylim,
  scales = list(x = list(rot = 45), y=list(tck = 0.4, rot = 0, relation =
'free')),
  ly = benchmark$Score - benchmark$Error,
  uy = benchmark$Score + benchmark$Error,
  prepanel = prepanel.ci,
  panel.groups = panel.ci
  )


################################ Deepayan's solution
################################
prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
    y <- as.numeric(y)
    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, ...) {
    x <- as.numeric(x)
    y <- as.numeric(y)
    ly <- as.numeric(ly[subscripts])
    uy <- as.numeric(uy[subscripts])
    panel.barchart(x, y, ...)
    panel.arrows(x, ly, x, uy, col = 'black',
                 length = 0.1, unit = "native",
                 angle = 90, code = 3)
}

all = barchart(
  Score ~ Methods | Score.Name * RNA.Type,
  data = benchmark,
  box.ratio = 1.2,
  xlab = 'Methods',
  ylab = 'Percentage',
  groups = Seq.Number,
  layout = c(2, 5), # 2 columns per row
  between = list( y = 0.5, x = 0 ),
#  par.settings = list(fontsize=list(text=8)),
  ## specify the colors used for bars
  par.settings = list(fontsize=list(text=8), superpose.polygon = list(border
= 'black', col = c('white', 'gray', 'black'))),
  par.strip.text = list(cex=0.9),
  auto.key = list(space = 'top', columns = 3, cex = 0.7),
#  key = key.variety,
#  index.cond = list(c('tRNA', '5S rRNA', 'SRP RNA', 'RNase P', '16S
rRNA')),
#  index.cond = list(rep(1,6)),
#  ylim = my.ylim,
  scales = list(x = list(rot = 45), y=list(tck = 0.4, rot = 0, relation =
'free')),
  ly = benchmark$Score - benchmark$Error,
  uy = benchmark$Score + benchmark$Error,
  prepanel = prepanel.ci,

  panel.groups = panel.ci,


  panel = panel.superpose  )



Sundar's solution gives me the exact same original plot without error
bars, and Deepayan's solution gives me a messy plot. Did I mess up
anything in these two solutions? I'd appreciate any help from you
experts. Thanks

-- 
Best,
Zhenjiang

	[[alternative HTML version deleted]]

