Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Controlling alignment of left AND right axes in combined graphs


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Controlling alignment of left AND right axes in combined graphs
Date   Mon, 19 Mar 2007 11:26:22 -0500

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Marcelo Villafani
> Sent: Sunday, March 18, 2007 9:08 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Controlling alignment of left AND right axes in combined
> graphs
> 
> I am plotting two separate graphs that have horizontal ylabels on the
> right
> and left axes, that later are combined in a col(1) orientation with the
> second plot providing xlabels for both of them. The problem is that the
> y-axes have different numbers of digits and I cannot align the ylabels.
> 
> I found that a similar question was posted last January regarding the left
> axis, but I couldn't adjust the suggested solution to the case where left
> and right y-axes are used simultaneously.
> 

How about this?


clear
set obs 100
gen x = uniform()
gen y1 = uniform()*10
gen y2 = uniform()*4000
gen y3 = uniform()*1000
gen y4 = uniform()*900000

//Upper left
qui sum y1
_natscale `=r(min)' `=r(max)' 5
local m1 = r(max)
local l1 = length("`m1'") 

//Upper right
qui sum y2
_natscale `=r(min)' `=r(max)' 5
local m2 = r(max)
local l2 = length("`m2'") 

//Lower left
qui sum y3
_natscale `=r(min)' `=r(max)' 5
local m3 = r(max)
local l3 = length("`m3'") 

//Lower right
qui sum y4
_natscale `=r(min)' `=r(max)' 5
local m4 = r(max)
local l4 = length("`m4'") 


local diff_left = abs((`l1'- `l3')*2 )
local diff_right = abs((`l2' - `l4')*2)
display "lengths: `l1' `l2' `l3' `l4' `diff_left' `diff_right' "

scatter y1 x, ylabel(,angle(h)) xlabel(none) xtitle("") /// 
	ytitle(, margin(0 `diff_left' 0 0)) /// 
	|| scatter y2 x, yaxis(2) ylabel(,angle(h) axis(2)) legend(off) /// 
	ytitle(, margin(`diff_right' 0 0 0) axis(2)) name(gr1, replace)
	


scatter y3 x, ylabel(,angle(h)) xlabel(none)  ytitle(, margin(0 0 0 0)) /// 
	|| scatter y4 x, yaxis(2) ylabel(,angle(h) axis(2)) /// 
	legend(order(1 "Y1 or Y3" 2 "Y2 or Y4"))  /// 
	ytitle(, margin(0  0  0 0) axis(2) ) name(gr2, replace)


graph combine gr1 gr2, col(1) name(gr_combine, replace)
graph drop gr1 gr2


Scott



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



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