[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: area between reference lines

From   Michael Hanson <>
Subject   Re: st: Re: area between reference lines
Date   Fri, 07 Nov 2008 18:09:34 -0500


While this approach can be useful, the example you give also shows its main disadvantage: the lines from the -xline- option span the full height of the chart, but the shaded -area- leaves very unsightly gaps between the shaded range and the border (axis) of the graph. In my opinion, this is not suitable for publication-quality graphics. Changing -area- to start/end at the border (axis) of the graphs (or adding an option to do so) would be, I think, a useful addition to Stata.


Kit Baum has written -nbercycles- to automate the plotting of time series against the recession intervals for the US determined by the NBER peak and trough dates. It uses -area- graphs as well, so it runs into the same problems as in Martin's example.

An alternative is to "force" -xline- (or -tline-) into plotting a range of lines. The code below isn't pretty -- and I welcome suggestions to improve it -- but it does work (for me, at least). This would be SO MUCH EASIER if -tline- could accept a range to shade, rather than just individual dates -- such as -tline (1960m4/1961m12 1969m12/1970m11)-, etc. Here's hoping StataCorp will consider this request for a future update....

// Example time series plot with recession shading
// Assumes monthly data from 1960 through 2008
// Recession peaks & troughs from <>
// Must have -freduse- installed: -ssc install freduse-

// Load example data
freduse UNRATE, clear	// Will overwrite any existing data -- save first!
drop date
gen date = mofd(daten)
format date %tm
tsset date

// Define NBER recession dates for shading
// Note: Your mail program may break or "wrap" these lines
// Note: Must place each -local- command on a single line
local rec1 "1960m4 1960m5 1960m6 1960m7 1960m8 1960m9 1960m10 1960m11 1960m12 1961m1 1961m2" local rec2 "1969m12 1970m1 1970m2 1970m3 1970m4 1970m5 1970m6 1970m7 1970m8 1970m9 1970m10 1970m11" local rec3 "1973m11 1973m12 1974m1 1974m2 1974m3 1974m4 1974m5 1974m6 1974m7 1974m8 1974m9 1974m10 1974m11 1974m12 1975m1 1975m2 1975m3"
local rec4 "1980m1 1980m2 1980m3 1980m4 1980m5 1980m6"
local rec5 "1981m7 1981m8 1981m9 1981m10 1981m11 1981m12 1982m1 1982m2 1982m3 1982m4 1982m5 1982m6 1982m7 1982m8 1982m9 1982m10 1982m11" local rec6 "1990m7 1990m8 1990m9 1990m10 1990m11 1990m12 1991m1 1991m2 1991m3" local rec7 "2001m3 2001m4 2001m5 2001m6 2001m7 2001m8 2001m9 2001m10 2001m11"

// Plot series with recession shading
tsline UNRATE if tin(1960m1,2008m12), ylabel(, angle(h)) ttitle("") ///
    tlabel(1960m1(60)2008m12, format(%tmcY) labsize(*0.9)) ///
    tline(`rec1' `rec2' `rec3' `rec4' `rec5' `rec6' `rec7', lc(gs12))

// end of example

A final note: depending on what you do with your graph (i.e. what format you convert it to for publication), you may notice some "banding" of the vertical lines. Changing the line width of some of the *interior* dates of the -tline()- command can address this concern. (Best not to change lines for the peak/trough dates, or they may spill over into adjacent dates.) I haven't noticed this problem when I go from Stata graphs -> PDF -> LaTeX on my Mac -- but some students working with certain combinations of Microsoft OS's, office suites, and file formats (WMF, EMF, etc.) have seen this, so YMMV.

Hope this is helpful,

On Nov 5, 2008, at 6:24 PM, Martin Weiss wrote:

Line for the server...

sysuse uslifeexp, clear

*plain vanilla
tw (line le year, xli(1970 1980)) (area le year if year>=1970 & year<=1980), name(first, replace) legend(off)

*or quick and dirty tricks if you want color up the upper edge of the graph
tempvar aux
g `aux'=80
tw (line le year, xli(1970 1980)) (area `aux' year if year>=1970 & year<=1980), name(second, replace) legend(off)

*let`s see both
gr combine first second

----- Original Message ----- From: "Robert Duval" <>
To: <>
Sent: Thursday, November 06, 2008 12:07 AM
Subject: st: area between reference lines

Dear all,

Does anyone knows how to fill the background area between two
(vertical) reference lines in a twoway line graph?

The idea is to shade the background of a specific (vertical) region of the graph to indicate that during those years (x axis) the economy was
in a recession.

Any help will be greatly appreciated

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index