Announcement

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

  • Adding a total category in catplot command in Stata

    Hello everyone,

    I have data in long format at the individual level on individuals' gender, sector of employment, and year. I used the following command to get the distribution of both males and females across sectors in each year:

    Code:
    #d
    catplot instsec sex round [aw=expan_indiv], 
    percent(sex round)  
    var1opts(label(labsize(medsmall))) 
    var2opts(label(labsize(medsmall) angle(45)))
    var3opts(label(labsize(medsmall)))  
    intensity(25)
    asyvars stack 
    bar(1, color(gs0) fintensity(inten100)) 
    bar(2, color(gs5) fintensity(inten100))
    bar(3, color(gs10) fintensity(inten100))
    bar(4, color(gs12) fintensity(inten100))
    title("", size(medsmall)) ytitle("", size(medsmall))
    legend(rows(2) size(small) title(, size(small)))
    recast(bar) graphregion(color(white))
    blabel(bar, position(center) color(white) orientation(horizontal) format(%9.1f) size(small))
    name(emp_instsec_sex, replace)
    ;
    #d              cr
    , which produces the following graph:
    emp_instsec_sex.gph

    Is there a way to add a total category to the graph for both males and females in each year? I would appreciate your help

  • #2
    There is no data example here and the graph image should be .png not .gph. Please see https://www.statalist.org/forums/help#stata for both points.

    The main idea is to expand 2 and change the sex variable for the extra copy. If you used 1 and 2, then the total should be 3, or at least any integer not 1 and 2, and so on.

    This was all written up at https://journals.sagepub.com/doi/pdf...867X1401400117

    Here is a toy example.

    Code:
    clear 
    set obs 4
    gen outcome = _n 
    expand 4 
    sort outcome 
    egen round = seq(), to(4)
    expand 2 
    gen sex = _n > _N/2 
    label def sex 0 Male 1 Female 
    label val sex sex 
    expand 2 
    replace outcome = 3 if outcome == 2 & sex == 1 & runiform() > 0.5 
    
    catplot outcome sex round, percent(sex round)  ///
    asyvars stack recast(bar)  /// 
    blabel(bar, position(center) color(white) orientation(horizontal) format(%9.1f) size(small))
    
    expand 2, gen(expanded)
    replace sex = 2 if expanded 
    label def sex 2 Total, add 
    
    catplot outcome sex round, percent(sex round)  ///
    var2opts(label(labsize(small))) /// 
    asyvars stack recast(bar)  /// 
    blabel(bar, position(center) color(white) orientation(horizontal) format(%9.1f) size(small))
    Click image for larger version

Name:	expanded .png
Views:	1
Size:	59.7 KB
ID:	1740940

    Comment


    • #3
      Worked perfectly, thanks a lot Nick for your time

      Comment


      • #4
        Excellent. For anyone puzzled by this thread:

        catplot is a community-contributed command from SSC.

        The first line of code in #1 should I think be

        Code:
        #delimit ; 
        where the elimit is optional but explains more.

        Comment

        Working...
        X