Announcement

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

  • twoway bar: adding label above bars and imposing gaps between bars

    I need to transform a graph bar into a twoway bar once I will be plotting two different things on the same plot.

    I have two main questions
    1) how to have gaps between the bars as it is the default with the graph bar command?
    2) how to add data value labels above the bars as with the option blabel with the graph bar command?

    Below you can find the data structure, the graph bar command (which gives the output I would like to obtain with the twoway command) and the twoway bar command (still to add data value labels and impose gaps between bars).

    Data structure
    Code:
    clear
    input long country_id str48 country float cat_reg int percap_total float percap_total_reg byte Selection int year
    102 "United States"          5 9839      7380 1 2015
     74 "Andorra"                2 9203  2798.714 0 2015
     94 "Switzerland"            2 7465  2798.714 1 2015
     90 "Norway"                 2 7024  2798.714 1 2015
     87 "Luxembourg"             2 6530  2798.714 1 2015
     89 "Netherlands"            2 5579  2798.714 1 2015
     93 "Sweden"                 2 5550  2798.714 1 2015
     81 "Germany"                2 5532  2798.714 1 2015
     84 "Ireland"                2 5371  2798.714 1 2015
     75 "Austria"                2 5183  2798.714 1 2015
     78 "Denmark"                2 5144  2798.714 1 2015
     76 "Belgium"                2 4939  2798.714 1 2015
    101 "Canada"                 5 4921      7380 1 2015
     80 "France"                 2 4741  2798.714 1 2015
     71 "Australia"              1 4400 1098.6072 1 2015
     67 "Japan"                  1 4286 1098.6072 1 2015
     95 "United Kingdom"         2 4285  2798.714 1 2015
     83 "Iceland"                2 4205  2798.714 1 2015
     79 "Finland"                2 4101  2798.714 1 2015
     69 "Singapore"              1 3657 1098.6072 0 2015
     72 "New Zealand"            1 3648 1098.6072 1 2015
     88 "Malta"                  4 3642 1419.9524 0 2015
     86 "Italy"                  2 3445  2798.714 1 2015
     92 "Spain"                  2 3363  2798.714 1 2015
    151 "Qatar"                  4 3251 1419.9524 0 2015
    152 "Saudi Arabia"           4 3138 1419.9524 0 2015
     68 "South Korea"            1 2835 1098.6072 1 2015
     77 "Cyprus"                 2 2821  2798.714 0 2015
     55 "Slovenia"               2 2806  2798.714 1 2015
     91 "Portugal"               2 2712  2798.714 1 2015
    145 "Kuwait"                 4 2640 1419.9524 0 2015
     85 "Israel"                 4 2560 1419.9524 1 2015
      8 "Taiwan"                 1 2535 1098.6072 0 2015
     47 "Czech Republic"         2 2534  2798.714 1 2015
    156 "United Arab Emirates"   4 2489 1419.9524 0 2015
    140 "Bahrain"                4 2470 1419.9524 0 2015
     82 "Greece"                 2 2352  2798.714 1 2015
     54 "Slovakia"               2 2216  2798.714 1 2015
     66 "Brunei"                 1 2092 1098.6072 0 2015
     99 "Uruguay"                3 2038  948.9063 1 2015
     48 "Hungary"                2 2031  2798.714 1 2015
    119 "Trinidad and Tobago"    3 2024  948.9063 0 2015
     98 "Chile"                  3 1950  948.9063 1 2015
     58 "Estonia"                2 1946  2798.714 1 2015
     60 "Lithuania"              2 1941  2798.714 0 2015
     14 "Maldives"               6 1850   411.375 0 2015
    106 "The Bahamas"            3 1818  948.9063 0 2015
     51 "Poland"                 2 1757  2798.714 1 2015
     46 "Croatia"                2 1736  2798.714 0 2015
    150 "Oman"                   4 1684 1419.9524 0 2015
     59 "Latvia"                 2 1683  2798.714 1 2015
     45 "Bulgaria"               2 1620  2798.714 0 2015
    132 "Panama"                 3 1588  948.9063 1 2015
     62 "Russian Federation"     2 1544  2798.714 0 2015
     97 "Argentina"              3 1457  948.9063 1 2015
    135 "Brazil"                 3 1431  948.9063 1 2015
     53 "Serbia"                 2 1398  2798.714 0 2015
    126 "Costa Rica"             3 1339  948.9063 1 2015
    107 "Barbados"               3 1237  948.9063 0 2015
     57 "Belarus"                2 1232  2798.714 0 2015
    142 "Iran"                   4 1232 1419.9524 0 2015
     34 "Azerbaijan"             2 1221  2798.714 0 2015
    146 "Lebanon"                4 1207 1419.9524 0 2015
    105 "Antigua and Barbuda"    3 1198  948.9063 0 2015
     40 "Turkmenistan"           2 1171  2798.714 0 2015
     52 "Romania"                2 1128  2798.714 0 2015
    196 "South Africa"           7 1109 282.41666 0 2015
    183 "Mauritius"              7 1094 282.41666 0 2015
    172 "Equatorial Guinea"      7 1089 282.41666 0 2015
    130 "Mexico"                 3 1081  948.9063 1 2015
     44 "Bosnia and Herzegovina" 2 1076  2798.714 0 2015
     13 "Malaysia"               1 1072 1098.6072 0 2015
    195 "Namibia"                7 1033 282.41666 0 2015
    155 "Turkey"                 2 1029  2798.714 1 2015
    122 "Ecuador"                3 1028  948.9063 1 2015
    139 "Algeria"                4 1026 1419.9524 0 2015
    193 "Botswana"               7 1019 282.41666 0 2015
     36 "Kazakhstan"             2 1017  2798.714 0 2015
    118 "Suriname"               3  993  948.9063 0 2015
     50 "Montenegro"             2  985  2798.714 0 2015
    109 "Cuba"                   3  977  948.9063 0 2015
    186 "Seychelles"             7  957 282.41666 0 2015
    111 "Dominican Republic"     3  932  948.9063 0 2015
     49 "Macedonia"              2  921  2798.714 0 2015
    125 "Colombia"               3  861  948.9063 1 2015
     33 "Armenia"                2  849  2798.714 0 2015
     43 "Albania"                2  848  2798.714 0 2015
     35 "Georgia"                2  803  2798.714 0 2015
    154 "Tunisia"                4  791 1419.9524 0 2015
      6 "China"                  1  779 1098.6072 0 2015
    136 "Paraguay"               3  738  948.9063 1 2015
    144 "Jordan"                 4  730 1419.9524 0 2015
    112 "Grenada"                3  715  948.9063 0 2015
    116 "Saint Lucia"            3  714  948.9063 0 2015
    197 "Swaziland"              7  693 282.41666 0 2015
    123 "Peru"                   3  683  948.9063 1 2015
     18 "Thailand"               1  614 1098.6072 0 2015
    110 "Dominica"               3  606  948.9063 0 2015
     24 "Marshall Islands"       1  604 1098.6072 0 2015
    127 "El Salvador"            3  598  948.9063 1 2015
    end
    label values cat_reg cat_reg
    label def cat_reg 1 "East Asia and Pacific", modify
    label def cat_reg 2 "Europe and Central Asia", modify
    label def cat_reg 3 "Latin America and the Caribbean", modify
    label def cat_reg 4 "Middle East and North Africa", modify
    label def cat_reg 5 "North America", modify
    label def cat_reg 6 "South Asia", modify
    label def cat_reg 7 "Sub-Saharan Africa", modify
    
    label var cat_reg "classification of country by region"
    label var Selection "selection of countries for the graph"
    label var percap_total "Total Health Spending per Capita (thousands of 2017 US$, PPP)"
    label var percap_total_reg "Total Health Spending per Capita by Region (thousands of 2017 US$, PPP)"
    
    * highlighting Brazil in the graph
    separate percap_total, by(country == "Brazil")

    Graph using the graph bar command
    Code:
    graph bar percap_total0 percap_total1 if year==2015 & Selection==1, ///
    over(country, label(angle(90) labsize(vsmall)) sort(percap_total) descending) ///
    ytitle("") yscale(off) blabel(total, orientation(vertical) size(vsmall)) legend(off) nofill ///
    bar(2, bcolor(red))

    Graph using the twoway command so far
    Code:
    labmask country_id, values(country)
    
    gsort -year -Selection -percap_total
    gen axis = _n
    labmask axis, values(country)
    
    twoway bar percap_total0 percap_total1 axis if year==2015 & Selection==1, ///
    ytitle("") yscale(off) legend(off) plotregion(margin(zero)) ///
    xla(1/45, valuelabels ang(v) labsize(vsmall) notick)
    Many thanks!

  • #2
    I have two main questions
    1) how to have gaps between the bars as it is the default with the graph bar command?
    The option barw() will do this.

    2) how to add data value labels above the bars as with the option blabel with the graph bar command?
    Possibilities include specifying these using the text option but marker labels would be simpler. Here I use labmask from Stata journal authored by Nick Cox to label the y variable using its values.

    Code:
    *ASSIGN VALUE LABELS
    labmask percap_total0 , val( percap_total0 )
    *THE POSITION OF MARKERS IS SLIGHTLY ELEVATED, SOME TWEEKING NEEDED
    gen var= percap_total0+200
    
    *TWOWAY COMMAND
    twoway (bar percap_total0 percap_total1 axis if year==2015 & Selection==1, ///
    ytitle("") barw(0.7) yscale(off) legend(off) plotregion(margin(zero)) ///
    xla(1/45, valuelabels ang(v) labsize(vsmall) notick)) ///
    (scatter var percap_total1 axis if year==2015 & Selection==1, ///
    msym(none) mlab( percap_total0) mlabpos(12) mlabcolor(black) mlabsize(vsmall) mlabangle(ver))
    Click image for larger version

Name:	2way.png
Views:	1
Size:	56.0 KB
ID:	1446540

    Comment


    • #3
      Hi Andrew, Is it possible to make cluster of the bars/countries based on region variable (cat_reg) using 'twoway bar' command ?

      Comment


      • #4
        #3 sounds as if you are reaching for

        Code:
        help by option
        but graph hbar is likely to work better if you have very different numbers of observations in each region, which is usually true.

        Comment

        Working...
        X