Statalist


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

Re: st: [Graphics] how to set the length of an axis?


From   Ulrich Kohler <kohler@wzb.eu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: [Graphics] how to set the length of an axis?
Date   Fri, 20 Mar 2009 13:43:08 +0100

You can construct boxplots with twoway as well:

sysuse auto, clear
by rep78, sort: egen pct25 = pctile(weight), p(25)
by rep78, sort: egen pct50 = pctile(weight), p(50)
by rep78, sort: egen pct75 = pctile(weight), p(75)
by rep78, sort: egen min = min(weight)
by rep78, sort: egen max = max(weight)
gen ufence = min(pct50 + 1.5*(pct75-pct25),max)
gen lfence = max(pct50 - 1.5*(pct75-pct25),min)

graph twoway ///
  || rbar pct25 pct75 rep78, barwidth(.5) ///
  || rspike ufence pct75 rep78 ///
  || rspike lfence pct25 rep78 ///
  || scatter pct50 rep78, ms(O) 

You will have to work on colors and friends.

Generally, producing graphs is much more a data management task. Once
you have your data ready, -graph twoway- is nice and simple.

Uli



Am Freitag, den 20.03.2009, 10:49 +0100 schrieb Antoine Terracol:
> Thanks Ulrich, excellent solution, much simpler than mine!
> 
> I wonder now if such a simple solution would also be available for the 
> upper part of the R graph, since -graph box- is not a twoway type and, 
> as I understand it, cannot be overlayed with another -twoway- graph...
> 
> Antoine
> 
> Ulrich Kohler wrote:
> > Antoine,
> > 
> > I got some error messages from your solution, but this one points you in
> > a different direction and it arguably simpler. It calculates the numbers
> > for the histogram by hand and uses -tw rbar- instead of -tw histogram-:
> > 
> > ------------------------------------------------------cut here---
> > clear
> > set scheme s2mono
> > webuse nlswork.dta, clear
> > g wage=exp(ln_wage)
> > su wage, de
> > drop if wage>r(p99)
> > egen t=group(year)
> > g ti=t+runiform()
> > g ti2=ti^2
> > g ti3=ti^3
> > qreg wage ti ti2 ti3
> > predict med
> > qreg wage ti ti2 ti3 , quantile(.95)
> > predict q95
> > qreg wage ti ti2 ti3 , quantile(.05)
> > predict q5
> > 
> > sum wage, meanonly
> > gen wageclass = autocode(wage,20,r(min),r(max))
> > by t wageclass, sort: gen wageperc = _N/r(N)
> > 
> > // rescale wageperc
> > sum wageperc, meanonly
> > replace wageperc = wageperc/r(max) * .80
> > 
> > gen lower = t
> > gen upper = t + wageperc
> > 
> > levelsof wageclass, local(K)
> > local width  = (`:word 2 of `K'' - `:word 1 of `K'')*.95
> > twoway                                         ///
> >   || rbar lower upper wageclass,               /// 
> >   color(gs12) barwidth(`width') horizontal     ///
> >   || line med q95 q5 ti, sort lcolor(black..)  
> > 
> > -----------------------------------------------------------------
> > 
> > 
> > Am Freitag, den 20.03.2009, 09:07 +0100 schrieb Antoine Terracol:
> >> Dear _all,
> >>
> >> I was asked if I could reproduced a graphic like 
> >> http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=109
> >> in Stata.
> >>
> >> Using nlswork.dta, I was able to produce the graph whose code is pasted 
> >> below.
> >>
> >> As you can see, the years are not evenly spaced on the x-axis. In 
> >> particular, the part between "1" and "2" is much smaller than the rest.
> >>
> >> I understand that, in fact, the total width of the sub graphs are all 
> >> equal, and that the visual difference is due to the presence of the y 
> >> axis and its legend in the first sub-graph.
> >>
> >> Is there a way to set the length of the axis in the first sub graph, or, 
> >> alternatively, to produce a graph with just the y-axis in such a way 
> >> that it would look nice?
> >>
> >> Best,
> >> Antoine
> >>
> >> /*--------------------------------------------*/
> >> clear
> >> set scheme s2mono
> >> webuse nlswork.dta, clear
> >> g wage=exp(ln_wage)
> >> su wage, de
> >> drop if wage>r(p99)
> >> egen t=group(year)
> >> g ti=t+runiform()
> >> g ti2=ti^2
> >> g ti3=ti^3
> >> qreg wage ti ti2 ti3
> >> predict med
> >> qreg wage ti ti2 ti3 , quantile(.95)
> >> predict q95
> >> qreg wage ti ti2 ti3 , quantile(.05)
> >> predict q5
> >>
> >> twoway (hist wage if t==1 , fcolor(gs14) plotr(m(zero)) 
> >> graphregion(margin(zero) color(white)) xlabel(0 "1") xtitle("") 
> >> ylabel(0(5)20, grid gmin gmax)  horiz) (line med q95 q5 ti if t==1, sort 
> >> xaxis(2) yaxis(2) ylabel(0(5)20, axis(2)) lcolor(black black black) 
> >> lwidth(thick thick thick) lpattern(solid solid solid) xscale(off 
> >> axis(2)) yscale(off axis(2)) plotr(m(zero))) , legend(off)  saving(a, 
> >> replace)
> >> local list "a.gph"
> >>
> >> local t=2
> >> while `t'<=15 {
> >>      twoway (hist wage if t==`t', fcolor(gs14) plotr(m(zero)) 
> >> graphregion(margin(zero) color(white)) xlabel(0 "`t'") xtitle("") 
> >> yscale(off)  xtitle("") ylabel(0(5)20 , grid gmin gmax)  horiz) (line 
> >> med q95 q5 ti if t==`t', sort xaxis(2) yaxis(2) ylabel(0(5)20, axis(2)) 
> >> lcolor(black black black) lwidth(thick thick thick) lpattern(solid solid 
> >> solid) xscale(off axis(2)) yscale(off axis(2)) ) , legend(off) 
> >> saving(a`t', replace)
> >> 	local list "`list' a`t'.gph"
> >> 	local t=`t'+1
> >> }
> >> graph combine `list', imargin(0 0 0 0) row(1) graphregion(color(white)) 
> >> note(t, pos(6))
> >> /*--------------------------------------------------*/
> >>
> >> *
> >> *   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/
> 
> 
-- 
kohler@wzb.eu
030 25491-361

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index