Announcement

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

  • Horizontal bar chart with confidence intervals and labels

    Hi, I am trying to improve the following graph. Specifically, I would like to put the labels on the axis instead of numbers, but I cant find a way. Note I am just using the temp_canton to order the bars, which surely can be done in a proper way too.

    Thank you very much


    twoway (bar mean_raw_X temp_canton if temp_canton==1, horizontal fcolor(maroon) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==2, horizontal fcolor(dkgreen) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==3, horizontal fcolor(maroon) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==4, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==5, horizontal fcolor(maroon) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==6, horizontal fcolor(maroon) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==7, horizontal fcolor(maroon) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==8, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==9, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==10, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==11, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==12, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==13, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==14, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==15, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==16, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==17, horizontal fcolor(dkgreen) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==18, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==19, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==20, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==21, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==22, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==23, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==24, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==25, horizontal fcolor(navy) legend(off)) ///
    (bar mean_raw_X temp_canton if temp_canton==26, horizontal fcolor(navy) legend(off)) ///
    (rcap hi_raw_X lo_raw_X temp_canton, horizontal) ///
    (scatter temp_canton mean_raw_X, msymbol(none) mlabel(canton) mlabposition(1))

    This is the dataset:
    canton mean_raw_X hi_raw_X lo_raw_X temp_canton
    JU Jura 4.934579 5.114114 4.755045 1
    TI Ticino 5.619071 5.678478 5.559665 2
    NE Neuchatel 5.728467 5.780889 5.676044 3
    GE Geneva 5.732616 5.790908 5.674325 4
    VD Vaud 5.771214 5.807302 5.735126 5
    VS Valais 5.905141 5.961002 5.849281 6
    FR Fribourg 5.96763 6.024003 5.911258 7
    TG Thurgovia 6.174515 6.238292 6.110738 8
    SO Solothurn 6.186933 6.238211 6.135654 9
    SH Schaffhausen 6.245283 6.353175 6.137391 10
    ZG Zug 6.269742 6.357928 6.181557 11
    UR Uri 6.29235 6.458621 6.126079 12
    BS Basle-Town 6.297029 6.384856 6.209203 13
    SG St. Gall 6.30759 6.349748 6.265431 14
    BL Basle-Country 6.335317 6.385091 6.285542 15
    GL Glarus 6.365231 6.50824 6.222222 16
    GR Grisons 6.376017 6.439077 6.312957 17
    BE Berne 6.386811 6.413481 6.360142 18
    AG Argovia 6.443415 6.476026 6.410804 19
    SZ Schwyz 6.447567 6.515052 6.380081 20
    ZH Zurich 6.48209 6.505616 6.458565 21
    LU Lucerne 6.499676 6.538615 6.460737 22
    AR Appenzell Outer-Rhodes 6.522981 6.643445 6.402518 23
    OW Obwalden 6.547884 6.679071 6.416697 24
    NW Nidwalden 6.835106 6.966758 6.703455 25
    AI Appenzell Inner-Rhodes 6.841177 7.039129 6.643224 26

  • #2
    I wouldn't use bar charts here. A mass of chartjunk all showing mostly that the temperatures are not zero!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str25 canton float(mean_raw_x hi_raw_x lo_raw_x) byte temp_canton
    "JU Jura"                   4.934579 5.114114 4.755045  1
    "TI Ticino"                 5.619071 5.678478 5.559665  2
    "NE Neuchatel"              5.728467 5.780889 5.676044  3
    "GE Geneva"                 5.732616 5.790908 5.674325  4
    "VD Vaud"                   5.771214 5.807302 5.735126  5
    "VS Valais"                 5.905141 5.961002 5.849281  6
    "FR Fribourg"                5.96763 6.024003 5.911258  7
    "TG Thurgovia"              6.174515 6.238292 6.110738  8
    "SO Solothurn"              6.186933 6.238211 6.135654  9
    "SH Schaffhausen"           6.245283 6.353175 6.137391 10
    "ZG Zug"                    6.269742 6.357928 6.181557 11
    "UR Uri"                     6.29235 6.458621 6.126079 12
    "BS Basle-Town"             6.297029 6.384856 6.209203 13
    "SG St. Gall"                6.30759 6.349748 6.265431 14
    "BL Basle-Country"          6.335317 6.385091 6.285542 15
    "GL Glarus"                 6.365231  6.50824 6.222222 16
    "GR Grisons"                6.376017 6.439077 6.312957 17
    "BE Berne"                  6.386811 6.413481 6.360142 18
    "AG Argovia"                6.443415 6.476026 6.410804 19
    "SZ Schwyz"                 6.447567 6.515052 6.380081 20
    "ZH Zurich"                  6.48209 6.505616 6.458565 21
    "LU Lucerne"                6.499676 6.538615 6.460737 22
    "AR Appenzell Outer-Rhodes" 6.522981 6.643445 6.402518 23
    "OW Obwalden"               6.547884 6.679071 6.416697 24
    "NW Nidwalden"              6.835106 6.966758 6.703455 25
    "AI Appenzell Inner-Rhodes" 6.841177 7.039129 6.643224 26
    end
    
    gen where = substr(canton, 4, .)
    * -labmask- is from the Stata Journal 
    labmask temp_canton, values(where)
    set scheme s1color 
    twoway scatter temp_canton mean, legend(off) yla(1/26, ang(h) grid val noticks) || rcap hi_raw_x lo_raw_x temp_canton, horizontal ytitle("") xsc(alt) ysc(reverse) xtitle(some kind of temperature ({&degree}C))
    
    gen group = 1 // navy 
    replace group = 2 if inlist(temp_canton, 1, 3, 5, 6, 7) // naroon 
    replace group = 3 if inlist(temp_canton, 2, 17) /// dkgreem 
    
    
    
    scatter temp_canton mean, legend(off) yla(1/26, ang(h) grid val noticks) || rcap hi_raw_x lo_raw_x temp_canton, horizontal ytitle("") xsc(alt) ysc(reverse) xtitle(some kind of temperature ({&degree}C)) name(G1)
    
    
    foreach v of var mean hi lo { 
        separate `v' , by(group) veryshortlabel 
    }
    
    scatter temp_canton mean_raw_x, yla(1/26, ang(h) grid val noticks) ms(none) ///
    || scatter temp_canton mean_raw_x1, mc(navy) ms(Oh) ///
    || scatter temp_canton mean_raw_x2, mc(maroon) ms(Dh) ///
    || scatter temp_canton mean_raw_x3, mc(dkgreen) ms(Th) ///
    || rcap hi_raw_x1 lo_raw_x1 temp_canton, lc(navy) horizontal ///
    || rcap hi_raw_x2 lo_raw_x2 temp_canton, lc(maroon) horizontal ///
    || rcap hi_raw_x3 lo_raw_x3 temp_canton, lc(dkgreen) horizontal ///
    ytitle("") xsc(alt) ysc(reverse) xtitle(some kind of temperature ({&degree}C)) legend(off) name(G2)
    Click image for larger version

Name:	marino_G1.png
Views:	1
Size:	45.7 KB
ID:	1669896


    Click image for larger version

Name:	marino_G2.png
Views:	1
Size:	47.7 KB
ID:	1669897


    On the other hand, the division into groups needs harder work with this design,such as more lurid colours for the smaller groups.

    Comment


    • #3
      Naturally the x-axis title should be changed if the data are not temperatures, and indeed in any case,

      Comment


      • #4
        This is great! Thank a lot Nick!

        Comment

        Working...
        X