Statalist The Stata Listserver

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

RE: st: Box graph question

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Box graph question
Date   Wed, 1 Nov 2006 22:43:17 -0000

I've polished this up a little so that it is now 
a do file that takes arguments. 

Much of the point of this would be lost, on this 
occasion, if it were generalised into a program 
with its own help file and detailed syntax. 

Rather, what value it may have, it seems to me, 
is largely  in showing that you can come up with your own 
design for a different graph which can be implemented 
using -statsby- (on this occasion) and various 
parts of -twoway-. 

A public communication by Vince Wiggins at the 
2006 Boston users' meeting underlined the value
of -statsby- as a basis for graphing summary 

This script has lots of limitations (e.g. no 
support for -if- and -in-, stuff wired in
that you might not like, e.g. the colour red, 
etc., no -saving()- option). 

The aim is to give a script that anyone who 
likes can examine and hack around to taste
and circumstance 
without too many inhibitions that they are
not a Stata programmer, etc. 

// box-box plot: outer box range with mean bar 
//               inner box mean +/- k sd (k defaults to 1) 
// two or three arguments: y byvar [k] 
// examples: do boxbox mpg rep78 
//           ru boxbox mpg rep78 0.6745 
// NJC 1 Nov 2006 
// Rodrigo Alfaro: original suggestion 
// Maarten Buis, Scott Merryman, Vince Wiggins: comments

args y byvar k 
if "`byvar'" == "" error 198 
if "`k'" == "" local k = 1 
local Y `"`: variable label `y''"' 
if `"`Y'"' == "" local Y "`y'" 

tempvar mean max min sd meanpsd meanmsd  
qui statsby "su `y'" `mean'=r(mean) `max'=r(max) `min'=r(min) ///
	`sd'=r(sd), by(`byvar')
gen `meanpsd' = `mean' + `k' * `sd'
gen `meanmsd' = `mean' - `k' * `sd'
local stuff = cond(`k' == 1, "sd", "`k' sd") 

// the -scatter- does nothing distinctive, but is there to allow 
// optional easy edit to use -line- or -connected- instead 

twoway scatter `mean' `byvar' || ///
rbar `mean' `max' `byvar', bcolor(none) barw(0.2) || ///
rbar `mean' `min' `byvar', bcolor(none) barw(0.2) || ///
rbar `meanmsd' `meanpsd' `byvar', barw(0.1) bcolor(red) ///
legend(off) ytitle(`"`Y'"') yla(, ang(h)) ///
note("red bars: mean +/- `stuff'; empty bars: min, mean, max")


[email protected] 

<this, that and the other> 

Rodrigo A. Alfaro <[email protected]>
> I am not graphic at all. I cannot find a simple solution for 
> my problem.
> Probably the answer is simple, but I need help to find it. I 
> want to create
> a box graph with a line connecting the means (not the 
> medians), but the
> boxes have to cover the range (not the IQR)... also with 
> boxes covering
> k*std_deviation.

*   For searches and help try:

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