Hello,

I am attempting to write a script that adds error bars to a barchart. I
basing my attempt heavily on the following thread:

http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2791.html

I can't seem to get around the problem that was discussed in the thread. The
following example should illustrate my problem. Sorry about the messy
example but I am 1) trying to make it as close as possible to my actual work
and 2) my skill level is spotty at best. Can anyone suggest a way to do this
or even another way to make a grouped barchart with error bars? I'm not
married to this method although I prefer working with lattice. Thanks for
any help in advance!

Sam

#Generating the data
library(lattice)

temp <- abs(rnorm(81*5))
err <- as.data.frame(temp)
err$section=c("down","down","down","mid","mid","mid", "up","up", "up")

err$depth=c("Surface","D50", "2xD50")

err$err.date=c("05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","28/08/2009",
"28/08/2009", "28/08/2009","28/08/2009", "28/08/2009",
"28/08/2009","28/08/2009", "28/08/2009", "28/08/2009")


err.split <-
     with(err,
          split(temp, list(depth,section, err.date)))

#I've tried to alter the panel function according to the thread to produce
vertical error bars in my barcharts

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, lx, ux, subscripts, pch = 16, ...) {
         x <- as.numeric(x)
         y <- as.numeric(y)
         lx <- as.numeric(lx[subscripts])
         ux <- as.numeric(ux[subscripts])

         panel.arrows(x, ly, x, uy, col = 'black',
                  length = 0.25, unit = "native",
                  angle = 90, code = 3)
         panel.barchart(x, y, pch = pch, ...)
     }

se <-function(x) sqrt(var(x)/length(x))



err.ucl <- sapply(err.split,
            function(x) {
                st <- boxplot.stats(x)
                c(mean(x), mean(x) + se(x), mean(x) -se(x))
            })



err.ucl <- as.data.frame(t(err.ucl))
names(err.ucl) <- c("mean", "upper.se", "lower.se")
err.ucl$label <- factor(rownames(err.ucl),levels = rownames(err.ucl))

# add factor, grouping and by variables
err.ucl$section=c("down","down","down","mid","mid","mid", "up","up", "up")
err.ucl$depth=c("Surface","D50", "2xD50")

#There has got to be a better way of doing this
err.ucl$err.date=c("05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","28/08/2009",
"28/08/2009", "28/08/2009","28/08/2009", "28/08/2009",
"28/08/2009","28/08/2009", "28/08/2009", "28/08/2009")

#This produces the figure I am looking for minus the error bars.

with(err.ucl, barchart(mean ~ err.date | section, group=depth,
        layout=c(1,3),
         horizontal=FALSE,
         scales=list(x=list(rot=45)),
        ))


# Deepayan's original example. I am unsure how to diagnose the packet error.
This is where I run into problems

with(err.ucl, barchart(mean ~ err.date | section, group=depth,
         layout=c(1,3),
         horizontal=FALSE,
         scales=list(x=list(rot=45)),
         ly=lower.se,
         uy=upper.se,
         prepanel=prepanel.ci,
         panel=panel.superpose,
         panel.groups=panel.ci
         ))





-- 
*****************************************************
Sam Albers
Geography Program
University of Northern British Columbia
3333 University Way
Prince George, British Columbia
Canada, V2N 4Z9
phone: 250 960-6777
*****************************************************

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to