Announcement

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

  • How to add error bar to bar chart?


    Hi, I am new to the lovely forum. I want to add error bar at the top of each of the bars in this graph. How can I do it?
    Thanks in advance for the help.
    Click image for larger version

Name:	Screenshot 2023-10-27 at 18.54.45.png
Views:	1
Size:	730.3 KB
ID:	1731798

  • #2
    I recommend searching the forum for mentions of "dynamite plot".

    Comment


    • #3
      Thanks for the hint. I am glad to see it is possible.

      My code for the attached graph is

      graph bar pre post, over(treated, label(labsize(zero))) blabel(bar, format(%9.2f)) ///
      ytit("Feature") ///
      text(-2.50 25 "Control (N=914)" -2.50 78 "Treated (N=1691)***", size(small)) ///
      legend(label(1 "Pre ") label( 2 "Post ") size(small) position(6))

      Could you advice how do I use rcap? Sorry, I am new to Stata.

      Comment


      • #4
        Here is the first hit I found for "dynamite plot" on this site (after this thread).

        https://www.statalist.org/forums/for...ultiple-colors

        There are three key points, or so I suggest.

        1. A dynamite plot is a bad idea. See the links in the above thread for precisely why.

        2. graph bar is a dead end here. You need to start with twoway bar if you want a bar chart plus error bar, except as in #1 above there are better plots. If you disagree, or you're under instruction to produce such a plot, then combine twoway bar with twoway rcap.

        3. The simplest idea is to combine twoway scatter with twoway rcap for a direct plot of confidence intervals. In very many situations, and yours looks among them too, a comparison with zero is not of direct scientific or practical interest and so bars starting at zero waste space. It is the comparison of estimates with each other that is of primary concern.

        Here is an example based on the linked thread.

        Code:
        sysuse auto, clear
        
        preserve
        
        gen Treatment = foreign + 1
        
        statsby, by(Treatment) clear: ci mean mpg
        
        foreach v in mean ub lb {
            separate `v', by(Treatment)
        }
        
        twoway scatter mean1 Treatment, ms(O) mcolor(stc1)   || ///
               scatter mean2 Treatment, ms(O) mcolor(stc2)   || ///
               rcap ub1 lb1 Treatment, lw(medium) lc(stc1) || ///
               rcap ub2 lb2 Treatment, lw(medium) lc(stc2) legend(off)  ///
               xlabel(1 "Treatment 1" 2 "Treatment 2", noticks) xsc(r(0.8 2.2)) ytitle(whatever) title(better title needed) xtitle("") aspect(1)
        restore
        Click image for larger version

Name:	two_treatment.png
Views:	1
Size:	19.7 KB
ID:	1731816
        Last edited by Nick Cox; 27 Oct 2023, 14:15.

        Comment

        Working...
        X