Help the Stat Consulting Group by giving a gift

How can I make a bar graph with error bars?

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

Now, let's use theuse http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear

Now, let's make the upper and lower values of the confidence interval.collapse (mean) meanwrite= write (sd) sdwrite=write (count) n=write, by(race ses)

Now we are ready to make a bar graph of the data Thegenerate hiwrite = meanwrite + invttail(n-1,0.025)*(sdwrite / sqrt(n)) generate lowrite = meanwrite - invttail(n-1,0.025)*(sdwrite / sqrt(n))

We can make the graph look a bit prettier by adding thegraph bar meanwrite, over(race) over(ses)

But, this graph does not have the error bars in it. Unfortunately, as nice as thegraph bar meanwrite, over(race) over(ses) asyvars

So, we have a conundrum. Thegraph twoway (bar meanwrite race) (rcap hiwrite lowrite race), by(ses)

First, we will make a variable

Now, we will make a graph usinggenerate sesrace = race if ses == 1 replace sesrace = race+5 if ses == 2 replace sesrace = race+10 if ses == 3 sort sesrace list sesrace ses race, sepby(ses)+---------------------------------+ | sesrace ses race | |---------------------------------| 1. | 1 low hispanic | 2. | 2 low asian | 3. | 3 low african-amer | 4. | 4 low white | |---------------------------------| 5. | 6 middle hispanic | 6. | 7 middle asian | 7. | 8 middle african-amer | 8. | 9 middle white | |---------------------------------| 9. | 11 high hispanic | 10. | 12 high asian | 11. | 13 high african-amer | 12. | 14 high white | +---------------------------------+

We can now overlay the error bars by overlaying atwoway (bar meanwrite 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 separatetwoway (bar meanwrite sesrace) (rcap hiwrite lowrite sesrace)

The legend labels the bars nicely, but would look cleaner if it were just one row and thetwoway (bar meanwrite sesrace if race==1) /// (bar meanwrite sesrace if race==2) /// (bar meanwrite sesrace if race==3) /// (bar meanwrite sesrace if race==4) /// (rcap hiwrite lowrite sesrace)This is looking better, but let's use the legend to label the bars better.twoway (bar meanwrite sesrace if race==1) /// (bar meanwrite sesrace if race==2) /// (bar meanwrite sesrace if race==3) /// (bar meanwrite sesrace if race==4) /// (rcap hiwrite lowrite sesrace), /// legend( order(1 "Hispanic" 2 "Asian" 3 "Black" 4 "White") )

Now we have a graph that looks like the kind of graph that we would get fromtwoway (bar meanwrite sesrace if race==1) /// (bar meanwrite sesrace if race==2) /// (bar meanwrite sesrace if race==3) /// (bar meanwrite sesrace if race==4) /// (rcap hiwrite lowrite sesrace), /// legend(row(1) order(1 "Hispanic" 2 "Asian" 3 "Black" 4 "White") ) /// xlabel( 2.5 "Low" 7.5 "Middle" 12.5 "High", noticks) /// xtitle("Socio Economic Status") ytitle("Mean Writing Score")

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.