Statalist


[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   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: How to label bars with frequency AND percentage for categorical variables?
Date   Wed, 25 Nov 2009 08:47:16 -0000

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 

		preserve 
		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'
end 
	
Nick 
[email protected] 

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) ///
		name(gr_`var',replace)
drop count* percent rep?
}


See also:
http://www.stata.com/support/faqs/data/percentvars.html

On Tue, Nov 24, 2009 at 9:06 AM, Adam Collins <[email protected]> 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:
*   http://www.stata.com/help.cgi?search
*   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