Announcement

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

  • Adding error bars to histogram

    I have some data where I am trying to use -graph bar- to make some figures. Using this data and code:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 question str3 subscribe float(margin se)
    "q1" "yes" .23 .05
    "q1" "no"  .14 .03
    "q2" "yes" .45  .1
    "q2" "no"  .13  .1
    "q3" "yes" .34 .07
    "q3" "no"   .3 .04
    end

    Code:
    graph bar margin, over(subscribe) over(question)
    I can get everything right except for overlaying the error bars.
    I looked at this website: https://stats.oarc.ucla.edu/stata/fa...th-error-bars/ but this seems both incredibly convoluted and also not possible anyway in my case, because I only have the data shown here (though with more observations), and not the underlying data--I was just asked to take this and make said figure. Does anyone have any alternative solutions?

  • #2
    These plots are widely known pejoratively as dynamite, detonator or plunger plots. Those are good terms for searching this forum.

    I wouldn't call them histograms.

    What isn't so predictable is that graph bar is useless for what you want and you have to switch to graph twoway.

    Thanks for the data example. This may show some technique.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 question str3 subscribe float(margin se)
    "q1" "yes" .23 .05
    "q1" "no"  .14 .03
    "q2" "yes" .45  .1
    "q2" "no"  .13  .1
    "q3" "yes" .34 .07
    "q3" "no"   .3 .04
    end
    
    encode question, gen(Q)
    encode subscribe, gen(S) 
    
    gen upper = margin + se 
    set scheme s1color 
    local colour color(blue)
    label var S "subscribes?"
    twoway rspike margin upper S, `colour' || rcap upper upper S, `colour' || bar margin S, barw(0.6) by(Q, note("") legend(off) row(1)) xla(1 2, valuelabel tlc(bg)) `colour' yla(, ang(h)) ytitle(margin + SE)
    Click image for larger version

Name:	dynamite.png
Views:	1
Size:	19.9 KB
ID:	1677378

    Comment

    Working...
    X