Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st: catplot showing not chosen categories in x-axis


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: catplot showing not chosen categories in x-axis
Date   Thu, 26 Aug 2010 16:21:50 +0100

As Eric underlines, -catplot- is a user-written command from SSC. As always, please remember to explain where user-written commands you refer to come from. 

Developing Eric's comments just very slightly: -catplot- is just a wrapper (in this case) for -graph bar-. Broadly speaking, -graph bar- knows nothing about categories that might exist but are not matched by observations. 

If you are prepared to do a bit of work you can use -twoway bar- to show a bar chart with whatever axis labels you want, which need not correspond to existent categories. 

sysuse auto, clear
contract foreign
su _freq, meanonly 
gen percent = 100 * _freq/r(sum)
gen text = string(percent, "%2.1f") 
twoway bar percent foreign, base(0) barw(0.6) ///
|| scatter percent foreign, mlabel(text) ms(none) mlabpos(12) ///
yla(0(25)75, ang(h)) xla(0 1 2 "Other", val) legend(off)

There is an essay in this spirit at 

SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
        (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
        Q2/08   SJ 8(2):269--289
        outlines techniques for producing table-like graphs

Nick 
n.j.cox@durham.ac.uk 

Eric Booth

The value "2" and its label "other" are never "chosen categories" as far as -catplot-, and Stata for that matter, are concerned. 
It's important to realize that adding a label for the value "2" where there is never actually a value of "2" for the variable (foreign) does not change the summary statistics for that variable.  

There's likely a better solution than this, but you could add a new observation with the value "2" for foreign to a very large dataset where that new observation will account for <.1% of the data, so it looks like "other" has a 0% label.  You could temporarily -expand- the dataset and the relative percentages each category would remain the same as the unexpanded data (but be aware of what you are doing here and what it is doing to your data):

***********!
sysuse auto, clear
label define origin  2 "other", add
label li origin
ta for, miss
catplot  foreign, recast(bar) percent blabel(bar, position(outside) format(%5.1f) ///
size(medium) color(red) ) ytitle(Prozent) ylabel(0(10)100, labsize(small))

	preserve
expand 1000
set obs `=_N+1'
replace for = 2 in `=_N'
ta for, miss
catplot  foreign, recast(bar) percent blabel(bar, position(outside) format(%5.1f) ///
size(medium) color(red) ) ytitle(Prozent) ylabel(0(10)100, labsize(small))
	restore
***********!

Also, it's worth mentioning that -catplot- is a user-written package by NJC available from SSC.

On Aug 25, 2010, at 2:23 PM, Jörg Eulenberger wrote:

> i am looking for an possibility in catplot to show all (not only the chosen one) categories of labelset in the x-axis.
> 
> sysuse auto
> label define origin  2 "other", add
> label list origin
> catplot  foreign, recast(bar) percent blabel(bar, position(outside) format(%5.1f) size(medium) color(red) ) ytitle(Prozent) ylabel(0(10)100, labsize(small))
> 
> In the example i want in the x axis the new third category with zero percent.
> Any ideas?


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index