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

st: RE: Stacked bar chart of categorical data

From   "Nick Cox" <>
To   <>
Subject   st: RE: Stacked bar chart of categorical data
Date   Thu, 27 May 2004 10:48:31 +0100

-catplot- is a user-written command. 

I just looked at the help and found this explanation: 

'The default display with -bar- and -hbar- is graphically 
conservative, reflecting the view  that height of bars 
and text indicating categories are the best ways of conveying
information. If you wish also to have bars in different 
colours, specify the option -asyvars-, which differentiates 
the categories of the first named variable.... If you
wish also to stack bars of different colours, specify the 
further option -stack-.' 

So I surmise that you are not specifying -asyvars- (read 
"as [separate] y var[iables]s". 

The logic here, which perhaps should be spelled out, 
is not that of the author of -catplot-. 
Stata quite rightly won't stack bars of the same colour, 
as they would then be indistinguishable within the 
stacked bar. 

With the auto data, an analogue of your simpler problem
might be 

. catplot hbar rep78 foreign, percent(foreign) asyvars stack 


. catplot bar rep78 foreign, percent(foreign) asyvars stack 


. catplot bar rep78, by(foreign) percent(foreign) asyvars stack

Your more complicated problem would seem to require either 
juxtaposing three separate bar charts, or a -reshape- from 
four variables to three: 

I assume you have some identifier variable, say -id-. 

reshape long assessment, i(id) j(type) 

after which you can do something like 

catplot bar assessment outcome, by(type, row(1)) 
	percent(type outcome) asyvars stack 


catplot bar outcome assessment , by(type, row(1)) 
	percent(type assess) asyvars stack 

Be warned: -catplot- isn't smart enough to do 
the right thing with 

catplot bar outcome assessment , by(type, row(1) total) 
	percent(type assess) asyvars stack 

I only just noticed this. That is, within -catplot-, 

1. Percents are constructed to be what 
-percent()- says they are. 

2. Nothing special is done about -by()-. 
It is just passed along to -graph-. 

3. To do the right thing given a "total" 
within -by()- would require peeking inside
-by()- and doing a work-around. That sounds 
too much like hard work to me, so I guess
that it is not likely to happen w.r.t. -catplot-. 

Having said all that, you could just construct
your own percent variables and then use -graph bar- 
or -graph hbar- directly. That way, no limitations
of -catplot- will bite. 


Fiona Bruinsma
> I have a number of categorical variables that I want to 
> present on a stacked
> bar chart (for each of the bars the height of the bar will be 100%).
> My categorical variable (from 1-4) is the number of times an 
> individual has
> had a certain type of assessment by the outcome of interest 
> (0=no, 1=yes).
> I have included an example of the tabulated command I used to 
> generate the
> data I am trying to present graphically
> tab2 DSM_mdep as_ht if self_ref==1, row mi
> Ideally, I would then like to present on the same graph a number of
> different types of assessment by the outcome (assessment1 by outcome,
> assessment2 by outcome, assessment3 by outcome).
> Catplot has been of some assistance but I haven't yet been 
> able to get it to stack the bars.

*   For searches and help try:

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