Statalist


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

RE: st: Re: area between reference lines


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Re: area between reference lines
Date   Tue, 11 Nov 2008 18:58:25 -0000

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/



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