[R] Hopefully an easy error bar question

Frank E Harrell Jr f.harrell at vanderbilt.edu
Sat Sep 6 14:19:33 CEST 2008

Brown, Heidi wrote:
> Hi im trying to add error bars to my barplots, there very basic, i have a few grapghs where the y variable is different but on all the X variable is Age (Adult and Juvenile) however this is split into two levels so i have males and females, so my graph basically has four bars on it.
> I know how to add eror bars for instance when there is only one level eg lookng at the diffrence between male and female lizards and tree height and have used the code:
> error.bars<-function(yv,z,nn) {
>  xv<-
> barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv)
> ))
> g=(max(xv)-min(xv))/50
> for (i in 1:length(xv)) {
> lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i]))
> lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i]))
> lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i]))
> }}
> Have then worked out the standard errors and added that using:
> se<-c()
> and then added the final bit of code:
> labels<-as.character(levels(Sex2))
> ybar<-as.vector(tapply(Height2,Sex2,mean))
> error.bars(ybar,se,labels)
> So i was wondering, in order to get error bars on my barplot with two levels do i need to change the last bit of code. For example im looking at mean body condition (y) against age (x) and sex (level), i tried to change the code to the following but it didnt work, any suggestions?
> labels<-as.character(levels(list(sex,Age)))
> ybar<-as.vector(tapply(ConditionIndex,sex,Age,mean))
> error.bars(ybar,se,labels)

See http://biostat.mc.vanderbilt.edu/DynamitePlots for many reasons not 
to use dynamite plots.


Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

More information about the R-help mailing list