Announcement

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

  • Placing Bar Labels in Twoway Bar Graphs

    Dear Stata Community,
    I am having some difficulties in placing mean numbers as labels above each bar. I use Stata 14.2. I want to produce a bar graph with CI. So far so good. However, after having struggled for more than an hour and half on trying to use available commands to generate labels for mean bar above each bar, I am helpless and desperate. I have used the following commands:

    ci means swd_post if amount==1
    ci means swd_post if amount==2
    ci means swd_post if amount==3
    ci means swd_post if amount==4

    gen lb =4.929435 if amount==1
    gen ub=5.532104 if amount==1

    replace lb=5.358832 if amount==2
    replace ub=5.700756 if amount==2

    replace lb=5.487815 if amount==3
    replace ub=5.825519 if amount==3

    replace lb=5.262667 if amount==4
    replace ub=5.85683 if amount==4

    egen mean_swd_0=mean(swd_post) if amount==1
    egen mean_swd_1=mean(swd_post) if amount==2
    egen mean_swd_2=mean(swd_post) if amount==3
    egen mean_swd_3=mean(swd_post) if amount==4

    gen mean_swd=mean_swd_0 if mean_swd_0!=.
    replace mean_swd=mean_swd_1 if mean_swd_1!=.
    replace mean_swd=mean_swd_2 if mean_swd_2!=.
    replace mean_swd=mean_swd_3 if mean_swd_3!=.

    twoway (bar mean_swd amount, barwidth(0.3)) (rcap lb ub amount) ///
    , xlabel(1 "Three losses" 2 "Two losses" 3"One loss" 4"No loss") xscale(range(1 4.20)) legend(off) ytitle("Satisfaction With Democracy") scheme(s1mono)

    I came across the following post in Statalist: https://www.statalist.org/forums/for...way-bar-graphs
    But I could not replicate it for some reason, perhaps I am too dumb, but I would be super grateful if you could walk me through it.

    Thanks a million for your contribution.

    Best,

    L

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(swd_post amount)
    9 4
    7 4
    3 4
    6 4
    8 4
    4 4
    4 4
    . 4
    5 4
    3 4
    end

  • #2
    You could overlay your bar graph with a scatter plot (not showing the markers), which allows marker labels. To illustrate this I changed your data example a little bit.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(swd_post amount)
    9 1
    7 1
    8 1
    7 2
    6 2
    8 2
    4 3
    5 3
    5 3
    . 4
    5 4
    6 4
    5 4
    end
    
    statsby mean_swd=r(mean) upper=r(ub) lower=r(lb), by(amount) clear : ci mean swd_post
    format mean_swd %9.3g
    
    twoway (bar mean_swd amount, barwidth(0.5)) ///
           (rcap lower upper amount) ///
           (scatter mean_swd amount, msymbol(none) mlabel(mean_swd) mlabposition(1)), ///
           legend(off) ytitle("Satisfaction with democracy") xtitle("Amount")
    Click image for larger version

Name:	bar.png
Views:	1
Size:	48.0 KB
ID:	1513970


    However, personally I would get rid of the bars altogether and make a scatter plot. As Nick Cox often mentions, it is arbitrary that bars start from zero, and they make it hard to see the confidence intervals clearly.
    Code:
    twoway (rcap lower upper amount) ///
           (scatter mean_swd amount, mlabel(mean_swd) mlabposition(3)), ///
           legend(off) ytitle("Satisfaction with democracy") xtitle("Amount") xscale(range(4.2))
    Click image for larger version

Name:	scatter.png
Views:	1
Size:	46.2 KB
ID:	1513971

    Comment


    • #3
      These are fantastic. Thanks a million, Wouter. So much appreciated!

      Comment


      • #4
        See also discussion in https://www.statalist.org/forums/for...i-in-bar-chart

        Comment

        Working...
        X