Announcement

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

  • Label Top of Stacked Bar

    I'm trying to add data labels to the just the top of a stacked bar graph that gives the total height of each column; The below code labels the cumulative height for every portion of each column. Like the example code, I'm looking to do this with one "over" variable.

    Code:
    graph bar (sum) mpg trunk turn , over(foreign) stack  ///
            legend(cols(3) position(6) symxsize(3) size(vsmall)   ) blabel(total)
    The closest solution I found is here, but it generates counts instead of totals and uses the equivalent of asyvars:

  • #2
    On stacking: Just say No.

    That said, you may have to reach for twoway to do what you want. Here is some sample code.

    (On clicking the link after I wrote this. I find that was my advice then. There isn't much different except what you show as numeric text.)

    Code:
    sysuse auto, clear  
    
    collapse (sum) mpg trunk turn, by(foreign)
    gen bar2 = mpg + trunk
    gen bar3 = mpg + trunk + turn
    
    twoway bar mpg foreign, barw(0.6) || ///
    rbar mpg bar2 foreign, barw(0.6)  || ///
    rbar bar2 bar3 foreign, barw(0.6) || ///
    scatter bar3 foreign, ms(none) mla(bar3) mlabpos(12) ///
    xla(0 1, val noticks) legend(pos(3) col(1) order(3 "turn" 2 "trunk" 1 "mpg")) ysc(r(0 4200))

    Comment


    • #3
      Works perfectly. Thanks for your help.

      Comment


      • #4
        Dear Nick Cox,

        I wonder if it is possible to adapt your suggestion to horizontally stacked bars. My idea is something like the below image, but with the total height of each column. Any clues?

        Thank you,

        Rafael

        Click image for larger version

Name:	image_4110.png
Views:	1
Size:	69.8 KB
ID:	1467997

        Comment


        • #5
          Code:
          sysuse auto, clear
          
          collapse (sum) mpg trunk turn, by(foreign)
          gen bar2 = mpg + trunk
          gen bar3 = mpg + trunk + turn
          
          twoway bar mpg foreign, horizontal barw(0.6) || ///
          rbar mpg bar2 foreign, horizontal barw(0.6) || ///
          rbar bar2 bar3 foreign, horizontal barw(0.6) || ///
          scatter foreign bar3, ms(none) mla(bar3) mlabpos(3) ///
          xla(0 1, val noticks) legend(pos(3) col(1) order(3 "turn" 2 "trunk" 1 "mpg")) ysc(r(0 4500))

          Comment


          • #6
            Thanks very much, Nils.

            Works very well!

            Some minor ajustments:

            Code:
            sysuse auto, clear
            
            collapse (sum) mpg trunk turn, by(foreign)
            gen bar2 = mpg + trunk
            gen bar3 = mpg + trunk + turn
            
            twoway bar mpg foreign, horizontal barw(0.2) || ///
            rbar mpg bar2 foreign, horizontal barw(0.2) || ///
            rbar bar2 bar3 foreign, horizontal barw(0.2) || ///
            scatter foreign bar3, ms(none) mla(bar3) mlabpos(3) ///
            yla(0 1, val noticks) legend(pos(3) col(1) order(3 "turn" 2 "trunk" 1 "mpg")) xsc(r(0 4000))

            Comment

            Working...
            X