Announcement

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

  • Adding line to bar graph

    Hi,

    Is there a way to connect the top of bar graphs in stata like the graph below?

    I am using the code:

    graph bar mean_zero_thresh_ratio_I,
    over(Income,...

    Thank you!
    Attached Files

  • #2
    Use -twoway bar- instead:
    Code:
    sysuse citytemp, clear
    graph bar tempjan , over(division)
    collapse tempjan tempjul, by(division)
    twoway bar tempjan division, barw(.5) xlabel(1(1)9, valuelabel) ylabel(0(10)90)  base(0)  /// 
     || line tempju division , name(gr,replace)

    Comment


    • #3
      Hi,

      The following is my dataset:

      Country Country_Code Zerobandthreshold GNI Region_Name Income zero_thresh_ratio mean_zero_thresh_ratio_R mean_zero_thresh_ratio_I
      Fiji FJI 30000 10995.753 East Asia & Pacific Upper-Middle Income 2.728326 1.096858 1.072207
      Australia AUS 18200 69410.24 East Asia & Pacific High .2622091 1.096858 .3402028
      Papua New Guinea PNG 12500 7615.9536 East Asia & Pacific Lower-Middle Income 1.641291 1.096858 1.517274
      Lao PDR LAO 12000000 19439707 East Asia & Pacific Lower-Middle Income .6172932 1.096858 1.517274
      Myanmar MMR 1 1654167.8 East Asia & Pacific Lower-Middle Income 6.05e-07 1.096858 1.517274
      Thailand THA 150000 213443.58 East Asia & Pacific Upper-Middle Income .7027618 1.096858 1.072207
      Philippines PHL 250000 181157.26 East Asia & Pacific Lower-Middle Income 1.380016 1.096858 1.517274
      Singapore SGP 20000 76863.338 East Asia & Pacific High .2602021 1.096858 .3402028
      Cambodia KHM 12000000 5264039.3 East Asia & Pacific Lower-Middle Income 2.279618 1.096858 1.517274
      Azerbaijan AZE 2076 6810.0787 Europe and Central Asia Upper-Middle Income .3048423 .8213231 1.072207
      Albania ALB 360000 544717.54 Europe and Central Asia Upper-Middle Income .660893 .8213231 1.072207
      Cyprus CYP 19500 22148.713 Europe and Central Asia High .8804123 .8213231 .3402028
      Tajikistan TJK 72 7901.3444 Europe and Central Asia Lower-Middle Income .0091124 .8213231 1.517274
      Uzbekistan UZB 1377920 8141889.1 Europe and Central Asia Lower-Middle Income .1692384 .8213231 1.517274
      Serbia SRB 1962756 591657.82 Europe and Central Asia Upper-Middle Income 3.317384 .8213231 1.072207
      Malta MLT 9100 22337.895 Europe and Central Asia High .4073795 .8213231 .3402028
      Chile CHL 11368.06 9569227.7 Latin America & The Caribbean High .001188 1.174139 .3402028
      Jamaica JAM 1500000 634205.32 Latin America & The Caribbean Upper-Middle Income 2.365165 1.174139 1.072207
      Ecuador ECU 11310 6131.8909 Latin America & The Caribbean Upper-Middle Income 1.844455 1.174139 1.072207
      Brazil BRA 22847.88 30734.424 Latin America & The Caribbean Upper-Middle Income .7433971 1.174139 1.072207
      Dominican Republic DOM 416220 320157.98 Latin America & The Caribbean Upper-Middle Income 1.300046 1.174139 1.072207
      Nicaragua NIC 100000 64880.717 Latin America & The Caribbean Lower-Middle Income 1.54129 1.174139 1.517274
      Guyana GUY 720000 966843.61 Latin America & The Caribbean Upper-Middle Income .7446913 1.174139 1.072207
      El Salvador SLV 4064 3662.2199 Latin America & The Caribbean Lower-Middle Income 1.10971 1.174139 1.517274
      Colombia COL 37354.3 18409901 Latin America & The Caribbean Upper-Middle Income .002029 1.174139 1.072207
      Honduras HND 152557.16 54076.485 Latin America & The Caribbean Lower-Middle Income 2.821137 1.174139 1.517274
      Puerto Rico PRI 9000 21222.67 Latin America & The Caribbean High .4240748 1.174139 .3402028
      Dominica DMA 30000 17733.913 Latin America & The Caribbean Upper-Middle Income 1.691674 1.174139 1.072207
      Costa Rica CRI 9804000 6341998.4 Latin America & The Caribbean Upper-Middle Income 1.545885 1.174139 1.072207
      Uruguay URY 323232 475985.23 Latin America & The Caribbean High .6790799 1.174139 .3402028
      Panama PAN 11000 13779.763 Latin America & The Caribbean Upper-Middle Income .7982721 1.174139 1.072207
      Tunisia TUN 60000 8116.664 Middle East & North Africa Lower-Middle Income 7.3922 2.234762 1.517274
      Egypt, Arab Rep. EGY 8000 34879.935 Middle East & North Africa Lower-Middle Income .2293582 2.234762 1.517274
      Algeria DZA 120000 443039.82 Middle East & North Africa Upper-Middle Income .270856 2.234762 1.072207
      Morocco MAR 30000 28663.315 Middle East & North Africa Lower-Middle Income 1.046634 2.234762 1.517274
      Bhutan BTN 200000 186684.82 South Asia Lower-Middle Income 1.071324 1.742482 1.517274
      Afghanistan AFG 60000 38904.225 South Asia Low Income 1.542249 1.742482 1.214354
      India IND 250000 123861.44 South Asia Lower-Middle Income 2.018384 1.742482 1.517274
      Pakistan PAK 400000 171088.66 South Asia Lower-Middle Income 2.337969 1.742482 1.517274
      Senegal SEN 630000 752862.32 Sub-Saharan Africa Low Income .8368064 1.00142 1.214354
      Gabon GAB 1500000 4044931.2 Sub-Saharan Africa Upper-Middle Income .3708345 1.00142 1.072207
      Zambia ZMB 39600 13923.878 Sub-Saharan Africa Lower-Middle Income 2.844035 1.00142 1.517274
      Ethiopia ETH 7200 17106.878 Sub-Saharan Africa Low Income .4208833 1.00142 1.214354
      Burkina Faso BFA 360000 361530.29 Sub-Saharan Africa Low Income .9957672 1.00142 1.214354
      Tanzania TZA 170000 2087600.5 Sub-Saharan Africa Low Income .0814332 1.00142 1.214354
      Burundi BDI 1800000 504129.1 Sub-Saharan Africa Low Income 3.570514 1.00142 1.214354
      Chad TCD 800000 380537.07 Sub-Saharan Africa Low Income 2.102292 1.00142 1.214354
      Angola AGO 413400 659481.99 Sub-Saharan Africa Upper-Middle Income .6268556 1.00142 1.072207
      Seychelles SYC 8555.5 195618.23 Sub-Saharan Africa High .0437357 1.00142 .3402028
      Uganda UGA 2820000 2089137.9 Sub-Saharan Africa Low Income 1.349839 1.00142 1.214354
      Gambia, The GMB 24000 32381.619 Sub-Saharan Africa Low Income .7411612 1.00142 1.214354
      Sierra Leone SLE 6000000 3596132.4 Sub-Saharan Africa Low Income 1.668459 1.00142 1.214354
      Ghana GHA 3132 8488.2403 Sub-Saharan Africa Lower-Middle Income .3689811 1.00142 1.517274
      Botswana BWA 36000 75975.504 Sub-Saharan Africa Upper-Middle Income .4738369 1.00142 1.072207
      Madagascar MDG 250000 1358274.5 Sub-Saharan Africa Low Income .184057 1.00142 1.214354
      Congo, Dem. Rep. COD 524161 664207.44 Sub-Saharan Africa Lower-Middle Income .7891526 1.00142 1.517274
      Namibia NAM 50000 68622.77 Sub-Saharan Africa Upper-Middle Income .7286211 1.00142 1.072207
      Malawi MWI 360000 241601.55 Sub-Saharan Africa Low Income 1.490057 1.00142 1.214354
      Cote d'Ivoire CIV 600000 884009.88 Sub-Saharan Africa Lower-Middle Income .6787254 1.00142 1.517274
      Zimbabwe ZWE 3000 1214.7437 Sub-Saharan Africa Low Income 2.469657 1.00142 1.214354
      Rwanda RWA 360000 608548.6 Sub-Saharan Africa Low Income .5915715 1.00142 1.214354
      Equatorial Guinea GNQ 1000000 4478123.3 Sub-Saharan Africa Upper-Middle Income .2233078 1.00142 1.072207
      Guinea GIN 1000000 7684939.9 Sub-Saharan Africa Low Income .1301246 1.00142 1.214354
      Benin BEN 600000 478167.47 Sub-Saharan Africa Low Income 1.254791 1.00142 1.214354
      Denmark DNK 46200 380634.28 Western Europe High .1213764 .3139073 .3402028
      France FRA 9964 34996.833 Western Europe High .2847115 .3139073 .3402028
      United Kingdom GBR 5000 30469.157 Western Europe High .1641004 .3139073 .3402028
      Finland FIN 17600 41118.047 Western Europe High .4280359 .3139073 .3402028
      Germany DEU 9169 40469.838 Western Europe High .2265638 .3139073 .3402028
      Luxembourg LUX 11265 65733.747 Western Europe High .1713732 .3139073 .3402028
      Switzerland CHE 14500 80203.841 Western Europe High .1807894 .3139073 .3402028
      Austria AUT 11000 41933.985 Western Europe High .2623171 .3139073 .3402028
      Sweden SWE 455200 461710.92 Western Europe High .9858983 .3139073 .3402028


      My code:

      clear all
      import excel "C:\Users\wb544155\Desktop\TPAF Graphs in STATA-latest\Graphs\PIT_threshold_rates.xlsx", sheet("Sheet4") firstrow
      cd "C:\Users\wb544155\Desktop\TPAF Graphs in STATA-latest\Graphs"
      rename Region_Code Region_Name
      rename Income_Group Income
      #delimit ;
      label define reg_label 1 "East Asia & Pacific" 2 "Europe and Central Asia"
      3 "Latin America & The Caribbean" 4 "Middle East & North Africa"
      5 "North America" 6 "South Asia"
      7 "Sub-Saharan Africa" 8 "Western Europe", modify;
      #delimit cr
      label values Region_Name reg_label

      #delimit ;
      label define label_inc 1 "Low Income" 2 "Lower-Middle Income"
      3 "Upper-Middle Income" 4 "High", modify;
      #delimit cr
      label values Income label_inc

      gen zero_thresh_ratio = Zerobandthreshold/GNI
      replace zero_thresh_ratio =. if zero_thresh_ratio==0
      drop if zero_thresh_ratio==.
      egen mean_zero_thresh_ratio_R = mean(zero_thresh_ratio), by (Region_Name)
      egen mean_zero_thresh_ratio_I = mean(zero_thresh_ratio), by (Income)


      sort Region_Name
      #delimit ;
      twoway (bar mean_zero_thresh_ratio_R Region_Name,
      graphregion(color(white)) xsca(range(1(1)8),))
      (line mean_zero_thresh_ratio_R Region_Name,);
      #delimit cr


      I will fix the labels and everything but the problem is when I run my code I get the following graph with the missing bar?

      Attached Files

      Comment


      • #4
        Please use -dataex- if you are going show example data.

        One way to address this is to create a new consecutive category variables usinge -egen, group()-. Compare the following:

        Code:
        sysuse citytemp, clear
        drop if divi == 8
        collapse tempjan tempjul, by(division)
        twoway bar tempjan division, barw(.5) xlabel(1(1)9, valuelabel) ylabel(0(10)90)  base(0)  /// 
         || line tempju division , name(gr,replace)
         
        sysuse citytemp, clear
        drop if divi == 8
        egen id = group(division)
        label values id division
        collapse tempjan tempjul, by(id)
        twoway bar tempjan id, barw(.5) xlabel(1(1)8, valuelabel) ylabel(0(10)90)  base(0)  /// 
         || line tempju id , name(gr2,replace)
        Click image for larger version

Name:	gr.png
Views:	1
Size:	17.3 KB
ID:	1499434
        Click image for larger version

Name:	gr2.png
Views:	1
Size:	18.0 KB
ID:	1499435
        Last edited by Scott Merryman; 21 May 2019, 17:14.

        Comment


        • #5
          Scott Merryman gives excellent advice.

          Note that base(0) is often essential to see all bars.

          Further, from the data you list it's not obvious that there are any North American countries represented, which if true would explain why a bar is missing.

          Comment

          Working...
          X