Announcement

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

  • Include N (counts) as bar labels

    Hi Stata users,

    I am interested in having N/counts in a graph apart from the current label (percent). The syntax am using is below

    Code:
        set seed 12345
        
        sysuse auto, clear
        
        gen x = (runiform() > 0.5) * 100
        
        graph hbar x, over(foreign) legend(off) blabel(bar, format(%16.0fc)) ytitle("Percent")
    Thanks in advance!

  • #2
    You need to switch to twoway bar if I understand correctly. graph hbar doesn't have options for showing arbitrary text like that.

    Comment


    • #3
      Thanks Nick! Any hints on how to use
      Code:
      twoway bar

      Comment


      • #4
        The main trick here is to knit your own text to show and add it as a marker label.

        Code:
        set seed 12345
         
        sysuse auto, clear
         
        gen x = (runiform() > 0.5) * 100
            
        bysort foreign : egen count = total(x)
            
        by foreign : egen pc = mean(x)
            
        gen toshow = strofreal(count) + " (" + strofreal(pc, "%2.1f") + ")" 
            
        twoway bar pc foreign, base(0) barw(0.5) xla(0 1, valuelabel) || scatter pc foreign, ms(none) mla(toshow) mlabpos(12) yla(0(10)50) legend(off)

        Comment


        • #5
          Thanks so much Nick Cox for the fantastic solution. Much appreciated!

          Comment

          Working...
          X