Announcement

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

  • stacked graph bar over different years and age groups

    I have the dataset below: proportion of the population (variable called prop_pop) in different age groups (variable called grupo) over different years (variable called ano). They sum up to 1 (100%) for a given year.

    I would like to create a graph that shows the proportion of the population in each age group for the years available. Hence, each of the years in the horizontal axis should be a stacked bar. I am lost in how I can mix a stacked graph bar with the twoway command and the over option. Could anyone help? Many thanks!

    Code:
    grupo    ano    prop_pop
    0-4    2000    0.1008
    0-4    2010    0.0762
    0-4    2020    0.0696
    0-4    2030    0.0606
    0-4    2040    0.0535
    0-4    2050    0.0499
    0-4    2060    0.0472
    40-64    2000    0.2127
    40-64    2010    0.2588
    40-64    2020    0.2952
    40-64    2030    0.3253
    40-64    2040    0.3421
    40-64    2050    0.3293
    40-64    2060    0.3155
    5-39    2000    0.6284
    5-39    2010    0.5917
    5-39    2020    0.5369
    5-39    2030    0.4786
    5-39    2040    0.4303
    5-39    2050    0.4022
    5-39    2060    0.3825
    65-84    2000    0.0547
    65-84    2010    0.0666
    65-84    2020    0.0885
    65-84    2030    0.1209
    65-84    2040    0.1508
    65-84    2050    0.1835
    65-84    2060    0.2088
    85+    2000    0.0033
    85+    2010    0.0066
    85+    2020    0.0098
    85+    2030    0.0145
    85+    2040    0.0232
    85+    2050    0.0351
    85+    2060    0.0461

  • #2
    Before we can do anything serious, we need to sort out your age variable, which looks all too much like a string variable that can only sort in a dopey order, as your data example implies. (But please in future do use dataex as requested: FAQ Advice #12.)

    Then I wouldn't use twoway at all -- but graph bar -- and in fact I wouldn't use graph bar really, but rather tabplot (Stata Journal). Naturally stuff in your own language (Portuguese?) is up to you.

    Here's all my code:

    Code:
    * data 
    clear 
    input str5 grupo    ano    prop_pop
    0-4    2000    0.1008
    0-4    2010    0.0762
    0-4    2020    0.0696
    0-4    2030    0.0606
    0-4    2040    0.0535
    0-4    2050    0.0499
    0-4    2060    0.0472
    40-64    2000    0.2127
    40-64    2010    0.2588
    40-64    2020    0.2952
    40-64    2030    0.3253
    40-64    2040    0.3421
    40-64    2050    0.3293
    40-64    2060    0.3155
    5-39    2000    0.6284
    5-39    2010    0.5917
    5-39    2020    0.5369
    5-39    2030    0.4786
    5-39    2040    0.4303
    5-39    2050    0.4022
    5-39    2060    0.3825
    65-84    2000    0.0547
    65-84    2010    0.0666
    65-84    2020    0.0885
    65-84    2030    0.1209
    65-84    2040    0.1508
    65-84    2050    0.1835
    65-84    2060    0.2088
    85+    2000    0.0033
    85+    2010    0.0066
    85+    2020    0.0098
    85+    2030    0.0145
    85+    2040    0.0232
    85+    2050    0.0351
    85+    2060    0.0461
    end 
    
    set scheme s1color 
    
    * prepare stacked bar 
    label def grupo 1 "0-4" 2 "5-39" 3 "40-64" 4 "65-84" 5 "85+"
    encode grupo, gen(Grupo) label(grupo)
    
    * do stacked bar 
    graph bar (asis) prop_pop, over(Grupo) over(ano) asyvars stack ///
    legend(row(1)) ytitle(Cumulative proportion) yla(0 1 0.25 0.5 0.75, ang(h))
    
    * prepare tabplot 
    gen pc_pop = prop_pop * 100 
    label def grupo2 5 "0-4" 4 "5-39" 3 "40-64" 2 "65-84" 1 "85+"
    encode grupo, gen(Grupo2) label(grupo2)
    
    * do tabplot 
    tabplot Grupo2 ano [iw=pc_pop] , subtitle(%) showval  bfcolor(ltblue) blcolor(blue) ytitle(age) xtitle("")
    Click image for larger version

Name:	age1.png
Views:	1
Size:	27.8 KB
ID:	1461801


    Click image for larger version

Name:	age2.png
Views:	1
Size:	24.2 KB
ID:	1461802


    Notes:

    1. I haven't worked at the colours for the stacked bar but using different colours is essential.

    2. It is hard to read off changes in the smallest categories in the stacked bar chart and indeed it isn't really good for quantitative interpretation.

    3. Depending on your readers 2 decimal places in the second graph from tabplot might be too many.

    4. You can have different colours in the tabplot, but they aren't the default.

    5. I love to lose the legend (kill the key) which you can do with use of the axis labels.

    6. Strictly tabplot is an application of twoway, but perhaps not what you were imagining.

    Many more examples on this site of which first is https://www.statalist.org/forums/for...updated-on-ssc See also the Stata Journal write-up.

    Comment


    • #3
      Many thanks!!

      Comment

      Working...
      X