Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: graphing and shading

From (Derek Wagner, Stata Corp)
Subject   st: Re: graphing and shading
Date   Fri, 06 Jun 2003 12:18:24 -0500

> Hi everyone,
> I've got a quation regarding the new graphing facilities in STATA8.
> Lets say I've got some GDP data over time. Stata provides a means whereby
I can
> demarcate a year using xline. Thus, I could show the great depression as
> area between two vertical lines:
> - twoway connected gdp time, xline(1929 1945)
> But lets say that I'd like to shade the area between two years (useful if
> want to have a bunch of intervals indicating time of recession. Is there a
> to do this in STATA?
> Thanks.
> --John

> John,

> A similar question came up last month.  Gerald Wright (
> came up with this example to show how it may be done:

> preserve
> sysuse gnp96,clear
> sum gnp
> local max=`r(max)'
> gen c=`max' if date>q(1992q1)&date<q(2001q1)
> lab var c "Clinton Years"
> twoway (area c date, bcolor(gs15)) (line gnp date, clpattern(solid))
> restore

> Hope this helps,
> Scott

Another way (and somewhat easier way) to produce the same graph as the above
is to use the -scatteri- command with the -recast- option.  The -scatteri- command
allows you to plot points manually in the graph region, and the -recast- option allows 
you to recast the points into an area plot.  By finding the maximum value for the 
y-axis, you could plot one point at the beginning of the date range in need and one 
at the end.  Recasting to an -area- plot will automatically include the area beneath 
the plotted points.  For example, using the same data as above:    

sysuse gnp96,clear 

twoway (scatteri 10000 `=q(1992q1)' 10000 `=q(2001q1)', bcolor(gs15) recast(area) ///
legend(on order(1 "Clinton Years" 2 "Real GNP"))) ///
(line gnp date, clpattern(solid)), xlabel(, format(%tq))

As you can see, you can also avoid creating macros by using Stata's in-line evaluation
feature.  The expressions `=q(1992q1)'  and `=q(2001q1)' get evaluated to 128 and 164, 
respectively.  So, I've told Stata to plot points at (128,10000) and (164,10000).
Then, the -recast(area)- option shaded the region below the two points with a bar color of gs15.
The only thing left to do after overlaying the -line- graph is to reformat the time series labels for
the x-axis using the -xlabel- option.

Hope this helps.

Derek Wagner
*   For searches and help try:

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