Announcement

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

  • Problem deleting common y-axis in catplot with by() option

    Dear Stata users,

    Context : I want to display the industry distribution within 3 age categories (relative to total employment in each age category), and show this for men and women in a combined way (left and right hand side).
    My problem is the following: the y-axis labels are repeated and eat up a lot of space.
    My objective: ideally, deleting these labels on the right-hand side plot only.

    What I have done: I have almost successfully used catplot, either in combination with graph combine, or with the by() option. However, in either versions, I would like to delete the repeated y-axis labels of the right-hand side graph, that unnecessarily eat up a lot of space (in my opinion).

    Code:
    ssc install catplot
    ssc install tabplot
    ssc install grc1leg
    
    ** OPTION 1
    
    #delimit ;
    catplot agecat industry if gender==1,
        percent(agecat)
        var1opts(label(nolabels))  
        graphregion(color(white))  
        l1title("") title("Men")
        legend(label(1 "18-34") label(2 "35-59  ") label(3 "60-64  "))
        asyvars bar(1, color(gs14)) bar(2, color(gs8)) bar(3, color(gs3))  saving("$fgs/part1.gph", replace)  ;
    #delimit cr
    
    #delimit ;
    catplot agecat industry if gender==2,
        percent(agecat)
        var1opts(label(nolabels))  
        graphregion(color(white))  
        l1title("") title("Women")
        legend(label(1 "18-34") label(2 "35-59  ") label(3 "60-64  "))
        asyvars
        bar(1, color(gs14)) bar(2, color(gs8)) bar(3, color(gs3)) saving("part2.gph", replace) ;
    #delimit cr
    
    grc1leg "part1" "part2", graphregion(color(white))
    output :

    Code:
    ** OPTION 2
    
    #delimit ;
    catplot agecat industry ,
        percent(agecat gender)
        by(gender, style(combine) graphregion(color(white)) plotregion(color(white)))
        var1opts(label(nolabels))  
        graphregion(color(white))
        asyvars
        bar(1, color(gs14)) bar(2, color(gs8)) bar(3, color(gs3))
        legend(label(1 "18-34") label(2 "35-59") label(3 "60-64")) ;
    #delimit cr
    output :


    What I have also tried: After reading a few Stata list posts (and notably a recent suggestion here: https://www.statalist.org/forums/for...ts-for-catplot) I have tried turning to tabplot, and that successfully took care of the y-axis labels problem; but another problem has arisen, which is that I am unable to display the relevant horizontal bars next to each other.

    Code:
    #delimit ;
    tabplot industry agecat,
        percent(agecat gender)
        by(gender, graphregion(color(white)) plotregion(color(white)) ) subtitle(,fcolor(white) bcolor(white))
        graphregion(color(white))
        horizontal
        separate(agecat) bar1(color(gs14)) bar2(color(gs8)) bar3(color(gs3))
        ytitle("")
        xtitle("Age categories")
        xlabel(1 "18-34" 2 "35-59  " 3 "60-64  ")
        legend(label(1 "18-34") label(2 "35-59") label(3 "60-64")) ;
    #delimit cr
    output :


    Would you be able to help? I hope the desired output is clear: similar to the catplot output, without the y-labels on the right-hand side plot.

    A working data example is below. To reduce the total size of the post I have restricted it to 100 obs, the real data much larger data (the 100 sample size makes the distribution very noisy and somewhat weird, but the problem remains the same - ylabels).

    With my deepest gratitude,

    Paul
    (uses Stata 15.1 or earlier versions)


    ---
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(gender industry) float(emp agecat)
    1  6 1 18
    2 10 1 35
    2  1 1 35
    2  6 1 18
    1  6 1 18
    1  1 1 35
    1  1 1 35
    2  9 1 35
    1  1 1 35
    1  7 1 35
    2  1 1 35
    2  3 1 35
    1  1 1 18
    1  7 1 18
    2  9 1 18
    2  6 1 35
    1  7 1 18
    1  6 1 18
    1  7 1 35
    2  1 1 35
    1  1 1 18
    1  1 1 35
    2  6 1 35
    2 10 1 35
    2  9 1 18
    2 10 1 35
    2  8 1 35
    1  6 1 18
    1  6 1 18
    1  1 1 35
    2  6 1 35
    1  1 1 18
    2  6 1 18
    1  7 1 35
    2  8 1 35
    1  1 1 35
    1  4 1 18
    2  6 1 18
    1  9 1 35
    1  5 1 35
    2  6 1 35
    1  6 1 35
    1 10 1 35
    1  3 1 18
    1  1 1 35
    1  6 1 35
    2  9 1 35
    2 10 1 18
    1  1 1 35
    1  1 1 35
    1  6 1 35
    1  3 1 35
    1  1 1 35
    2 10 1 35
    1  9 1 18
    2  3 1 18
    1  3 1 18
    2  1 1 18
    1  1 1 18
    2  6 1 18
    2  6 1 18
    2 10 1 18
    1  6 1 35
    2  1 1 35
    2  6 1 35
    2  8 1 18
    1  1 1 18
    2  6 1 18
    2  1 1 18
    2  6 1 35
    1  5 1 35
    2  1 1 18
    2  1 1 35
    2  1 1 35
    1  6 1 35
    2  7 1 18
    2  8 1 35
    1  1 1 35
    2  1 1 35
    2 10 1 35
    1  5 1 60
    1  5 1 35
    2  6 1 18
    1 10 1 35
    2  6 1 18
    1  9 1 35
    2  9 1 35
    2  5 1 35
    1  6 1 18
    1  1 1 35
    2  6 1 35
    2  6 1 35
    2  6 1 35
    2  3 1 18
    2  1 1 18
    2  6 1 35
    1  7 1 18
    2  1 1 18
    1  9 1 18
    1  3 1 35
    end
    label values gender s
    label def s 1 "Men", modify
    label def s 2 "Women", modify
    label values industry i
    label def i 1 "Agriculture", modify
    label def i 3 "Manufacturing", modify
    label def i 4 "Utilities", modify
    label def i 5 "Construction", modify
    label def i 6 "Trade", modify
    label def i 7 "Transport", modify
    label def i 8 "Finance", modify
    label def i 9 "Services", modify
    label def i 10 "Domestic Services", modify
    Attached Files
    Last edited by Paul Dutronc; 02 Apr 2019, 03:59.

  • #2
    Thanks for the clear example!

    The short answer is to use by(), not separate graphs.

    For those struggling with which graphs are which, the first two are from tabplot (best source is Stata Journal, not SSC) and the others are from catplot (SSC, as you say).

    I did this

    Code:
    catplot agecat industry, by(gender, note("")) ///
        percent(gender agecat)  var1opts(label(nolabels))  ///
        graphregion(color(white))  ///
        legend(label(1 "18-34") label(2 "35-59  ") label(3 "60-64  ") row(1)) ///
        asyvars bar(1, color(gs14)) bar(2, color(gs8)) bar(3, color(gs3))
    and removed the repeated axis labels in the Graph Editor, together with other small cosmetic changes; I too would like to know the syntax for suppressing the repeated axis labels.

    Click image for larger version

Name:	dutronc.png
Views:	1
Size:	24.9 KB
ID:	1491340

    Comment


    • #3
      I had a second go with tabplot. (I rarely use catplot for my own work and usually reach for tabplot instead.)

      I like this graph much more than #2.

      Code:
      label def agecat 18 "18-34" 35 "35-59" 60 "60-64"
      label val agecat agecat 
      
      local spaces = 3 * uchar(9) 
          
      tabplot industry agecat, by(gender, compact note("% by gender and age group", size(small))) ///
          percent(gender agecat) showval(offset(0.2))   ///
          graphregion(color(white))  ytitle("") xtitle("`spaces'Age group", size(small))  ///
           xsc(titlegap(*8)) subtitle(, fcolor(none))  /// 
          separate(agecat) barall(blcolor(black)) bar1(bfcolor(gs14)) bar2(bfcolor(gs8)) bar3(bfcolor(gs3))

      Click image for larger version

Name:	dutronc2.png
Views:	1
Size:	22.0 KB
ID:	1491349

      Comment


      • #4
        Thanks a lot for both these suggestions, which are very useful.
        I had momentarily forgotten hand-editing through the GE!
        I'll go for tabplot as well, I may have to replicate this (or variants of it).

        Comment

        Working...
        X