Announcement

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

  • How can I create such a bar graph in Stata?

    Hi,

    I came across this figure on the internet:

    Click image for larger version

Name:	cohab.png
Views:	1
Size:	31.2 KB
ID:	1553435


    Is there any way I can create a figure like it in Stata? The features I am interested in is that it's a bar graph that shows values for two groups next to one another, one y-axis is reversed and indicates the percentage for one of the groups, and a second y-axis denotes the percentage point gap between the groups.

    I tried -twoway bar- but struggled with placing two bars within one x-category next to one another and I tried -graph bar- where I struggled with everything else.

    Does anyone have an idea? Here's the data I have guesstimated from looking at the plot:

    Code:
    clear
    input str10 cntry    marriage cohabitation
    "France"    62    64
    "Russia"    61    63
    "Hungary"    58    60
    "Poland"    30    35
    "Estonia"    55    60
    "Belgium"    65    75
    "Norway"    65    83
    "Sweden"    70    85
    "NDL"    61    75
    "UK"    63    81
    "Italy"    25    45
    end
    
    gen gap = cohabitation - marriage  // Gap variable
    egen country = rank(gap), unique  // country variable sorted by the size of the gap
    labmask country, value(cntry)       // country variable sorted by the size of the gap -- labmask might be from SSC
    Thanks
    Nora


  • #2
    Here is the general idea.

    https://www.statalist.org/forums/for...ored-on-x-axis

    Comment


    • #3
      Here's a hack at it. I haven't felt compelled to reproduce every detail. Stata's grid lines never go on top. "Lose the legend -- if you can" is one of slogans.

      labmask might be from SSC
      That's a little lazy as well as hazy. search will let you tell people where it is.

      Code:
      clear
      input str10 cntry    marriage cohabitation
      "France"    62    64
      "Russia"    61    63
      "Hungary"    58    60
      "Poland"    30    35
      "Estonia"    55    60
      "Belgium"    65    75
      "Norway"    65    83
      "Sweden"    70    85
      "NDL"    61    75
      "UK"    63    81
      "Italy"    25    45
      end
      
      gen gap = cohabitation - marriage  
      egen country = rank(gap), unique  
      
      * on SSC in -labutil-; on SJ at gr0034 
      labmask country, value(cntry)       
      
      gen y1 = -marriage
      gen y2 = 0 
      gen y3 = - marriage + cohabitation 
      
      gen x1 = country - 0.2
      gen x2 = country + 0.2
      
      set scheme s1color 
      
      * on SSC  
      mylabels 0(20)60, myscale(-@) local(show)
      
      local spaces = 70 * " "
      
      scatter y1 country, ms(p) ///
      yaxis(1 2) yla(`show', labcolor(blue) ang(h) grid) yla(0(10)20, labcolor(red) grid axis(2) ang(h)) /// 
      ytitle(Marriage, color(blue)) ytitle("`spaces' Cohabitation gap", color(red) axis(2)) /// 
      xla(1/11, noticks ang(h) valuelabel labsize(small))  legend(off) ///
      || rbar y1 y2 x1, blcolor(blue) bfcolor(blue*0.4) barw(0.4) || rbar y1 y3 x2, blc(red) bfcolor(red*0.4) barw(0.4) xsize(6)
      Click image for larger version

Name:	cohabitation.png
Views:	1
Size:	26.1 KB
ID:	1553474


      Comment


      • #4
        Oh wow, this is really cool. Can I ask one more question? Do you think this figure format can still be meaningful if the "cohabitation gap" is negative in one or more countries, like e.g. here:

        Code:
        clear
        input str10 cntry marriage cohabitation
        "France" 62 64
        "Russia" 61 63
        "Hungary" 58 60
        "Poland" 30 35
        "Estonia" 55 60
        "Belgium" 65 75
        "Norway" 65 83
        "Sweden" 70 85
        "UK" 63 81
        "Italy" 25 45
        "Prussia" 63 53
        end
        Thanks
        Nora

        Comment


        • #5
          I know this is not strictly a Stata related question, but do you think this could still be a viable approach to showing the percentages for marriage, cohabitation, and their gap?

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	63.5 KB
ID:	1553706


          Code:
          clear
          input str10 cntry marriage cohabitation
          "France" 62 64
          "Russia" 61 63
          "Hungary" 58 60
          "Poland" 30 35
          "Estonia" 55 60
          "Belgium" 65 75
          "Norway" 65 83
          "Sweden" 70 85
          "UK" 63 81
          "Italy" 25 45
          "Prussia" 63 53
          end
          
          gen gap = cohabitation - marriage  
          egen country = rank(gap), unique  
          
          * on SSC in -labutil-; on SJ at gr0034
          labmask country, value(cntry)       
          
          gen y1 = -marriage
          gen y2 = 0
          gen y3 = - marriage + cohabitation
          
          gen x1 = country - 0.2
          gen x2 = country + 0.2
          
          set scheme s1color
          
          * on SSC  
          mylabels 0(20)60, myscale(-@) local(show)
          
          local spaces = 70 * " "
          
          scatter y1 country, ms(p) ///
          yaxis(1 2) yla(`show', labcolor(blue) ang(h) grid) yla(0(10)20, labcolor(red) grid axis(2) ang(h)) ///
          ytitle(Marriage, color(blue)) ytitle("`spaces' Gap", color(green) axis(2)) ///
          xla(1/11, noticks ang(h) valuelabel labsize(small))  legend(off) ///
          || rbar y1 y2 x1, blcolor(blue) bfcolor(blue*0.4) barw(0.4) || rbar y1 y3 x2, blc(red) bfcolor(red*0.4) barw(0.4) ///
          || rbar gap y2 x2, blc(green) bfcolor(green*0.4) barw(0.4) xsize(6)

          Comment


          • #6
            It seems that cohabitation gap = cohabitation rate MINUS marriage rate and if the data give negative values, that doesn't invalidate the graph. I have to guess that may mean something like a couple is married but living apart: say they are separated or just working and living in different places.

            Comment


            • #7
              My concern is that if the data give negative values, the cohabitation bar somehow hangs in the air.

              (I had always interpreted the figure in a way that there's some outcome that the figure doesn't inform us about, like being childless or being poor or so, but I don't know, I only saved the figure because it looked cool and can't recall where I got it from.)

              Comment


              • #8
                Cool could mean clear, clever, cute or something else. I find the design shown in #1 to be a little tricky. There are other choices, such as given here. A negative gap would be very distinct here as a bar pointing left.

                Code:
                clear
                input str10 cntry    marriage cohabitation
                "France"    62    64
                "Russia"    61    63
                "Hungary"    58    60
                "Poland"    30    35
                "Estonia"    55    60
                "Belgium"    65    75
                "Norway"    65    83
                "Sweden"    70    85
                "NDL"    61    75
                "UK"    63    81
                "Italy"    25    45
                end
                
                gen gap = cohabitation - marriage  
                egen country = rank(gap), unique  
                
                * on SSC in -labutil-; on SJ at gr0034 
                labmask country, value(cntry)       
                
                set scheme s1color 
                
                local opts ysc(alt) linetype(line) lines(lc(gs12) lw(thin)) 
                
                graph dot (asis) marriage cohabitation, over(country, sort(gap))  ytitle(Marriage and cohabitation) marker(1, ms(Oh) mc(blue)) marker(2, ms(+) mc(red)) `opts' name(G1, replace)
                
                graph dot (asis) marriage cohabitation, over(country, sort(cohabitation)) `opts' ytitle(Marriage and cohabitation) marker(1, ms(Oh) mc(blue)) marker(2, ms(+) mc(red)) name(G2, replace)
                
                graph hbar (asis) gap, over(country, sort(gap)) ysc(alt) legend(on) ytitle(Gap = cohabitation - marriage) bar(1, lcolor(black) fcolor(gs12)) name(G3, replace)
                
                graph combine G1 G3 , xcommon
                Click image for larger version

Name:	cohabitation2.png
Views:	1
Size:	28.3 KB
ID:	1553719

                Comment


                • #9
                  Thanks, i meant it mostly in the "clever" sense, as I thought it could provide the same information in one figure where I would have needed two like in your last example. But thanks again, you helped me very much!

                  Comment

                  Working...
                  X