Statalist The Stata Listserver

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

Re: st: graph bar labels with number of observations

From   Ulrich Kohler <>
Subject   Re: st: graph bar labels with number of observations
Date   Tue, 20 Feb 2007 09:46:02 +0100 wrote:
> I have the following graph bar code for three categorical variables, var1,
> var2 and var3.
> graph bar var1, over(var2) over(var3) title("Mean of var1 by var2 and
> var3")
> What I'd like to add is the number of observations that are included in the
> each bar.  I can manually do this with this code:
> sort var2 var3;
> by var2 var3: summ var1;
> The output will give me summary statistics for 9 groups of var1:  all the
> combinations of the three categorical variables: when 0, 1 and missing.
> But, how can I incorporate this information in the bar chart above?
> I tried to extract the information manually and go through the blabel
> option, but I was unsuccessful.  Blabel option can only do a few things:
> use bar height or names of yvars.
> In a histogram, this is easily accomplished: I would use these two options:
>  frequency and addlabel.  But, how to do it with a graph bar..?

I usually use -twoway bar- if I need to fine tune a bar chart. -twoway bar-
allows you the full range of options of twoway graphs, including overlaying
other plot-types. Here is an example which comes close to what you want, and
which can be easily expandet:

sysuse auto,clear
collapse (mean) mpg (count) n=mpg, by(for rep78)
egen axis = axis(for rep78), gap label(rep78)
graph twoway ///
   || bar mpg axis  ///
   || scatter mpg axis, ms(i) mlab(n) mlabpos(12) mlabcolor(black) ///
   || , legend(off) xlab(1(1)5 7(1)9)

Note: You need to download Nick Cox' -egenmore- package to make this example

  ssc install egenmore

Hope this help

Ulrich Kohler
*   For searches and help try:

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