Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Combined Horizontal Box Blots do not line up


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: Combined Horizontal Box Blots do not line up
Date   Fri, 16 Sep 2005 15:31:14 +0200

The following code is a step in the door, but I wonder whether this is 
worthwile. If the categorical variables differ in their number of categories 
(i.e. levels), the -graph, by()- will lead either to unequal box-width or to 
unequal filled plotregions. I think it would be better to have the panels for 
each categorical covariate with a size proportional to the number of 
categories. 

With -graph combine- we might apply -fysize()- to correct for this but I 
cannot see how to automate this.

------------------------------------
program define hboxcomp
version 9
syntax varlist, between(varlist)
preserve

// Generate Covariates for each dimension
local i 1
local j 1
foreach var of local between {
	gen cov`j':cov = .
	local dimlab `"`dimlab' `j' `"`: var lab `var''"'"'
	levels `var', local(K)
	foreach k of local K {
		replace cov`j' = `i' if `var' == `k'
		label define cov `i++' `"`: label (`var') `k''"', modify
	}
	local j = `j' + 1
}

// Reshape to long
gen index = _n
keep `varlist' cov*  index
reshape long cov, i(index) j(dim)
label define dim `dimlab'
label val dim dim

// Graph
graph box `varlist', over(cov) by(dim, cols(1)) nofill horizontal

end


n j cox wrote:
> That works nicely. A refinement is to add
>
> label def dim 1 "Repair record" 2 "Origin"
> label val dim
>
> before the -graph box- call to avoid the
> detail of "1" and "2".
>
> One question now is there is a useful and interesting
> generalisation that lends itself to automation? That
> is, assume
>
> 1. There is a response variable.
>
> 2. There are two or more categorical covariates.
>
> 3. For each covariate, we want the distribution
> of the response by its categories.
>
> 4. We want the individual displays aligned.
>
> Uses could vary from exploration all the way
> to final report.
>
> Nick
> [email protected]
>
>  >>> Ulrich Kohler
>
> Thanks Vince, this answered my question thouroughly. I went for the
> -reshape- solution, which is far as complicated as I thought.
>
> To round this tread up, here is a template for the auto-data:
>
> . gen cov1:cov = rep78
> . gen cov2:cov = 6 + foreign
> . label define cov 6 "Domestic" 7 "Foreign"
> . reshape long cov, i(make) j(dim)
> . graph box turn, over(cov) by(dim, cols(1)) nofill horizontal
>
> Reshape rules!
>
> *
> *   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/

-- 
[email protected]
+49 (030) 25491-361
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index