Ernest Berkhout <[email protected]> asks about getting two different
colored -rarea- plots from two crossing curves:
> The idea seems simple, the implementation that I tried so far appears to be
> not... Therefor I firstly report what I have managed to tackle myself, but
> finally there are still some open ends. Anyone out there who might have a clue?
>
> The problem is as follows: imagine a twoway (overlayed) graph with two
> lines that cross eachother (maybe more than once), for example Y = sin(x)
> and Y2 = 0.2.
> Now I want to shade the closed areas between these two lines. If possible,
> I also want different colors for the two possibilities Y>Y2 and Y<Y2. My
> first approach was to copy minimum and maximum values and then use the
> -twoway rarea- approach.
>
> ***********************************
> The example can be reproduced with:
>
> set obs 100
> gen x = _n/10
> gen y = sin(x)
> gen y2=.2
>
> *** create low/high-series for the areas where Y>Y2:
> gen low1 = y2 if y > y2
> gen high1 = y if y > y2
>
> *** create low/high-series for the areas where Y<Y2:
> gen low2 = y if y < y2
> gen high2 = y2 if y < y2
>
> twoway (line y x) (line y2 x ) (rarea low1 high1 x ) (rarea low2 high2 x )
> ********************************
>
> This yields two problems:
> 1. The shaded area of this graph does not extend until the exact
> points of intersection, especially when there are relatively few
> datapoints. The 'edges' of the area are left blank.
> 2. Follows from problem 1: although the variables low1 and high1 are set to
> missing when the values of Y are below Y2 (low1>high1), there is still an
> area to plot for Stata, namely from the last non-missing observations to
> the first non-missing observations from the next area where Y>Y2 again.
>
> I managed to work around most of the second problem, by setting low1 &
> high1 equal to either Y or Y2 instead of missing. Then the code reads:
>
> gen low1 = y2 if y > y2
> gen high1 = y if y > y2
> replace low1 = y2 if low1==.
> replace high1= y2 if high1==.
>
> But when datapoints aren't many (or when functions aren't that smooth as in
> my example), problem 1 still leaves behind its marks. What is really needed
> is a way to adress the graph to fill the area up untill the intersection
> points (and not any further).
> Does anyone have a clue? Is it possible to adress the intersection in a
> continuous way, or does one manually has to compute these points and add
> them (temporarily) to the data? Any opinions are welcome!
Ernest almost has it. The solution is actually a little simpler:
***** BEGIN:
set obs 100
gen x = _n/10
gen y = sin(x)
gen y2=.2
gen low = min(y,y2)
gen high = max(y,y2)
twoway (rarea low y2 x ) (rarea high y2 x )
***** END:
Ernest could then control the individual line colors by specifying options to
each -rarea- plot.
--Jeff
[email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/