An aside note... Nick accidentally mis-specified the genders when tweaking the legend (and I didn't notice that until after sending my previous message). For correctness' sake, the code should read:
sysuse uslifeexp, clear
* range y variables and set buffer
summ le_male, meanonly
local min = r(min)
local max = r(max)
sum le_female, meanonly
local min = min(`min', r(min))
local max = max(`max', r(max))
local buff = (`max' - `min') * 0.04
local min = `min' - `buff'
local max = `max' + `buff'
* do plot
tw (scatteri `max' 1915 `max' 1921, recast(area) col(emidblue*.25)) ///
(line le_female year) (line le_male year) ///
(scatteri 80 1921 "WW-I & " 78 1921 "Spanish Flu" 80 1945 "Post WW-II", s(i)) ///
, legend(order(2 "females" 3 "males") ring(0) pos(4) col(1)) ///
xtitle("") ytitle("life expectancy (years)") yla(, ang(h)) ///
xli(1945, lcol(emidblue*.5)) plotr(m(zero)) ///
yscale(range(`min' `max')) xsca(ran(1898 2002))
-----------------------------------
Thomas J. Steichen
[email protected]
-----------------------------------
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Tuesday, November 11, 2008 1:58 PM
To: [email protected]
Subject: RE: st: Re: area between reference lines
Tom gave an elegant solution.
Here is a slightly different way of doing it: not really better, just
different.
You could use -scatteri-, giving the four corners of the rectangular
band, and then -recast(area)-.
That avoids the creation of a temporary variable.
The code below includes various extra cosmetic tweaks.
sysuse uslifeexp, clear
* range y variables and set buffer
summ le_male, meanonly
local min = r(min)
local max = r(max)
sum le_female, meanonly
local min = min(`min', r(min))
local max = max(`max', r(max))
local buff = (`max'-`min') * 0.04
local min = `min' - `buff'
local max = `max' + `buff'
* do plot
tw (scatteri `min' 1915 `max' 1915 `max' 1921 `min' 1921, recast(area)
col(emidblue*.25)) ///
(line le_female year) (line le_male year) ///
(scatteri 80 1921 "WW-I & " 78 1921 "Spanish Flu" 80 1945 "Post
WW-II", s(i)) ///
, legend(order(2 "males" 3 "females") ring(0) pos(4) col(1))
///
xtitle("") ytitle("life expectancy (years)") yla(, ang(h)) ///
xli(1945, lcol(emidblue*.5)) plotr(m(zero)) yscale(range(`min'
`max'))
Steichen, Thomas J.
One possibility is to creat the "buffer" yourself. Here I place a 4%
buffer on the y-axis using yscale(range()) and some pre-calculations (if
desired, one could do something similar for the x-axis). My preference
is for subtle shading (as shown) but many other preferences exist.
sysuse uslifeexp, clear
* range y variables and set buffer
qui summ le_male
local min = r(min)
local max = r(max)
qui sum le_female
local min = min(`min', r(min))
local max = max(`max', r(max))
local buff = (`max'-`min') * 0.04
local min = `min' - `buff'
local max = `max' + `buff'
* do plot
tempvar aux
g `aux'=`max'
tw (area `aux' year if year>=1915 & year<=1921, col(emidblue*.25)) ///
(line le_female year) (line le_male year) ///
(scatteri 80 1921 "WW-I & " 78 1921 "Spanish Flu" 80 1945 "Post
WW-II", s(i)) ///
, legend(order(2 3)) xli(1945, lcol(emidblue*.5)) plotr(m(zero))
yscale(range(`min' `max'))
Michael Hanson
Thanks, Tom: -plotregion(margin(zero))- is a useful option to know --
I would not have found it without your suggestion. However, in
testing it, I noticed that forcing the margins of all graphs to zero
(which it must do to preserve the y-axis range) can have undesirable
side effects. For example, if you replicate Martin's earlier example
with this option, the line graph will touch the x-axis in 1918.
There are circumstances where this behavior would be fine, but others
in which it would not -- and I prefer the default behavior of Stata
to "buffer away" the lower limit of the line graph from the axis in
this case.
Thus, I still find my "hack" of using a large range of dates within a
-tline- command to be preferable. I still would like to see
StataCorp modify -tline- to accept a date range rather than a list of
individual dates, as this option would make my "hack" a little
cleaner and easier to use.
On Nov 10, 2008, at 8:53 AM, Steichen, Thomas J. wrote:
> Try adding option -plotr(m(zero))- to your plot. This will remove
> the gap.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s), contains information that may be confidential, protected by the attorney-client or other legal privileges, and/or proprietary non-public information. If you are not an intended recipient of this message or an authorized assistant to an intended recipient, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution, or reproduction of this message and/or any of its attachments (if any) by unintended recipients is not authorized and may be unlawful.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/