[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: How to label bars with frequency AND percentage for categorical variables?

From   Adam Collins <>
To   "''" <>
Subject   RE: st: How to label bars with frequency AND percentage for categorical variables?
Date   Wed, 25 Nov 2009 10:59:35 +0100

Scott, Nick - thank you both so much!

Scott: your solution works well for categorical variables with category values close together, however it ran into problems with variables that have "gaps" between the category values - for example, my data set contains standard codes for "other" and "don't know" (96 and 98 respectively).  

Nick: The -mybar- solution copes fine with all the category values, and moving the text differently for different numbers of category (including the proportional `nudge' for fewer than 12 categories) makes it very robust.  I especially like the fact the value labels and variable title are already displayed.  Once -mybar- is "installed", the simplicity of use is quite beautiful!  

Thanks again!  This is really helpful!!


-----Original Message-----
From: [] On Behalf Of Nick Cox
Sent: mercredi 25 novembre 2009 09:47
Subject: RE: st: How to label bars with frequency AND percentage for categorical variables?

I'll add my solution without much detailed comment. Independently of this I wrote a program using very similar logic. 

I agree with Scott's tacit position: for this, you need to retreat from -graph bar- or -graph hbar- and work out your own solution using -twoway bar-, if only because -graph *bar- does not seem to offer scope for customised bar labels. (By extension, neither does -catplot-. -catplot- is from SSC. Please remember to explain where user-written programs you refer to come from.) 

Much of the complication in the code arises because if the number of categories is even moderate, you run out of space to put the frequency atop the percent. 

program mybar
	version 9 
	syntax varname [if] [in] [, baropts(str asis) sc1opts(str asis) *] 

	quietly { 
		marksample touse, strok  
		count if `touse'
		if r(N) == 0 error 2000 

		tempvar fr pc text y y1 y2 

		contract `varlist' if `touse', freq(`fr') percent(`pc') 
		egen `y' = group(`varlist'), label 
		su `fr', meanonly 
		local xmax = 1.15 * r(max) 

		if _N > 12 { 
			gen `text' = string(`fr') + " (" ///
			+ string(`pc', "%2.1f") + "%)" 
			local textcall ///
		scatter `y' `fr', ms(none) mla(`text') ///
	        mlabc(green) yti("")  
		else { 
			local nudge = 0.02 * _N 
			gen `y1' = `y' - `nudge'
			gen `y2' = `y' + `nudge'
			gen `text' = "(" + string(`pc', "%2.1f") + "%)" 
			local textcall ///
		scatter `y1' `fr', ms(none) mla(`fr') mlabc(green) `sc1opts' ///
		|| scatter `y2' `fr', ms(none) mla(`text') mlabc(green) 

		levelsof `y', local(Y) 
		local header `"`: var label `varlist''"' 
		if `"`header'"' == "" local header "`varlist'" 
	twoway bar `fr' `y', horizontal barw(0.6) base(0) `baropts' ///  
	|| `textcall'  ///
	yla(`Y', valuelabels noticks ang(h)) ysc(reverse) ///
	xti("") xla(none) xsc(r(0 `xmax')) ///
	legend(off) subtitle(`"`header'"') `options'

Scott Merryman

Something like this?

sysuse auto, clear
xtile cat_mpg = mpg, nq(4)

foreach var of varlist  rep  cat {
	qui {
	count if `var' != .
	local total =r(N)
	egen count = count(`var') if `var' !=., by(`var')
	gen percent = string(round((count/`total')*100,.1)) + "%)"
	replace percent = "("+percent
	gen count2= count+.5
	gen rep1 = `var' - .2
	gen rep2 = `var' + .1
	twoway bar count `var', barw(0.7)    || ///
		scatter count2 rep1, mlabel(count) mlabpos(0)  ///
		ms(none) mlabcolor(black) || ///
		scatter count2 rep2, mlabel(percent) mlabpos(0) ///
		ms(none) legend(off)  mlabcolor(black) ///
drop count* percent rep?

See also:

On Tue, Nov 24, 2009 at 9:06 AM, Adam Collins <> wrote:

> I have a series of categorical variables.  For each one, I would like to create a quick bar graph that displays the count (frequency) of each category, but also the percentage.
> For example, if it was an "hbar" graph, it might look something like this:
> __________
>          | 10
> __________|(33.3%)
> _________________
>                 | 16
> _________________|(53.3%)
> ____
>    | 4
> ____|(13.3%)
> I intend to use a "foreach" loop to iterate through the list of categorical variables, so I am looking for a solution that can be automated for each variable in my list.  I don't mind if the solution uses catplot or hbar or something else.

*   For searches and help try:

*   For searches and help try:

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