May 3, 2012

Put a Little Shade in your Graphs

Suppose you graph a time series and you want to highlight a certain range (for example, U.S. real GDP series and you want to color the area between 1929 to 1939 to indicate the years under the Great Depression):

line rgdp year

Well, a simple way to do that is to add another variable and use the twoway graph command "AREA." To start with, your new variable has to be a constant with value equal to the highest data point in your y-axis. So for example, in your graph of U.S. real GDP, the highest value given is 3,500 billions of constant 2005 $. So:

gen new=3500

Now, your new graph command should be:

twoway (area new year if year>=1929 & year<=1939) (line rgdp year)

You have to start with the AREA-type graph first because the first graph type will always be superimposed by the next graph type.