 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: Box Graph Question

 From Joe Canner To "statalist@hsphsun2.harvard.edu" Subject st: RE: Box Graph Question Date Tue, 8 Oct 2013 20:55:58 +0000

```John,

It is possible, although a bit messy (at least the way I know how to do it).  It may also take some tweaking to apply it to your situation.

First the code:
*****
sysuse auto, clear

statsby "su mpg" mean=r(mean) max=r(max) min=r(min) sd=r(sd), by(rep78)

gen meanpsd = mean + sd
gen meanmsd = mean - sd

sort mean
gen meansort=_n

forvalues j=1/`=_N' {
local thislab=rep78[`j']
local lab `lab' `j' "`thislab'"
}

label define meanlabel `lab'
label value meansort meanlabel

twoway scatter mean meansort || rbar mean max meansort, bcolor(none) barw(0.2) || rbar mean min meansort, bcolor(none) barw(0.2) || rbar meanmsd meanpsd meansort , barw(0.1) bcolor(red) legend(off) ytitle(Miles per gallon) yla(, ang(h)) xlabel(1/`=_N', valuelabel)

*****
What this is doing is creating a variable (meansort) which is the order of the points sorted by mean.  When you replace all instances of rep78 with meansort you will get the graph sorted by mean.  But, your x-axis labels are wrong now, so you have to label them with the original values of rep78.

Regards,
Joe Canner
Johns Hopkins University School of Medicine

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of John Marvel
Sent: Tuesday, October 08, 2013 4:15 PM
To: statalist@hsphsun2.harvard.edu
Cc: John Marvel
Subject: st: Box Graph Question

My question refers to a statalist posting under the same subject line
as above ("Box Graph Question") from November 1, 2006.  I'm running
Stata 13 SE.

The code below (from Nick Cox) produces a very useful graph of
descriptive statistics.

My question is whether the elements of the graph (the bars) can be
ordered by mean.  In other words, if I wanted the bars that the code
produces to run from the lowest mean to the highest mean, how would I
do that (if possible?).

Thanks very much for any help,

John Marvel

****************************************************
sysuse auto, clear

statsby "su mpg" mean=r(mean) max=r(max) min=r(min) sd=r(sd), by(rep78)

gen meanpsd = mean + sd
gen meanmsd = mean - sd

twoway scatter mean rep78 || ///
rbar mean max rep78, bcolor(none) barw(0.2) || ///
rbar mean min rep78, bcolor(none) barw(0.2) || ///
rbar meanmsd meanpsd rep78 , barw(0.1) bcolor(red) ///
legend(off) ytitle(Miles per gallon) yla(, ang(h)) ///
note(red bars: mean +/- sd; empty bars show range)
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```