Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Including sample sizes with catplot

    Hi,

    I am trying to make a number of graphs using -catplot- and I am wondering if there is any way to show the sample size automatically?

    For example, the following code produces the following plot

    Code:
    sysuse auto.dta, clear
    set scheme burd5
    gen PriceLow = 1 if price <= 5006.5
    replace PriceLow = 0 if PriceLow != 1
    label define PriceLow 0 "High price" 1 "Low price"
    label values PriceLow PriceLow
    catplot rep78, asyvars stack percent(foreign PriceLow) over(PriceLow) ///
    over(foreign) blabel(bar, pos(center) format(%2.0f)) legend(pos(bottom) col(5))
    Click image for larger version

Name:	rep78.png
Views:	1
Size:	22.4 KB
ID:	1338908


    What I need is something like the plot below, which I've edited manually.

    Click image for larger version

Name:	rep78n.png
Views:	1
Size:	24.1 KB
ID:	1338909


    I need to create several hundred plots, so doing this manually is something I would really like to avoid if possible.

    Thanks in advance,
    Dominic.

  • #2
    catplot is from SSC, as you are asked to explain (Stata FAQ Advice 12.1). Similarly the burd5 scheme is from wherever it is.

    There is no built-in magic in catplot or graph hbar to do this. You'd need to generalise a loop like this which changes string values on the fly. As it's quite unclear whether you are looping over responses, over predictors, or both, general code does not suggest itself to me.

    Code:
    sysuse auto, clear
    
    foreach v of varlist foreign { 
         clonevar clone = `v'
         decode `v', gen(work) 
         bysort `v': replace work = work + " (n = " + string(_N) + ")" 
         * install once before this loop from Stata Journal site 
         labmask clone, values(work) 
         catplot rep78 clone, stack asyvars percent(clone) ///
         blabel(bar, pos(center) format(%2.0f)) legend(pos(bottom) col(5))
         drop clone work 
    }
    I think your example is trickier than this and calls for a new composite categorical variable on the fly, as you need different labels for the innermost predictor depending on the other predictor.

    Comment


    • #3
      Thanks for your reply Nick, and apologies for not mentioning catplot was from SSC.

      Comment

      Working...
      X