Statalist


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

Re: st: AW: RE: axis(1) and axis(2) scales in graphs


From   Leandro Brufman <[email protected]>
To   [email protected]
Subject   Re: st: AW: RE: axis(1) and axis(2) scales in graphs
Date   Tue, 26 Jan 2010 14:40:47 -0500

Thank you very much for your answers. I was thinking on one possible
solution to this problem. Making both sides symmetric. For example, if
the right hand axis ranges from -6 to 6 and the left hand axis ranges
from -25 to 25, then in both cases zero should be at the middle,
right? (see example)
****** begin example *************
clear*
set seed 123456
set obs 100
gen time=_n
gen serie1=rnormal()
gen serie2=rnormal(1,2)
tw ///
 || line serie1 time, yaxis(1) ///
 || line serie2 time, yaxis(2) ///
 || , yscale(range(-6 6) axis(1)) ///
      yscale(range(-25 25) axis(2)) ///
      yline(0)
****** end example *************

Well, the thing is that I am running the same graph for different
countries, so I would like to have different ranges for each country.
So I tried summarizing serie1 and serie2 for each country and saving
it into a local macro, so that I can latter put it on range. But for
some reason I had to use global macros (local macros disappeared after
the loop), and when trying to implement this code it doesn't work
properly (that is, I do not get the same results as in the previous
example.

Here I paste the code. crisis1 is just a d
********************beginning of code
*********************************************************
**** First loop creates the macros that are gonna be used in - range()
suboptions -
gen scountrystandard=subinstr(country," ","",.)
levelsof country, local(lcountry)
foreach x of local lcountry {
	levelsof scountrystandard if country==`"`x'"', local(lcountrystandard)
	foreach y of local lcountrystandard {
		levelsof date if crisis1==1 & country==`"`x'"'
			qui sum gdp_growth_quarter if country==`"`x'"'
			global lgdpmax`y' = r(max)
			global lgdpmin`y' = -r(max)
			qui sum diff_exchangerate if country==`"`x'"'
			global ldiffmax`y' = r(max)
			global ldiffmin`y' = -r(max)
	}
}
**** Now I graph the stuff.
levelsof country, local(lcountry)
foreach x of local lcountry {
	levelsof scountrystandard if country==`"`x'"', local(lcountrystandard)
		foreach y of local lcountrystandard {
			levelsof date if crisis1==1 & country==`"`x'"'
			#delimit ;
			graph twoway
				(bar gdp_growth_annual date, yaxis(1) ysca(axis(1)
r(${=lgdpmax`y'} ${=lgdpmin`y'}))     )
				(line gdp_growth_quarter date, yaxis(1) ysca(axis(1)
r(${=lgdpmax`y'} ${=lgdpmin`y'}))   )
				(line  diff2_exchangerate date, yaxis(2) ysca(axis(2)
r(${=ldiffmax`y'} ${=ldiffmin`y'}))  )
				(line  diff_exchangerate date, yaxis(2) ysca(axis(2)
r(${=ldiffmax`y'} ${=ldiffmin`y'}))   )
			if countryname==`"`x'"',
				title(`x')
				yline(0, axis(1) lstyle(foreground))
				xline(`r(levels)', lwidth(vthin) lcolor(gray) lpattern(shortdash))
			saving(Graphs-test\crisis1`y', replace)
			;
			#delimit cr
	       }	
}
***************** end of code ***********************************
Although you don't have the data, maybe somebody can give me a hint of
why Stata seems to be ignoring the -range suboptions-

Many thanks in advance!
Leandro


On Tue, Jan 26, 2010 at 8:32 AM, Martin Weiss <[email protected]> wrote:
>
> <>
>
> Your code does achieve the immediate goal of aligning the zeroes, but at a higher variance in one of the series, such as below, the left axis can easily look ridiculous.
>
>
>
> *************
> clear*
> set seed 123456
> set obs 100
> gen time=_n
> gen serie1=rnormal()
> gen serie2=rnormal(3,5)
>
> reshape long serie, i(time) j(nr)
>
> sum serie
>
> tw ///
>  || line serie time if nr==1, yaxis(1) ///
>  || line serie time if nr==2, yaxis(2) ///
>  || , yscale(range(`=r(min)-1' `=r(max)+1') axis(1)) ///
>      yscale(range(`=r(min)-1' `=r(max)+1') axis(2)) ///
>      yline(0)
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:[email protected]] Im Auftrag von Ulrich Kohler
> Gesendet: Dienstag, 26. Januar 2010 14:24
> An: [email protected]
> Betreff: Re: st: AW: RE: axis(1) and axis(2) scales in graphs
>
> Am Dienstag, den 26.01.2010, 13:47 +0100 schrieb Martin Weiss:
>> <>
>>
>> " I am not completely clear what the problem is"
>>
>>
>>
>> Actually, I found the problem exceptionally clear in this case:
>>
>>
>> *************
>> clear*
>> set seed 123456
>> set obs 100
>> gen time=_n
>> gen serieA=rnormal()
>> gen serieB=rnormal(1,2)
>> tw (line serieA time, yaxis(1) yli(0)) (line serieB time, yaxis(2))
>> *************
>>
>> I just did not know how to align the zero point on the left and right axis,
>> to be honest...
>
>
> Perhaps something along this lines
>
> -------------------------------------------
>
> clear*
> set seed 123456
> set obs 100
> gen time=_n
> gen serie1=rnormal()
> gen serie2=rnormal(1,2)
>
> reshape long serie, i(time) j(nr)
>
> sum serie
>
> tw ///
>  || line serie time if nr==1, yaxis(1) ///
>  || line serie time if nr==2, yaxis(2) ///
>  || , yscale(range(`=r(min)-1' `=r(max)+1') axis(1)) ///
>      yscale(range(`=r(min)-1' `=r(max)+1') axis(2)) ///
>      yline(0)
> -------------------------------------------------
>
> Uli
>
>
> *
> *   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/
>
>
> *
> *   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/
>

*
*   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