This post shows how to prepare a stacked bar graph in STATA. We will use data on school pupils in grades one to four, where for each grade we show percentage of pupils based on their performance compared to the previous year (those with and without growth in math scores).

While making any graph in STATA it is important to have the data in the form suitable for making the desired graph. Bar graph command in STATA is very powerful and it can make data manipulations/calculations in itself, however, this post suggests a rather simplistic approach to preparing a stacked bar graph which uses already prepared data.

We start by simulating the data using the code below.

``````clear
1
2
3
4
end

g mathgrowth_yes = round(runiform(),0.01)
g mathgrowth_no = 1 - mathgrowth_yes

list, clean noobs``````

At this point, our data looks as below.

Next, we prepare our stacked bar graph, with minimal use of options at this point.

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack
``````

Now, let’s make our graph a little nicer. We start by changing the legend labels.

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///
legend(order(1 "Met Growth Goal" 2 "Did not meet growth goal"))``````

Now let’s change the Y-axis labels to percentages (manually), remove ticks and grids, make the labels horizontal using the angle option, and finally reduce the labels size to small.

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///
legend(order(1 "Met Growth Goal" 2 "Did not meet growth goal")) ///
ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small))``````

Then, I will change the graph region color to white.

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///
legend(order(1 "Met Growth Goal" 2 "Did not meet growth goal")) ///
ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small)) ///
graphregion(color(white)) ``````

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///
legend(order(1 "Met Growth Goal" 2 "Did not meet growth goal")) ///
ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small)) ///
graphregion(color(white)) ///

Next, let’s add bar labels to see the actual bar height (make them white color, position them inside the graph, format with 2 decimal places).

``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///
legend(order(1 "Met Growth Goal" 2 "Did not meet growth goal") region(lcolor(white))) ///
ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small)) ///
graphregion(color(white)) ///
``````graph bar mathgrowth_yes mathgrowth_no, over(grade) stack ///