Announcement

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

  • Two way hbar

    Hi everyone,

    I would like to ask if it is possible to produce the graph below using hbar command. I've attached the data for reference.

    Thank you.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str11 children str6 sex float(lfp unemployment)
    "no kids"     "male"   -2.1 5.3
    "less than 6" "male"   -2.4 7.2
    "ages 6-12"   "male"     -3 6.3
    "ages 13-17"  "male"   -1.8 4.7
    "no kids"     "female" -1.5 7.6
    "less than 6" "female" -3.2   6
    "ages 6-12"   "female" -4.6 7.8
    "ages 13-17"  "female" -2.8 5.3
    end
    Click image for larger version

Name:	Screen Shot 2021-11-28 at 10.40.46 pm.png
Views:	1
Size:	56.5 KB
ID:	1638410


  • #2
    Here is a way, but you will have to specify the x-range and x-labels using graph editor code to ensure that the top and bottom axes ranges match (so instead of -xlab (0(2)9)-, you will have -.reset_rule 0 9 2- in the ensuing code).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str11 children str6 sex float(lfp unemployment)
    "no kids"     "male"   -2.1 5.3
    "less than 6" "male"   -2.4 7.2
    "ages 6-12"   "male"     -3 6.3
    "ages 13-17"  "male"   -1.8 4.7
    "no kids"     "female" -1.5 7.6
    "less than 6" "female" -3.2   6
    "ages 6-12"   "female" -4.6 7.8
    "ages 13-17"  "female" -2.8 5.3
    end
    
    
    rename (lfp unemp) var=
    reshape long var, i(children sex) j(which) string
    encode children, g(kids)
    egen groups= group(which sex), label
    recode groups (2=1 "LFP Male") (4=2 "Unemployment Male") (1=3 "LFP Female" ) (3=4 "Unemployment Female"), gen(g)
    set scheme s1mono
    separate var, by(g) veryshortlabel
    twoway (bar var? kids, by(g, xrescale note("") leg(off)) ylab(1/4,valuelabel angle(horiz)) ytitle("") ///
    horiz bcolor(red%50 red%50 blue%50 blue%50) barw(0.5 0.5 0.5 0.5)) (scatter kids var1, by(g) mc(none) mlab(var) mlabpos(0)) ///
    (scatter kids var2, by(g) mc(none) mlab(var) mlabpos(0))  (scatter kids var3, by(g) mc(none) mlab(var) mlabpos(0)) ///
    (scatter kids var4, by(g) mc(none) mlab(var) mlabpos(0))
    gr_edit .plotregion1.xaxis1[1].reset_rule -5 0 1 , tickset(major) ruletype(range)
    gr_edit .plotregion1.xaxis1[3].reset_rule -5 0 1 , tickset(major) ruletype(range)
    gr_edit .plotregion1.xaxis1[2].reset_rule 0 9 2 , tickset(major) ruletype(range)
    gr_edit .plotregion1.xaxis1[4].reset_rule 0 9 2 , tickset(major) ruletype(range)
    gr_edit .plotregion1.xaxis1[1].draw_view.setstyle, style(off)
    gr_edit .plotregion1.xaxis1[2].draw_view.setstyle, style(off)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.5 KB
ID:	1638425

    Comment


    • #3
      I haven't tried hard to replicate this exactly. I've thought more in terms of something loosely similar that might help.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str11 children str6 sex float(lfp unemployment)
      "no kids"     "male"   -2.1 5.3
      "less than 6" "male"   -2.4 7.2
      "ages 6-12"   "male"     -3 6.3
      "ages 13-17"  "male"   -1.8 4.7
      "no kids"     "female" -1.5 7.6
      "less than 6" "female" -3.2   6
      "ages 6-12"   "female" -4.6 7.8
      "ages 13-17"  "female" -2.8 5.3
      end
      
      * install from Stata Journal 
      seqvar order = 1 4 7 10 2 5 8 11  
      labmask order , values(children) 
      label var order "children"
      
      rename (lfp unemployment) (toshow1 toshow2)
      
      reshape long toshow, i(children sex) j(which)
      label def which 1 "lfp" 2 "unemployment"
      label val which which 
      
      set scheme s1color 
      
      twoway bar toshow order if sex == "female",  horizontal barw(0.8) bfcolor(orange*0.5) blcolor(orange) by(which, note("") xrescale) base(0) || bar toshow order  if  sex == "male", horizontal barw(0.8) bfcolor(blue*0.5) blcolor(blue) base(0)  /// 
       yla(, valuelabel)  legend(order(1 "female" 2 "male")) yla(1.5 "no children" 4.5 "less than 6" 7.5 "ages 6-12" 10.5 "ages 13-17" , ang(h) tlc(none)) xtitle("")
      Click image for larger version

Name:	malefemalelfpunemployment.png
Views:	1
Size:	19.0 KB
ID:	1638433

      Comment


      • #4
        Both suggestions work well. Thank you, Andrew and Nick.

        Nick, may I ask how to add the bar values in the plot? For example, the LFP rate of female with children ages 13-17 would be -2.8.

        Comment


        • #5
          See the extra code for scatter including the variable where

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str11 children str6 sex float(lfp unemployment)
          "no kids"     "male"   -2.1 5.3
          "less than 6" "male"   -2.4 7.2
          "ages 6-12"   "male"     -3 6.3
          "ages 13-17"  "male"   -1.8 4.7
          "no kids"     "female" -1.5 7.6
          "less than 6" "female" -3.2   6
          "ages 6-12"   "female" -4.6 7.8
          "ages 13-17"  "female" -2.8 5.3
          end
          
          * install from Stata Journal 
          seqvar order = 1 4 7 10 2 5 8 11  
          labmask order , values(children) 
          label var order "children"
          
          rename (lfp unemployment) (toshow1 toshow2)
          
          reshape long toshow, i(children sex) j(which)
          label def which 1 "lfp" 2 "unemployment"
          label val which which 
          
          set scheme s1color 
          
          gen where = cond(toshow < 0, 3, 9)
          
          twoway bar toshow order if sex == "female",  horizontal barw(0.8) bfcolor(orange*0.1) blcolor(orange) by(which, note("") xrescale) base(0)  ///
          || bar toshow order  if  sex == "male", horizontal barw(0.8) bfcolor(blue*0.1) blcolor(blue) base(0)  /// 
          || scatter order toshow, ms(none) mla(toshow) mlabvpos(where) ///
          yla(, valuelabel)  legend(order(1 "female" 2 "male")) yla(1.5 "no children" 4.5 "less than 6" 7.5 "ages 6-12" 10.5 "ages 13-17" , ang(h) tlc(none)) xtitle("")

          Comment

          Working...
          X