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
input grade
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.

grademathgrowth_yesmathgrowth_no
10.900.10
20.260.74
30.890.11
40.880.12

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)) 

Add title to the X-axis.

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)) ///
b1title("Grade in school")

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)) ///
b1title("Grade in school") ///
blabel(bar, color(white) position(inside) format(%4.2f))

Finally, I change bar colors to one of my favorites using the RGB color scheme, and I remove the bar outlines (which if not removed would by default appear in black).

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)) ///
b1title("Grade in school") ///
blabel(bar, color(white) position(inside) format(%4.2f)) ///
bar(1, fcolor("106 208 200") lwidth(none)) bar(2, fcolor("118 152 160") lwidth(none))