Announcement

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

  • Help with bar graph with error bars

    Hello all,

    I'm having trouble getting the bar graph exactly how I want it. Here's the collapsed data, where lb and ub are the lower and upper bounds for the errors:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(xx_island urban woman perc_indigenous lb ub)
    0 0 0   .337007 .30525145 .37645414
    0 0 1  .3776891  .3437194  .4176471
    0 1 0  .2905122  .2712949  .3142721
    0 1 1 .29768202 .27791074  .3218079
    1 0 0 .38662195 .33849835   .446679
    1 0 1  .3997391  .3433408     .4713
    1 1 0  .3038693 .26285684  .3668763
    1 1 1 .30063725  .2646368  .3534003
    end
    label values xx_island xx_lbl
    label def xx_lbl 0 "Rest of the State", modify
    label def xx_lbl 1 "XX Island", modify
    label values urban urban_lbl
    label def urban_lbl 0 "Rural", modify
    label def urban_lbl 1 "Urban", modify
    label values woman woman_lbl
    label def woman_lbl 0 "Men", modify
    label def woman_lbl 1 "Women", modify

    What I want is to basically add error bars to this graph:
    Code:
    graph bar perc_indigenous, over(woman) over(urban) over(xx_island)

    Click image for larger version

Name:	Graph_example.png
Views:	3
Size:	45.0 KB
ID:	1725429



    I know that to add the error bars I have to use graph twoway with bar and rcap, however, I'm not sure how to get it to work. Thanks in advance!



  • #2
    As you say you need to use twoway rcap (or possibly twoway rspike) for the error bars. But using any kind of bar chart with error bars risks problems well diagnosed in polemics against dynamite, detonator or plunger plots. See e.g.

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

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

    There are many small choices here, but some technique that can be varied is embedded in this code.

    I prize horizontal text and direct labelling and despise any use of legends that is avoldable.

    I have guessed that you want to show % despite the fact that your outcome variable looks like a proportion.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(xx_island urban woman perc_indigenous lb ub)
    0 0 0   .337007 .30525145 .37645414
    0 0 1  .3776891  .3437194  .4176471
    0 1 0  .2905122  .2712949  .3142721
    0 1 1 .29768202 .27791074  .3218079
    1 0 0 .38662195 .33849835   .446679
    1 0 1  .3997391  .3433408     .4713
    1 1 0  .3038693 .26285684  .3668763
    1 1 1 .30063725  .2646368  .3534003
    end
    label values xx_island xx_lbl
    label def xx_lbl 0 "Rest of the State", modify
    label def xx_lbl 1 "XX Island", modify
    label values urban urban_lbl
    label def urban_lbl 0 "Rural", modify
    label def urban_lbl 1 "Urban", modify
    label values woman woman_lbl
    label def woman_lbl 0 "Men", modify
    label def woman_lbl 1 "Women", modify
    
    gen man = 1 - woman 
    label def man 1 "Men" 0 "Women" 
    label val man man
    
    egen group = group(urban man), label 
    set scheme stcolor 
    scatter group perc_indigenous, by(xx_island, t1title(% indigenous) imargin(medium) col(1) note("") legend(off)) || rcap ub lb group, horizontal yla(1/4, tlc(none) valuelabel)  subtitle(, pos(9) fcolor(none) nobox nobexpand) ytitle("") xla(.25 "25" .30 "30" .35 "35" .40 "40" .45 "45") ysc(r(0.7 4.3)) xsc(alt) name(G1, replace) 
    
    twoway bar perc_indigenous group, base(0) barw(0.8) horizontal by(xx_island, t1title(% indigenous) imargin(medium) col(1) note("") legend(off)) || rcap ub lb group, horizontal yla(1/4, tlc(none) valuelabel)  subtitle(, pos(9) fcolor(none) nobox nobexpand) ytitle("") xla(.25 "25" .30 "30" .35 "35" .40 "40" .45 "45") ysc(r(0.7 4.3)) xsc(alt) name(G2, replace)
    Click image for larger version

Name:	xx_G1.png
Views:	1
Size:	30.0 KB
ID:	1725433
    Click image for larger version

Name:	xx_G2.png
Views:	1
Size:	25.8 KB
ID:	1725434


    Comment

    Working...
    X