Welcome to the Institute for Digital Research and Education

Stata FAQ
How can I make a bar graph with error bars?

Say that you were looking at writing scores broken down by race and ses.
You might want to graph the mean and confidence interval for each group using a
bar chart with error bars as illustrated below. This FAQ shows how you can make
a graph like this, building it up step by step.

First, lets get the data file we will be using.

use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear

Now, let's use the collapse command to make the mean and standard deviation by
race and ses.

Now we are ready to make a bar graph of the data The graph bar command makes a pretty
good bar graph.

graph bar meanwrite, over(race) over(ses)

We can make the graph look a bit prettier by adding the asyvars option as shown below.

graph bar meanwrite, over(race) over(ses) asyvars

But, this graph does not have the error bars in it. Unfortunately, as nice as the
graph bar command
is, it does not permit error bars. However, we can make a twoway graph that has error bars as shown
below. Unfortunately, this graph is not as attractive as the graph from graph
bar.

So, we have a conundrum. The graph bar command will make a lovely bar
graph, but will not support error bars. The twoway bar command makes
lovely error bars, but it does not resemble the nice graph that we liked from
the graph bar command.
However, we can finesse the twoway bar command to make a graph that
resembles the graph bar command and then combine that with error bars.
Here is a step by step process.

First, we will make a variable sesrace that will be a single variable that contains
the ses and race information. Note how sesrace has a gap between the
levels of ses (at 5 and 10).

Now, we will make a graph using graph twoway. Notice how the bars are in
three groups of four bars. The three groups correspond to the three levels of ses and
the four bars within each group correspond to the four levels of
race. You can relate this grouping to the way that we constructed raceses above.

twoway (bar meanwrite sesrace)

We can now overlay the error bars by overlaying a rcap graph

twoway (bar meanwrite sesrace) (rcap hiwrite lowrite sesrace)

This kind of looks like what we want, but it would look nicer if each of the
bars for the four different races were different colors. We can do this by
overlaying four separate bar graphs, one for each racial group.

The legend labels the bars nicely, but would look cleaner if it were just one row and
the x axis of the graph does not convey
that the three groups of bars correspond to the three groups of ses. We can
use the xlabel() option to remedy that. We also add better titles for the
x and y axes as well.

Now we have a graph that looks like the kind of graph that we would get
from graph bar but by finessing graph twoway bar into making
this pretty graph, we could then overlay the rbar graph to get the
error bars we desired.