Stacked graphs in Stata

For plotting the relative importance of (e.g.) input shares, it can be useful to stack them in a figure. In my specific problem, I wanted to show the development of low-, medium-, and high-skilled labour inputs, relative to all labour inputs over time. Since the three shares obviously sum up to 1, I wanted to have a figure like this:

In order to get a figures like this, follow these steps:
1) generate three variables (here: ls, ms, hs) which sum up to one. You need to sum up the shares so that: ls=ls; ms=ls+ms; hs=ls+ms+hs=1

2) Use the following code in Stata

twoway (area hs year, color(gs13)) (area ms year, color(gs9)) (area ls year, color(gs4)), xlabel(1970 (5) 2005, alternate) ylabel(0 (25) 100, alternate nogrid val) xtitle("") text(10 1980 "{bf:low-skilled}") text(50 1990 "{bf:medium-skilled}") text(90 1995 "{bf:high-skilled}") by(industry, legend(off))

The stacked graph is essentially an overlay of three separate layers of the figure (one for each input). Worth mentioning is the following command: text(10 1980  "{bf:low-skilled}") which puts text into the figure.

7 thoughts on “Stacked graphs in Stata

  1. This doesn’t quite work the way it is described. If I have three variables that sum to 1 (like market shares .6,.3,.1), then all the shares are graphed from the zero, rather than being stacked on top of each other.

  2. Yes they are sorted in that way. What I have to do is create new variables in this way

    gen x1 = share1
    gen x2 = share2+x1
    gen x3 = share3+x3

    Then I can create the area graphs using the temporary variables x1,x2, and x3. It will look as it does in your example. Without this intermediate step, they just overlap on each other rather than stacking. If you have found a way to get them to stack without this intermediate step, I would love to hear how you do it.

  3. I think using area three times does not work because the three areas are overlaid. One can use area for the first, bottom area, but the others should be (rarea x1 x2 year) and (rarea x2 x3 year).

  4. HHi, it works all good for me (when listing the variables from the smallest to the biggest) but I have another question: how to display blank space when data is missing? cmiss(n) seems not to work with area charts…Thanks a lot!

Leave a Reply