Announcement

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

  • blabel and mlabel not allowed in twoway bar graphs?

    Hello,

    I am trying to create a simple twoway bar graph with confidence intervals over two categories. I want to add a label showing sample size (N) on top of each of the bar graphs, but I get an error saying that mlab or blabel not allowed. How can I add just the count of individuals in each group as a label on top of the bar graphs?

    I first collapse the data as shown below:

    collapse (mean) meanretain_year3= retain_year3 (sd) sdretain_year3=retain_year3 (count) n=retain_year3, by(belongtreat disadvgrp)

    Then I generate the CI's:

    generate hiretain_year3 = meanretain_year3 + invttail(n-1,0.025)*(sdretain_year3 / sqrt(n))
    generate loretain_year3 = meanretain_year3 - invttail(n-1,0.025)*(sdretain_year3 / sqrt(n))

    Then I combine the two groups (disadvgrp, belongtreat) into one category - each of those have 2 sub categories

    generate disadvgrptreat = belongtreat if disadvgrp == 0
    replace disadvgrptreat = belongtreat+5 if disadvgrp == 1

    Graph works as expected below:

    twoway (bar meanretain_year3 disadvgrptreat if belongtreat==1) ///
    (bar meanretain_year3 disadvgrptreat if belongtreat==0) ///
    (rcap hiretain_year3 loretain_year3 disadvgrptreat), ///
    legend(row(1) order(1 "Intervention" 2 "Control" ) ) ///
    xlabel( 0 "Advantaged Students" 5 "Disadvantaged students", noticks) ///
    xtitle("Experimental Condition by Demographic Groups") title("Continuous Enrollment Over 2 years post-intervention")


    But, both these do not seem to work:

    mlabel(tolabel) mlabpos(12) mlabcolor(white) xla(0 1, tlcolor(none) valuelabel)
    blabel(total, position(inside) format(%9.1f) color(white))

    I have a variable called n in my collapsed dataset that I want to add as a label to the bar graphs on top.

    I appreciate any help with my question above. I also apologize if this has been answered before - I could not find responses to a similar question posted on statalist before.

    Thank you,
    Maithreyi

  • #2
    I am wondering about the same thing and have not found an easy solution. If someone wants to play with an example dataset, here is a similiar case to the OPs.
    https://stats.idre.ucla.edu/stata/fa...th-error-bars/

    Thanks!

    Comment


    • #3
      Short answer: blabel() is not allowed with twoway bar, only with graph bar. Confusing or puzzling, but true.

      Longer answer: Thanks to Miika for that reference. I guess that web page was written a long, long time ago and nobody has had the time to rewrite it. Better tricks have emerged since Stata 8.

      Key points behind my alternative:

      1. There is no advantage (beyond pedagogy) to do-it-yourself confidence intervals when ci will do it for you.

      2. statsby is your friend. search statsby to find material, including something I wrote.

      3. There is no attraction to bars starting at zero (search this forum for critiques of detonator or dynamite plots). We want clear comparisons of estimates and confidence intervals with each other, not with zero!

      4. A legend is at best a necessary evil obliging mental back and forth. Lose the legend (kill the key) if you can.

      5. Fruit salad colours that are gratuitous as well as meaningless? Black and white people aren't even dark and pale in the colour scheme.

      Here is another version, although subject in its turn to comments based on taste and judgement:


      Code:
      use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear
      
      statsby N=r(N) upper=r(ub) mean=r(mean) lower=r(lb), by(race ses) : ci mean write
      
      gen whereN = 2.5 
      
      twoway rcap upper lower race, horizontal lc(blue) ///
      || scatter race mean, mc(blue) by(ses, legend(off) note("") col(1)) ///
      yla(, val ang(h) tl(0)) ytitle("") xtitle(writing score) xla(0(20)100)  ///
      subtitle(, pos(9) nobexpand fcolor(none) nobox place(e)) ///
      || scatter race whereN, ms(none) mla(N) mlabpos(0) mlabc(gs4) mlabsize(*1.2)


      Click image for larger version

Name:	writing3.png
Views:	1
Size:	22.6 KB
ID:	1403171
      Last edited by Nick Cox; 21 Jul 2017, 13:39.

      Comment

      Working...
      X