Announcement

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

  • Error bar in bar chart, where x variable is a string

    Dear,

    I am trying to create a bar chart exactly like this one, but including error bars:

    Code:
    graph bar (mean) longrun shortrun, over(risk) bargap(-30) legend(label(1 "Long Run") label(2 "Short Run")) ytitle("Price Elasticity") title("Price Elasticities of Energy Consumption") subtitle("for households in different energy poverty risk groups")
    Click image for larger version

Name:	bar_graph.PNG
Views:	1
Size:	23.9 KB
ID:	1711026


    I have read the following:

    https://stats.oarc.ucla.edu/stata/fa...th-error-bars/

    This does not however create the bar chart I am looking for. My data is as follows:
    Click image for larger version

Name:	risk.PNG
Views:	1
Size:	21.7 KB
ID:	1711027


    Could anyone please help me?

    Thanks in advance!

    Kind regards,

    Hein Willems

  • #2
    Please read FAQ Advice #12 and use dataex to show us a data example. As explained there, screenshots are much less help than you hope and it's almost no-one's idea of fun to type in your data.

    Comment


    • #3
      Nick Cox Thank you for telling me, this is what my data looks like:


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str16 risk float(longrun shortrun longrun_se shortrun_se)
      "Double Risk"      -.6347311 -.08921739 -.02496048 .0282817
      "Expenditure Risk"  -.493215 -.04750146 -.03827782 .0230392
      "Income Risk"      -.5024235 -.05912407 -.03889907 .0236574
      "No Risk"          -.4346997 -.04133433 -.01794829 .0144884
      end

      Comment


      • #4
        Thanks but I am now wondering how long run SEs can be negative. I am going to assume that the negative signs are spurious.

        Your data seem to allow and indeed to encourage something much more direct and subtle.

        Detonator plot, dynamite plot and plunger plot are search terms to find that the design I think you're asking for is widely practised but also widely deplored by statistical people. See for example

        https://biostat.app.vumc.org/wiki/pu...de/Poster3.pdf

        https://simplystatistics.org/posts/2...lots-must-die/

        In my view, the issues are aesthetic as well as statistical. In particular,

        * Is the key issue comparing values with zero? or with each other?

        * Does the design of a plunger give enough prominence to uncertainty as expressed by standard errors?

        * Are wide bars in strong colours attractive as well as informative?

        * Can we use direct labelling and lose the legend?

        The immediate detail that risk categories are string is not material. We just construct a numeric variable but use the text supplied as axis labels. (An equivalent method would be to ensure that the text is assigned as value labels.)

        Here is some code to show technique. There is scope to vary everything. For example, some people prefer rcap to rspike for error bars.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str16 risk float(longrun shortrun longrun_se shortrun_se)
        "Double Risk"      -.6347311 -.08921739 -.02496048 .0282817
        "Expenditure Risk"  -.493215 -.04750146 -.03827782 .0230392
        "Income Risk"      -.5024235 -.05912407 -.03889907 .0236574
        "No Risk"          -.4346997 -.04133433 -.01794829 .0144884
        end
        
        gen axis = _n 
        
        local call 
        forval i = 1/4 { 
            local call `call' `i' "`=risk[`i']'"
        }
        
        set scheme s1color 
        
        gen longrun_h = longrun + 2 * abs(longrun_se)
        gen longrun_l = longrun - 2 * abs(longrun_se)
        
        gen shortrun_h = shortrun + 2 * shortrun_se
        gen shortrun_l = shortrun - 2 * shortrun_se
        
        twoway scatter longrun axis, mc(blue) || scatter shortrun axis, mc(red) ///
        || rspike longrun_h longrun_l axis, lc(blue) /// 
        || rspike shortrun_h shortrun_l axis, lc(red) /// 
        legend(off) ytitle(Price Elasticity) ///
        xla(`call', tlc(none))  xsc(r(0.8 4.2)) xtitle("") /// 
        text(-0.15 2.5 "Short run", color(red)) text(-0.6 2.5 "Long run", color(blue))
        Click image for larger version

Name:	elasticity.png
Views:	1
Size:	16.2 KB
ID:	1711154



        Comment

        Working...
        X