Skip to content
Prev 293322 / 398502 Next

error bars for a barchart

Walmes, Thank you so much!!!
I am still trying to understand all of your code but it works. I have changed it a bit so that I get upper and lower limits for the error bar, and that the origin starts at 0 so the negative values are plotted correctly.

barchart(Change~fTreat,groups=Process,change,
         stderr=change$stderr,
         ylab="Pocertage change",
         xlab="Treatment",
         #ylim=-115:50,
         scales=list(alternating=FALSE,
                     tick.number=7,
                     tck=c(-1,0)),
         prepanel=function(y, stderr, subscripts=subscripts, ...){
           uy <- as.numeric(y+stderr[subscripts])
           ly <- as.numeric(y-stderr[subscripts])
           list(ylim=range(y,uy,ly, finite=TRUE))
         },
         panel=
           function(x, y, subscripts, groups, stderr, box.ratio, ...){
             panel.barchart(x, y, subscripts=subscripts,
                            groups=groups, box.ratio=box.ratio,origin=0, ...)
             panel.abline(h=0,col="black",...)
             d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
             g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
             panel.arrows(as.numeric(x)+g,y-stderr[subscripts], as.numeric(x)+g, y+stderr[subscripts],
                          code=3,angle=90, length=0.025)
           }
         )
I am very new to creating function and would be great if you could explain what the d and g elemens actually do?
this is just for me to understand and later maybe make my own functions. I am assuming that g centers the error bars? but d?

Regards

Beatriz de Francisco Mora
PhD Student
The Scottish Association for Marine Science
Scottish Marine Institute
Oban
PA37 1QA
Tel: 06131 559000 (switchboard)
Fax: 01631559001
E. beatriz.defrancisco at sams.ac.uk
http://www.smi.ac.uk/beatriz-de-franciso