For those who don't know, Stata is a statistical programming language used widely by Economists and something I have become pretty good at. However, Stata is not the most flexible tool for graphics. Specifically, there are not options to shade regions of a graph effectively, so I took on the challenge along with a friend of mine to work around this.
On its surface, bgshade is a command that takes a dummy variable in a Stata dataset and adds shading to the areas where that dummy variable = 1. We extended this to include the following features: pre-coded NBER recession shading; horizontal or vertical shaded regions; separate styling for more than one shaded region; and adding shaded regions and their labels to the legend if asked.
An example is included directly below that shows US gross national product between 1967 and 2002 as the blue line. Shaded regions show periods when the US is at war and NBER recessions.
sysuse gnp96, clear gen wars = 0 replace wars = 1 if date <= yq(1978,4) // korean war replace wars = 1 if date >= yq(1990,1) & date <= yq(1991,4) // gulf war replace wars = 1 if date >= yq(2001,1) // Afghanistan lab var wars "US War Periods" bgshade date if date < yq(2002,4), shaders(wars quarter) legend /// twoway(line gnp date if date < yq(2002,4), title("US GNP (1967-2002)") lcolor(navy) /// legend(cols(1) order(3 1 2 "NBER US Recessions")))
Future plans for the command include adding an over() option for shaders, so for example, in the above example if one wanted to shade each war period separately and label them separately in the legend they could accomplish this by specifying "over(wars)". Also, we will add an option to only change one of the shaders (sstyle(lwidth( # # # 2)) changes the fourth shader's lwidth to 2) and further allow to edit just one of the shader's region's lwidth. (If this doesn't make any sense, that's fine, it's mainly for my own reference)
If you would like to check out the ado and help files in all of their glory, downloads are located
here.