Announcement

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

  • Combining bar graphs from different binary variables

    Dear all,

    I'm trying to get a combined bar chart for an overview of the proportions from three different binary variables (yes/no) in three different groups. Specifically, this means that I want to show for each of the three groups (close family, wider family, not family; categories from variable 'onecareperson_3') how large the respective share of tasks (pers_care, help_hh, help_paper) taken over is.

    As individual graphs, this is the code and what they currently look like (stacked bar graphs):
    Code:
    graph bar, over(pers_care) asyvars stack by(onecareperson_3)
    
    graph bar, over(help_hh) asyvars stack by(onecareperson_3)
    
    graph bar, over(help_paper) asyvars stack by(onecareperson_3)
    Click image for larger version

Name:	v1.png
Views:	1
Size:	48.3 KB
ID:	1616866 Click image for larger version

Name:	v2.png
Views:	1
Size:	48.4 KB
ID:	1616864 Click image for larger version

Name:	v3.png
Views:	1
Size:	48.3 KB
ID:	1616865

    Basically, I actually only want the "Yes" category plotted. Is there a command for this? Otherwise, as a workaround, I would have the category/bar portion disappear in color ...

    At best, I would like to have three bars for each group next to each other with the respective "Yes" shares of the tasks (all in one row). Can this be done?

    Thanks for any help!
    Last edited by Ariane Arbol; 29 Jun 2021, 16:11.

  • #2
    Not really good at Stata graph. I usually just compute the stats and then reshape it, followed by using different "over()" to arrange the clustering. I hope other users may have a more direct/elegant solution. Here is an example. Hope it may help:

    Code:
    sysuse nlsw88, clear
    
    rename union x1
    rename collgrad x2
    rename married x3
    
    collapse (mean) x1 x2 x3, by(race)
    gen id = _n
    reshape long x, i(id) j(item)
    
    label define l_item 1 "Union" 2 "Collgrad" 3 "Married"
    label values item l_item
    
    gen xp = round(x * 100, 0.01)
    
    graph bar xp, over(race) over(item)
    Last edited by Ken Chui; 29 Jun 2021, 19:17.

    Comment


    • #3
      This may be easier than you fear. Taking the example of Ken Chui a bif further you don't need a collapse. You do want a mean.


      Code:
      sysuse nlsw88, clear
      set scheme s1color 
      
      graph hbar (mean) union collgrad married, by(race) asyvars name(G1, replace)
      
      ssc inst statplot 
      statplot union collgrad married, by(race) name(G2, replace)
      Code:
      
      
      The code above produces two versions. I prefer that produced by statplot but the other is easy to produce. You can have bar not hbar either way.


      Click image for larger version

Name:	threebar.png
Views:	1
Size:	22.0 KB
ID:	1616938

      Comment


      • #4
        Thank you so much for your hints, Ken and Nick!

        I was just trying Ken's approach when Nick's suggestion came in. So I tried the seemingly easier solution from Nick, and the result is:
        Code:
        graph bar (mean) pers_care help_hh help_paper, by(onecareperson_3, row(1) note("")) asyvars    ///
            bargap(15) bar(1, color(black) fcolor(white)) bar(2, color(gs10)) bar(3, color(black))        ///
            legend(rows(1) ring(6) label(1 "Personal care") label(2 "Household help") label(3 "Help with paperwork") size(small))
        Click image for larger version

Name:	v4.png
Views:	1
Size:	42.0 KB
ID:	1617109
        However, I would like to change the subtitles of each graph, having a 'Close family' label instead of '1.close family' (same with the other labels). I tried the following variations, but none of them got me to label the graphs individually:
        Code:
        [graph bar command from above] ///
        
        subtitle("Close family")  --> labels all three graphs with 'Close family'
        
        subtitle(1 "Close family")  --> labels all three graphs with '1 Close family'
        
        subtitle(1, "Close family")  --> error message ('parantheses unbalanced')
        
        subtitle1("Close family")  --> error message ('option (...) not allowed')
        
        1subtitle("Close family")  --> error message('option (...) not allowed')
        
        sub1title("Close family")  --> error message('option (...) not allowed')
        In the Graph Editor, the positions are called 'subtitle[1]', subtitle[2], and subtitle[3]. I just don't get how to refer to them with Stata commands. Does anyone know or have an idea?

        Thanks for any hints!

        Comment


        • #5
          I'd fix that before your graphics command. by() takes its subtitle text from the values (or if defined value labels) of the variable concerned and you're better off fixing that upstream. If there's good reason for your existing values or value labels, you can clone the variable and/or create another set of value labels.

          It may seem odd that you can edit the legend labels on the fly but not the text describing panels, but there it is.

          If syntax isn't documented, there is occasionally an undocumented option that will do what you want, but that's rare.

          Comment


          • #6
            Nick, thank you for your feedback.

            I'm a little confused now because I had the variable values labeled and just had relabeled them again, but the result remained the same: Stata inserts '1. ...', '2. ...' and '3. ...' in advance of the label names, which I don't want. I now created a new variable, redefined the fourth category of the variable (which do not have any observations in the subsample I use for this description) as missings and labeled it again - and now it works!

            Probably I was somewhat at a loss with this fourth category, which is not of importance here, but is basically present. However, thank you so much for your advice!

            Comment


            • #7
              I've never seen graph bar unilaterally changing panel text. That sounds more like what numlabel is intended to do.

              Comment

              Working...
              X