Statalist The Stata Listserver


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

st: RE: Stacked Histogram


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Stacked Histogram
Date   Tue, 24 Oct 2006 17:44:51 +0100

The generic problem I think of here as "the way ahead is -bar-red", 
meaning that -graph bar- or -histogram- doesn't do it, so what is 
to be done? The answer is (usually) to make your own with -twoway bar-
as a basis. 

Here is an example: 

sysuse auto, clear 

twoway__histogram_gen mpg if foreign, gen(freq_foreign x) freq start(0) width(2)

twoway__histogram_gen mpg if !foreign, gen(freq_domestic x2) freq start(0) width(2)

qui gen freq = freq_domestic + freq_foreign 

twoway bar freq_domestic x if freq < ., barw(2) bcolor(blue) xsc(r(10 .)) || ///
rbar freq_domestic freq x2 if freq < ., barw(2) bcolor(red) ///
legend(order(1 "Domestic" 2 "Foreign") pos(11) col(1) ring(0)) ///
xtitle("`: var label mpg'") ytitle(Frequency) xla(10(5)40) yla(, ang(h)) ///
xsc(r(10 .)) 

The recipe here is 

1. Use -twoway__histogram_gen- to generate the raw frequencies (densities, 
percents). On this undocumented command, count the underscores carefully
(there are three, not two), see its help and also 

SJ-5-2  gr0014  . . . . . . . Stata tip 20: Generating histogram bin variables
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. A. Harrison
        Q2/05   SJ 5(2):280--281                                 (no commands)
        tip illustrating the use of twoway__histogram_gen for
        creation of complex histograms and other graphs or tables

itself reprinted in "33 Tips" (order one for everybody in your group as 
a seasonal present). 

2. We are going to stack frequencies, so we would need to generate every 
layer in the stack but the first as the sum of all the previous. 

3. We start with a -twoway bar- of the first category and then add others 
using -twoway rbar-. 

4. All else is cosmetics. 

I'd be interested to hear Marcello's defence of this as compared with 
alternatives. My prejudice is that it could be fairly successful with 
a very small number of categories (two or three) but otherwise could 
just look a mess. 

Nick 
[email protected] 

> -tabplot- and -tableplot- from SSC will do 
> something similar, arguably better. 
> 
> But in general the recipe is: work your 
> way towards a -twoway bar-. 
> 
> I'll post some example code later. 
 
Marcello Pagano
  
> > Has anyone got code for allowing a second, categorical 
> > variable into the 
> > histogram?  What I am thinking of is the freq variable is 
> the primary 
> > variable going into the histogram, but there might be a 
> > second variable, 
> > age, say, categorised as `young' and `old'. Then think of 
> each bar in 
> > the histogram displayed as a two colour bar, black and white, 
> > say, where 
> > the black part represents the proportion of that bar who are 
> > young, and 
> > the white represents the proportion of that bar who are old. 
> > A sort of 
> > stacked-bar-meet-histogram kind of plot.
> 

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