Skip to content

Constructing bar charts with standard error bars

4 messages · John Zabroski, Frank E Harrell Jr, Ben Bolker

#
John Zabroski wrote:
There are severe problems with dynamite plots such as these.  See 
http://biostat.mc.vanderbilt.edu/DynamitePlots for a list of problems 
and solutions.

Frank

  
    
#
John Zabroski <johnzabroski <at> gmail.com> writes:
[snip]
barplot() returns the x values you need for x0 and x1.
barplot(...,ylim=c(0,xbar+se)) will set the upper y limit so
the bars don't get cut off.

Here are three ways to create such a barplot (I will add
this to the wiki once it's back on line):  (n.b.: the
with() command is just a shortcut to avoid having
to type testdata$xbar, testdata$group, etc.)

## 1. tweaked version of what you did above

testdata <- data.frame(group=c(400,200,100,50,25),
	xbar= c(0.36038 , 0.35927 , 0.35925 , 0.35712 , 0.35396), 
	se = c(0.02154,0.02167,0.02341,0.01968,	0.01931))
xvals = with(testdata,
     barplot(xbar, names.arg=group, main="a=4.0",
        xlab="Group", ylab="xbar",ylim=c(0,max(xbar+se))))
with(testdata,
     arrows(xvals, xbar, xvals, xbar+se, length=0.4, angle=90, code=3))

## 2. using the plotCI function from plotrix to draw the
##    arrows instead
library(plotrix)
xvals = with(testdata,
     barplot(xbar, names.arg=group, main="a=4.0",
        xlab="Group", ylab="xbar",ylim=c(0,max(xbar+se))))
with(testdata,
     plotCI(xvals, xbar, liw=0,uiw=se,add=TRUE,pch=NA,gap=FALSE))

## 3. the most automatic way, using barplot2() from the
## gplots package

library(gplots)
with(testdata,
     barplot2(xbar,names.arg=group,main="a=4.0",
              xlab="Group",ylab="xbar",plot.ci=TRUE,
              ci.u=xbar+se,ci.l=xbar))

  P.S. I hope you're not hoping to infer a statistically
significant difference among these groups ...

  cheers
   Ben Bolker
#
On 7/25/07, Ben Bolker <bolker at ufl.edu> wrote:
Thanks a lot!  I tried all three and they all seem very dependable.
Also, I appreciate you rewriting my solution and adding elegance.

Is there a way to extend the tick marks to the ylim values, such that
the yscale ymax tickmark is something like max(xbar+se)?  In the
documentation, I thought par(yaxp=c(y0,y1,n)) would do the trick, but
after trying to use it I am not sure I understand what yaxp even does.

P.S. I am not looking for statistically significant differences.  I am
trying to learn how to leverage R's graphing capabilities.  I also
appreciate Frank Harrell referring me to the link about Dynamite Plots
and associated weaknesses.