Announcement

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

  • Stacked Bar, Blabel: How to remove intervening categories except final cumulative count?

    Hi

    I use the following code to generate stacked bar-chart. In each bar, the intervening cumulative numbers are shown (e.g. for column 2 (mpg= 14), each cumulative count : 1,3,5 are shown, for mpg=18 ,1,..,9 is shown). I want to show only the final cumulative count at the top (i.e only show 5 for mpg=14, and 9 for mpg=18).

    I would appreciate help on this. It seems like a simple fix but I couldnt find anything in documentation and other examples posted under blabel search on this forum. Thanks for your help.

    sysuse auto
    graph bar (count)foreign, over(rep78) over(mpg) blabel(total) asyvars stack



  • #2
    I don't see a way to do this in the graph bar command. I've run into similar limitations with stacked bar graphs before and ended up having to add the text after creating the graph. I created a variable with the information I wanted (in your case, you would want the total number of foreign for each bar), and then looped through the bars adding the text. Here's some code to get you moving in that direction:

    Code:
    *create some group variable containing the count for each bar
    *make sure the order of group variable matches the bar order
    local j=1
    foreach j of local groups {
    qui sum varname, meanonly
    local i "`r(mean)'"
    gr_edit .plotregion1.barlabels[`j'].text = {}
    gr_edit .plotregion1.barlabels[`j'].text.Arrpush `i' 
    local ++j
    }
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Here's a fudge with twoway

      Code:
      sysuse auto, clear 
      graph bar (count)foreign, over(rep78) over(mpg) blabel(total) asyvars stack
      
      tokenize "red red*0.4 blue*0.4 blue*0.7 blue" 
      
      gen work0 = 0 
      local call "twoway" 
      quietly forval j = 1/5 { 
        local i = `j' - 1 
        bysort mpg : gen work`j' = work`i' + sum(rep78 == `j') 
        by mpg : replace work`j' = work`j'[_N] 
        local call `call' rbar work`i' work`j' mpg, bfcolor(``j'') blcolor(gs2) || 
        local order `order' `j' "`j'" 
        local J `j' 
      }
      `call' scatter work`J' mpg, ms(none) mla(work`J') mlabpos(12) legend(order(`order') row(1)) ///
      xtitle("`: var label mpg'") ytitle(Frequency)
      Click image for larger version

Name:	fudge.png
Views:	1
Size:	16.1 KB
ID:	1331297

      Comment


      • #4
        Thank you Carole and Nick for the codes. I thought I was missing something simple in the code, but looks like it isnt easy after all.

        Comment


        • #5
          Indeed. But most things that are easy in Stata are only easy because someone wrote a program to do it in one line. If someone with programming experience wanted to do this again and again, they would write a program to do it.

          Comment

          Working...
          X