Announcement

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

  • Help with labels on bar graph

    I am trying to create a horizontal bar graph grouped by categories. I wanted to add two labels to the bars, one with percentages and one with the count. I have been able to generate these bar graphs individually, but I am not sure how to go about putting it all together.

    Code:
    graph hbar, over(Condensed_OutcomeMID) over(MID_Dur_Cat) ///
    asyvars blabel(bar, format(%9.1f)) ///
    name("Outcome_by_duration_per", replace) percentages
    graph export Outcome_by_duration_per.jpg, replace
    
    graph hbar (count),over(Condensed_OutcomeMID) over(MID_Dur_Cat) ///
    asyvars blabel(bar, format(%9.1f) position(inside) color(gs16)) ///
    name("Outcome_by_duration1", replace) 
    graph export Outcome_by_duration1.jpg, replace
    In the end, I get:

    Click image for larger version

Name:	Outcome_by_duration_per.jpg
Views:	1
Size:	101.5 KB
ID:	1625702


    Click image for larger version

Name:	Outcome_by_duration1.jpg
Views:	1
Size:	155.0 KB
ID:	1625701


    In the end, I'd like the x-axis to be the percentage out of the group, and to just add the count labels within the bars like in the second graph. Also, if anyone has recommendations about how to fix the text issues (with the group labels on the y axis and the legend), I would really appreciate it! I'm still pretty new to Stata so any help is appreciated.

  • #2
    On the face of it you seem close but I fear that only moving to graph twoway will solve a desire to show frequencies and percents too.

    It seems to me that your text labels are just too long for comfort, period. I would add that Stalemate surely is the intermediate case. Your present ordering seems merely alphabetical.

    The code below uses labmask and tabplot from the Stata Journal.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(count duration) str11 duration2 float outcome str13 outcome2
     2 1 "< 50 days"   1 "Favors Side A"
     3 1 "< 50 days"   3 "Favors Side E"
    11 1 "< 50 days"   2 "Stalemate"    
     4 2 "50-200 days" 1 "Favors Side A"
     3 2 "50-200 days" 3 "Favors Side E"
    13 2 "50-200 days" 2 "Stalemate"    
     3 3 "> 200 days"  1 "Favors Side A"
     5 3 "> 200 days"  3 "Favors Side E"
    13 3 "> 200 days"  2 "Stalemate"    
    end
    
    labmask duration, values(duration2) 
    labmask outcome, values(outcome2) 
    
    egen denom = total(count), by(duration)
    gen toshow = string(count) + " (" + string(100 * count/denom, "%2.1f") + "%)" 
    
    set scheme s1color 
    tabplot outcome duration [fw=count],  showval(toshow) separate(outcome) subtitle(count (%)) ///
    bar1(bfcolor(red*0.5) blcolor(red)) bar2(blcolor(black) bfcolor(gs14)) bar3(bfcolor(blue*0.5) blcolor(blue))
    Click image for larger version

Name:	conflict.png
Views:	1
Size:	26.5 KB
ID:	1625738


    Comment


    • #3
      Here's another take. The question of why not just give a table? remains a good one.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(count duration) str11 duration2 float outcome str13 outcome2
       2 1 "< 50 days"   1 "Favors Side A"
       3 1 "< 50 days"   3 "Favors Side E"
      11 1 "< 50 days"   2 "Stalemate"    
       4 2 "50-200 days" 1 "Favors Side A"
       3 2 "50-200 days" 3 "Favors Side E"
      13 2 "50-200 days" 2 "Stalemate"    
       3 3 "> 200 days"  1 "Favors Side A"
       5 3 "> 200 days"  3 "Favors Side E"
      13 3 "> 200 days"  2 "Stalemate"    
      end
      
      labmask duration, values(duration2) 
      labmask outcome, values(outcome2) 
      
      egen denom = total(count), by(duration)
      gen toshow = string(count) + " (" + string(100 * count/denom, "%2.1f") + "%)" 
      
      set scheme s1color
      expand count  
      gen wherey = outcome + 0.1
      gen wherex = duration - 0.1  
      * ssc install stripplot 
      stripplot outcome, yla(1/3, noticks valuelabel ang(h)) vertical over(duration)  separate(outcome) note(count (%)) ///
      mc(red black blue) ms(Sh Sh Sh) stack ysc(reverse) legend(off) addplot(scatter wherey wherex, ms(none) mla(toshow)) xla(, ang(0.001) tlc(none) tlength(*2)) xsc(alt)
      Click image for larger version

Name:	conflict2.png
Views:	1
Size:	26.5 KB
ID:	1625783

      Comment


      • #4
        Thanks for your help, Nick. Unfortunately, this is a graphic that my PI wants, so I am not sure I can deviate quite so far from the original design. I will see what I can do using graph twoway. I have also adjusted my labels and the spacing in the legend to avoid the overlap you can see in those graphs. I'll post again if I cannot figure out the graph using graph twoway.

        Comment


        • #5
          This is closer to your original.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(count duration) str11 duration2 float outcome str13 outcome2
           2 1 "< 50 days"   1 "Favors Side A"
           3 1 "< 50 days"   3 "Favors Side E"
          11 1 "< 50 days"   2 "Stalemate"    
           4 2 "50-200 days" 1 "Favors Side A"
           3 2 "50-200 days" 3 "Favors Side E"
          13 2 "50-200 days" 2 "Stalemate"    
           3 3 "> 200 days"  1 "Favors Side A"
           5 3 "> 200 days"  3 "Favors Side E"
          13 3 "> 200 days"  2 "Stalemate"    
          end
          
          labmask duration, values(duration2)
          labmask outcome, values(outcome2)
          
          egen denom = total(count), by(duration)
          gen toshow = string(count) + " (" + string(100 * count/denom, "%2.1f") + "%)"
          
          set scheme s1color
          
          separate count, by(outcome)
          
          local opts horizontal barw(0.8)
          
          twoway bar count1 outcome, `opts' bfcolor(red*0.5) blcolor(red) ///
          || bar count2 outcome, `opts' bfcolor(gs14) blcolor(black) ///
          || bar count3 outcome, `opts' bfcolor(blue*0.5) blcolor(blue) ///
          by(duration, col(1) note("") legend(off) t1title("Outcome frequency (%)") )  yla(1/3, ang(h) tlc(none) valuelabel) ytitle(Duration) ///
          subtitle(, pos(9) nobox nobexpand fcolor(none))  ///
          || scatter outcome count, ms(none) mla(toshow) xsc(r(0 16) off) name(G1, replace) xla(none)
          Click image for larger version

Name:	conflict3.png
Views:	1
Size:	26.7 KB
ID:	1626170

          Comment

          Working...
          X