Announcement

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

  • Error bars on bar graph (or advice on alternate graphing options)

    Hi

    I am totally new to Stata (using Stata 16.1) and am grateful for any advice you can offer. I am getting more confident in running statistical analysis but still have a lot to learn in terms of presenting data.

    I want to produce a graph that shows the rate of cooperation in an economic game as between two conditions, along with error bars. The rate of cooperation is a percentage amount and, as my data contains binary values (either the participant cooperated with their counterpart, or did not cooperate), I have calculated it using a formula. This is a summary of the relevant results:

    Code:
    clear
    input byte condition float rate int(count n) float error
    0 .5762082 155 269 .030129
    1 .4185185 113 270 .030022
    end
    I have used the following command to give me a quick summary of the data:

    twoway bar rate condition, xlabel(0 1, noticks valuelabel) ylabel(0 "0" .2 "20" .4 "40" .6 "60" .8 "80" 1.0 "100") ytitle(Rate of cooperation (%))

    But I am really struggling to develop out the basic graph. I appreciate that this is a summary data set rather than the full data set and so I am probably doing something wrong just by using this but I am not sure how my main data set will help as it is just binary responses across two conditions. I have looked into cibar, along with posts on Statalist too.

    I also know that people have reservations about using bar plots and I would be happy to take any suggestions about how you might display this data in a more transparent manner.

    Best wishes

  • #2
    try the option over(),
    Code:
    graph bar rate, over(condition) yti(Rate of cooperation)
    cibar from SSC is similar but you need to use it on the full data set

    Comment


    • #3
      Here are a couple of options

      Code:
      clear
      input byte condition float rate int(count n) float error
      0 .5762082 155 269 .030129
      1 .4185185 113 270 .030022
      end
      
      set scheme s1color
      
      cii prop 269 155, agre
      gen lb = r(lb) in 1
      gen ub = r(ub) in 1
      
      cii prop 270 113, agre
      replace lb = r(lb) in 2
      replace ub = r(ub) in 2
      
      
      twoway bar rate condition,      ///
         ylab(0(.2).6)                ///
         ytitle(Rate of cooperation)  ///
         xlab(0 1) barw(.9) ||        ///
             rcap lb ub condition,    ///
         name(bar, replace)           ///
         legend(off)
      Click image for larger version

Name:	bar.png
Views:	1
Size:	39.9 KB
ID:	1645723


      Code:
      twoway rbar lb ub condition,    ///
         barw(.05) astyle(ci)         ///
         ylab(0(.2).6)                ///
         ytitle(Rate of cooperation)  ///   
         xlab(0 1)                    ///
         xscale(range(-.25 1.25)) ||  ///
             dropline rate condition, ///
         name(dropline, replace)      ///
         legend(off)
      Click image for larger version

Name:	dropline.png
Views:	1
Size:	40.9 KB
ID:	1645724

      Code:
      twoway rbar lb ub condition,    ///
         horizontal                   ///
         barw(.05) astyle(ci)         ///
         xlab(0(.2).6)                ///
         xtitle(Rate of cooperation)  ///   
         ylab(0 1)                    ///
         yscale(range(-.25 1.25)) ||  ///
             dropline rate condition, ///
         horizontal                   ///
         name(horiz, replace)         ///
         legend(off)

      Click image for larger version

Name:	horiz.png
Views:	1
Size:	35.7 KB
ID:	1645725


      Code:
      foreach lev of numlist 60(5)95 96 97 98 99 {
          cii prop 269 155, agre level(`lev')
          gen lb`lev' = r(lb) in 1
          gen ub`lev' = r(ub) in 1
          cii prop 270 113, agre level(`lev')
          replace lb`lev' = r(lb) in 2
          replace ub`lev' = r(ub) in 2
          local gr = `"`gr' || rbar lb`lev' ub`lev' condition, horizontal barw(.05) color(black%5)"'
      }    
      
      twoway `gr'                     ///
         xlab(0(.2).6)                ///
         xtitle(Rate of cooperation)  ///   
         ylab(0 1)                    ///
         yscale(range(-.25 1.25)) ||  ///
             dropline rate condition, ///
         horizontal                   ///
         lstyle(p2) mstyle(p2)        ///   
         name(fuzzy, replace)         ///
         legend(off)
      Click image for larger version

Name:	fuzzy.png
Views:	1
Size:	38.8 KB
ID:	1645726

      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Maarten Buis did most of my work for me.

        Code:
        clear
        input byte condition float rate int(count n) float error
        0 .5762082 155 269 .030129
        1 .4185185 113 270 .030022
        end
        
        set scheme s1color
        
        cii prop 269 155, agre
        gen lb = r(lb) in 1
        gen ub = r(ub) in 1
        
        cii prop 270 113, agre
        replace lb = r(lb) in 2
        replace ub = r(ub) in 2
        
        
        twoway bar rate condition,      ///
           ylab(0(.2).6)                ///
           ytitle(Rate of cooperation)  ///
           xlab(0 1) barw(.9) ||        ///
               rcap lb ub condition,    ///
           name(bar, replace)           ///
           legend(off)
        As often remarked -- here on Statalist and widely in any forum discussing graphics -- graphs offer comparisons, and the key is which comparisons are of most interest. If it's comparing rates with zero, bars starting at zero do that directly, but there is little or no extra rationale for solid thick dark bars dwarfing any coupled error bars. If it's comparing rates with each other, that is a different story.
        Click image for larger version

Name:	cibar.png
Views:	1
Size:	14.2 KB
ID:	1645760




        As is clearly documented in its help, cibar from SSC is restricted to plotting the results of ci mean.
        Last edited by Nick Cox; 19 Jan 2022, 05:53.

        Comment


        • #5
          Øyvind Snilsberg - thank you for your thoughts. This one didn't quite work for what I am trying to achieve but I do appreciate your response!

          Originally posted by Maarten Buis View Post
          Here are a couple of options
          Maarten Buis - these are amazing, thank you so much! I've been through the code now (alongside the Stata manual) and I can see what you have done.

          Nick Cox - I take your point on making sure the graph lends itself to the clearest visualisation of the comparison I am trying to make. In this case, the most important point is the difference between the two conditions - so I will use a mix of the thick bar along with the horizontal thin bars to zone in on the detail in the 0-60 range.

          I really appreciate all of your time and detailed responses!

          Comment


          • #6
            It is your graph, but my point is precisely that being interested in the difference makes the conventional bar chart a bad idea.

            https://warwick.ac.uk/fac/sci/wdsi/e...es/plunger.pdf

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

            https://twitter.com/rafalab/status/1...449?lang=en-GB

            (The link in the last is broken.)

            These are just a few of the hits that googling detonator plot or dynamite plot or plunger plot will yield.

            Comment


            • #7
              Cheers, I will take a look at the links and pivot fom the conventional thick bar chart!

              Comment

              Working...
              X