Announcement

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

  • How can I label the values of the bars within a twoway bar graph?

    Hello!

    I am creating a bar graph with confidence intervals(picture attached below). It would be nice if I could label the bars itself so that it shows the values it conforms to. Does anyone have any suggestions? This is the code I used so far:

    twoway (bar avg_vote_2010 gender,barwidth(0.8)) ///
    (rcap low_val_2010 hi_val_2010 gender), ///
    xlabel( 0 "Female" 1 "Male", noticks) ///
    ytitle("Voting 2010 Average") by(actualtreatment)

    My goal is to label the bars with the values of the variable "avg_vote_2010." This would be similar in effect to the "blabel" option for the "graph bar" command. I would greatly appreciate any suggestions!

    Click image for larger version

Name:	Bargraph.png
Views:	1
Size:	60.2 KB
ID:	1580665


  • #2
    You can use the mlabel() option to put text on the graph at specified positions. To avoid a silly number of decimal places being displayed, your syntax could be something like this:


    Code:
    
    gen toshow = strofreal(avg_vote_2010, "%03.2f") 
    
    twoway bar avg_vote_2010 gender, barwidth(0.8) || ///
    rcap low_val_2010 hi_val_2010 gender, xlabel( 0 "Female" 1 "Male", noticks) ///
    ytitle("Voting 2010 Average") by(actualtreatment) || ///
    scatter avg_vote_2010 gender, ms(none) mla(toshow) mlabpos(12) legend(off)
    However, it is hard to see how you can place these labels without awkwardness with this design. I recommend rather something more like this:

    Code:
    
    gen toshow = strofreal(avg_vote_2010, "%03.2f") 
    
    twoway rcap low_val_2010 hi_val_2010 gender, xlabel( 0 "Female" 1 "Male", noticks) ///
    ytitle("Voting 2010 Average") by(actualtreatment) || ///
    scatter avg_vote_2010 gender, mla(toshow) legend(off) yline(0)

    Comment


    • #3
      Nick Cox Hello Nick! Thanks very much for your help. I set "mlabpos" to 1 and it displayed the value right beside the confidence interval bar and I think it made the design usable. Thank you once again!

      Comment

      Working...
      X