Announcement

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

  • How to draw a bar graph by age on y-axis by sex and over the period for the following data.

    clear
    input byte(measure_id sex_id) int(year age) double(sub_val val)
    1 1 2000 0 415587.3849337101 837065.6345280409
    1 1 2010 0 290244.89841389656 621544.9762610793
    1 1 2019 0 157938.7216079235 369419.74409517646
    1 2 2000 0 395341.36759617925 763733.3661207259
    1 2 2010 0 296033.86251944304 587464.1154192686
    1 2 2019 0 168120.0363419205 358453.3888616115
    1 3 2000 0 810928.7519500256 1600799.0170042515
    1 3 2010 0 586278.7677998543 1209009.1028897762
    1 3 2019 0 326058.7492867112 727873.1252025366
    1 1 2000 1 140027.1518392563 230345.7930060625
    1 1 2010 1 75096.22447288036 131432.78914384544
    1 1 2019 1 23468.021888911724 53006.693779286
    1 2 2000 1 188729.39758491516 298168.38675683737
    1 2 2010 1 104197.15360999107 171399.46037748456
    1 2 2019 1 30137.767442941666 59522.309093393385
    1 3 2000 1 328756.5523109436 528514.184536159
    1 3 2010 1 179293.37906885147 302832.25037258863
    1 3 2019 1 53605.790291786194 112529.00427203625
    1 1 2000 2 31058.57539653778 93602.27115210425
    1 1 2010 2 20661.363711714745 67263.61809724662
    1 1 2019 2 9912.696691274643 38867.87660894543
    1 2 2000 2 46464.99015712738 107167.60274564638
    1 2 2010 2 23550.80017399788 61008.91625909554
    1 2 2019 2 13010.941456079483 38978.251368196914
    1 3 2000 2 77523.56801223755 200769.8761595823
    1 3 2010 2 44212.16451239586 128272.53568308894
    1 3 2019 2 22923.63750886917 77846.1282645883
    1 1 2000 3 17349.423283576965 72264.93885537004
    1 1 2010 3 13145.074568748474 60379.63798735803
    1 1 2019 3 7139.931537628174 37103.979527642485
    1 2 2000 3 22268.56805896759 66856.12360407846
    1 2 2010 3 13964.970987319946 47965.00347286952
    1 2 2019 3 9178.664719104767 34559.389757715166
    1 3 2000 3 39617.991505622864 139121.06218921082
    1 3 2010 3 27110.045944213867 108344.64188926411
    1 3 2019 3 16318.596510887146 71663.3691900596
    1 1 2000 4 22113.93385696411 87461.90142887086
    1 1 2010 4 16622.63201904297 76879.01485773176
    1 1 2019 4 10682.886200666428 55519.073502700776
    1 2 2000 4 30692.766445159912 102989.67901076376
    1 2 2010 4 21412.130324840546 78246.36408299208
    1 2 2019 4 14205.8269790411 57270.644002933055
    1 3 2000 4 52806.70006561279 190451.57990309596
    1 3 2010 4 38034.76149845123 155125.37763553858
    1 3 2019 4 24888.71334695816 112789.71679802239
    1 1 2000 5 27045.370234012604 104130.26029264927
    1 1 2010 5 25593.49788761139 112457.64679711312
    1 1 2019 5 18804.188626289368 91281.70305936411
    1 2 2000 5 35576.942813396454 119714.64575389028
    1 2 2010 5 27973.726600170136 99780.75041074306
    1 2 2019 5 20333.172914862633 77033.64204104617
    1 3 2000 5 62622.31403064728 223844.9066838771
    1 3 2010 5 53567.224637031555 212238.39638757706
    1 3 2019 5 39137.36143064499 168315.34579297155
    1 1 2000 6 37620.095880031586 125031.6375065893
    1 1 2010 6 33846.31837463379 129838.87861100957
    1 1 2019 6 24932.39094531536 102892.8429427892
    1 2 2000 6 38246.07305145264 115413.3682961464
    1 2 2010 6 29177.23086452484 95821.87271294743
    1 2 2019 6 22837.29287546873 74987.2165481858
    1 3 2000 6 75866.16893577576 240445.00673872232
    1 3 2010 6 63023.55003118515 225660.75318168104
    1 3 2019 6 47769.68417739868 177880.05998778343
    1 1 2000 7 48233.20755624771 138564.39086249098
    1 1 2010 7 40024.57496738434 135074.97196973488
    1 1 2019 7 38879.59417307377 132556.23171487823
    1 2 2000 7 42770.13943529129 110157.89205764234
    1 2 2010 7 33415.38653373718 93820.09724063426
    1 2 2019 7 28941.66482102871 79466.1313412562
    1 3 2000 7 91003.3480014801 248722.28321304917
    1 3 2010 7 73439.95915937424 228895.06862366945
    1 3 2019 7 67821.2576084137 212022.36166863889
    1 1 2000 8 64022.60876941681 160391.230187878
    1 1 2010 8 64275.962792396545 182419.49952665716
    1 1 2019 8 57008.40997183323 165777.49424038082
    1 2 2000 8 42773.49138832092 100169.3349955976
    1 2 2010 8 38702.26919841766 98208.3593646437
    1 2 2019 8 37341.794684529305 91670.29385262728
    1 3 2000 8 106796.1007194519 260560.56642717123
    1 3 2010 8 102978.23292160034 280627.85842233896
    1 3 2019 8 94350.20440506935 257447.78803581
    1 1 2000 9 73252.11087226868 165572.8483517766
    1 1 2010 9 77605.10270261765 194795.25165511668
    1 1 2019 9 70979.26396465302 182082.1284391135
    1 2 2000 9 45532.14657497406 93971.12412154302
    1 2 2010 9 45595.37193489075 101739.01847390085
    1 2 2019 9 51200.68801236153 111186.61429525912
    1 3 2000 9 118784.25663375854 259543.9722980298
    1 3 2010 9 123200.47318172455 296534.26851542294
    1 3 2019 9 122179.95160937309 293268.7416496873
    1 1 2000 10 94846.2070274353 202338.0748974532
    1 1 2010 10 100404.38147640228 231362.3306872174
    1 1 2019 10 99117.89592671394 232838.73156763613
    1 2 2000 10 57490.180711746216 113130.5801549051
    1 2 2010 10 61728.7478761673 129532.7700083442
    1 2 2019 10 61608.60598421097 131164.35364434123
    1 3 2000 10 152336.38751220703 315468.65454265475
    1 3 2010 10 162133.12848091125 360895.1001698673
    1 3 2019 10 160726.50050091743 364003.08095887303
    1 1 2000 11 103492.14153289795 212775.6069868207
    end
    label values age age
    label def age 0 "<1", modify
    label def age 1 "1-4", modify
    label def age 2 "5-9", modify
    label def age 3 "10-14", modify
    label def age 4 "15-19", modify
    label def age 5 "20-24", modify
    label def age 6 "25-29", modify
    label def age 7 "30-34", modify
    label def age 8 "35-39", modify
    label def age 9 "40-44", modify
    label def age 10 "45-49", modify
    label def age 11 "50-54", modify
    [/CODE]
    ------------------ copy up to and including the previous line ------------------



    The data contains 11 age groups and three sex id (male female and overall) for three years (2000, 2010, 2019). I want to draw a bar graphs of percentages (sub_val/val*100) for each age group and sex. Age group should be on y-axis for three periods. I need the graphs for male and female.

    Thanks


  • #2
    You have 12 age groups (not 11) and 2 sexes and 3 years. That is 72 bars and I doubt that you can get a good clear graph out of that, which may be why you are asking. What code did you try?

    I got this far with a dot chart, which seems likely to work much better.

    Code:
    clear
    input byte(measure_id sex_id) int(year age) double(sub_val val)
    1 1 2000 0 415587.3849337101 837065.6345280409
    1 1 2010 0 290244.89841389656 621544.9762610793
    1 1 2019 0 157938.7216079235 369419.74409517646
    1 2 2000 0 395341.36759617925 763733.3661207259
    1 2 2010 0 296033.86251944304 587464.1154192686
    1 2 2019 0 168120.0363419205 358453.3888616115
    1 3 2000 0 810928.7519500256 1600799.0170042515
    1 3 2010 0 586278.7677998543 1209009.1028897762
    1 3 2019 0 326058.7492867112 727873.1252025366
    1 1 2000 1 140027.1518392563 230345.7930060625
    1 1 2010 1 75096.22447288036 131432.78914384544
    1 1 2019 1 23468.021888911724 53006.693779286
    1 2 2000 1 188729.39758491516 298168.38675683737
    1 2 2010 1 104197.15360999107 171399.46037748456
    1 2 2019 1 30137.767442941666 59522.309093393385
    1 3 2000 1 328756.5523109436 528514.184536159
    1 3 2010 1 179293.37906885147 302832.25037258863
    1 3 2019 1 53605.790291786194 112529.00427203625
    1 1 2000 2 31058.57539653778 93602.27115210425
    1 1 2010 2 20661.363711714745 67263.61809724662
    1 1 2019 2 9912.696691274643 38867.87660894543
    1 2 2000 2 46464.99015712738 107167.60274564638
    1 2 2010 2 23550.80017399788 61008.91625909554
    1 2 2019 2 13010.941456079483 38978.251368196914
    1 3 2000 2 77523.56801223755 200769.8761595823
    1 3 2010 2 44212.16451239586 128272.53568308894
    1 3 2019 2 22923.63750886917 77846.1282645883
    1 1 2000 3 17349.423283576965 72264.93885537004
    1 1 2010 3 13145.074568748474 60379.63798735803
    1 1 2019 3 7139.931537628174 37103.979527642485
    1 2 2000 3 22268.56805896759 66856.12360407846
    1 2 2010 3 13964.970987319946 47965.00347286952
    1 2 2019 3 9178.664719104767 34559.389757715166
    1 3 2000 3 39617.991505622864 139121.06218921082
    1 3 2010 3 27110.045944213867 108344.64188926411
    1 3 2019 3 16318.596510887146 71663.3691900596
    1 1 2000 4 22113.93385696411 87461.90142887086
    1 1 2010 4 16622.63201904297 76879.01485773176
    1 1 2019 4 10682.886200666428 55519.073502700776
    1 2 2000 4 30692.766445159912 102989.67901076376
    1 2 2010 4 21412.130324840546 78246.36408299208
    1 2 2019 4 14205.8269790411 57270.644002933055
    1 3 2000 4 52806.70006561279 190451.57990309596
    1 3 2010 4 38034.76149845123 155125.37763553858
    1 3 2019 4 24888.71334695816 112789.71679802239
    1 1 2000 5 27045.370234012604 104130.26029264927
    1 1 2010 5 25593.49788761139 112457.64679711312
    1 1 2019 5 18804.188626289368 91281.70305936411
    1 2 2000 5 35576.942813396454 119714.64575389028
    1 2 2010 5 27973.726600170136 99780.75041074306
    1 2 2019 5 20333.172914862633 77033.64204104617
    1 3 2000 5 62622.31403064728 223844.9066838771
    1 3 2010 5 53567.224637031555 212238.39638757706
    1 3 2019 5 39137.36143064499 168315.34579297155
    1 1 2000 6 37620.095880031586 125031.6375065893
    1 1 2010 6 33846.31837463379 129838.87861100957
    1 1 2019 6 24932.39094531536 102892.8429427892
    1 2 2000 6 38246.07305145264 115413.3682961464
    1 2 2010 6 29177.23086452484 95821.87271294743
    1 2 2019 6 22837.29287546873 74987.2165481858
    1 3 2000 6 75866.16893577576 240445.00673872232
    1 3 2010 6 63023.55003118515 225660.75318168104
    1 3 2019 6 47769.68417739868 177880.05998778343
    1 1 2000 7 48233.20755624771 138564.39086249098
    1 1 2010 7 40024.57496738434 135074.97196973488
    1 1 2019 7 38879.59417307377 132556.23171487823
    1 2 2000 7 42770.13943529129 110157.89205764234
    1 2 2010 7 33415.38653373718 93820.09724063426
    1 2 2019 7 28941.66482102871 79466.1313412562
    1 3 2000 7 91003.3480014801 248722.28321304917
    1 3 2010 7 73439.95915937424 228895.06862366945
    1 3 2019 7 67821.2576084137 212022.36166863889
    1 1 2000 8 64022.60876941681 160391.230187878
    1 1 2010 8 64275.962792396545 182419.49952665716
    1 1 2019 8 57008.40997183323 165777.49424038082
    1 2 2000 8 42773.49138832092 100169.3349955976
    1 2 2010 8 38702.26919841766 98208.3593646437
    1 2 2019 8 37341.794684529305 91670.29385262728
    1 3 2000 8 106796.1007194519 260560.56642717123
    1 3 2010 8 102978.23292160034 280627.85842233896
    1 3 2019 8 94350.20440506935 257447.78803581
    1 1 2000 9 73252.11087226868 165572.8483517766
    1 1 2010 9 77605.10270261765 194795.25165511668
    1 1 2019 9 70979.26396465302 182082.1284391135
    1 2 2000 9 45532.14657497406 93971.12412154302
    1 2 2010 9 45595.37193489075 101739.01847390085
    1 2 2019 9 51200.68801236153 111186.61429525912
    1 3 2000 9 118784.25663375854 259543.9722980298
    1 3 2010 9 123200.47318172455 296534.26851542294
    1 3 2019 9 122179.95160937309 293268.7416496873
    1 1 2000 10 94846.2070274353 202338.0748974532
    1 1 2010 10 100404.38147640228 231362.3306872174
    1 1 2019 10 99117.89592671394 232838.73156763613
    1 2 2000 10 57490.180711746216 113130.5801549051
    1 2 2010 10 61728.7478761673 129532.7700083442
    1 2 2019 10 61608.60598421097 131164.35364434123
    1 3 2000 10 152336.38751220703 315468.65454265475
    1 3 2010 10 162133.12848091125 360895.1001698673
    1 3 2019 10 160726.50050091743 364003.08095887303
    1 1 2000 11 103492.14153289795 212775.6069868207
    end
    label values age age
    label def age 0 "<1", modify
    label def age 1 "1-4", modify
    label def age 2 "5-9", modify
    label def age 3 "10-14", modify
    label def age 4 "15-19", modify
    label def age 5 "20-24", modify
    label def age 6 "25-29", modify
    label def age 7 "30-34", modify
    label def age 8 "35-39", modify
    label def age 9 "40-44", modify
    label def age 10 "45-49", modify
    label def age 11 "50-54", modify
    
    gen toshow = 100 * sub_val / val
    separate toshow, by(sex) veryshortlabel
    graph dot toshow1 toshow2, over(age) by(year, l1title(Age group) subtitle(better title needed) note("") row(1)) ///
    marker(1, ms(Oh)) marker(2, ms(+)) ///
    legend(order(1 "male" 2 "female")) linetype(line) lines(lc(gs12) lw(vthin)) ysc(alt)  ///

    Click image for larger version

Name:	age_sex_year.png
Views:	1
Size:	34.9 KB
ID:	1730770

    Comment


    • #3
      Take 2

      Code:
      scatter age toshow1, ms(Oh) || scatter age toshow2, ms(+) ///
      by(year, l1title(Age group) subtitle(better title needed) note("") row(1)) ///
      legend(order(1 "male" 2 "female")) yla(0/11, val glp(solid) glc(gs12) glw(vthin)) ysc(reverse) xsc(alt)
      Click image for larger version

Name:	age_sex_year2.png
Views:	1
Size:	39.6 KB
ID:	1730782

      Comment


      • #4
        Nick Cox Thanks for the response sir. These are quite helpful. However, I am wondering if I can draw a graph as follows with my data.

        How to plot proportion category data of a variable for different years (eg: past and recent years) in Stata - Statalist

        Thanks.

        Comment


        • #5
          Why not try it?

          Comment

          Working...
          X