Annual death rates in people of different ages

Author

math4mad

Code
include("utils.jl")
[ Info: loading success
["Entity", "Code", "Year", "Central death rate - Type: period - Sex: both - Age: 0", "Central death rate - Type: period - Sex: both - Age: 10", "Central death rate - Type: period - Sex: both - Age: 25", "Central death rate - Type: period - Sex: both - Age: 45", "Central death rate - Type: period - Sex: both - Age: 65", "Central death rate - Type: period - Sex: both - Age: 80"]

2. load data->dataframe

Code
df=@pipe CSV.File("./data/annual-death-rates-in-different-age-groups.csv")|>DataFrame
cats=names(df)
rename!(df,  "Central death rate - Type: period - Sex: both - Age: 0"=>:age0,
              "Central death rate - Type: period - Sex: both - Age: 10"=>:age10,
              "Central death rate - Type: period - Sex: both - Age: 25"=>:age25,
              "Central death rate - Type: period - Sex: both - Age: 45"=>:age45,
              "Central death rate - Type: period - Sex: both - Age: 65"=>:age65,
              "Central death rate - Type: period - Sex: both - Age: 80"=>:age80    
)
first(df,10)
10×9 DataFrame
Row Entity Code Year age0 age10 age25 age45 age65 age80
String String15? Int64 Float64 Float64 Float64 Float64 Float64 Float64
1 Afghanistan AFG 1950 353.469 7.10599 14.9553 24.7113 72.9251 219.856
2 Afghanistan AFG 1951 350.066 7.04221 14.8208 24.475 72.3925 218.43
3 Afghanistan AFG 1952 342.826 6.91651 14.5584 24.0431 71.3978 215.819
4 Afghanistan AFG 1953 335.489 6.79443 14.3049 23.6353 70.4698 213.431
5 Afghanistan AFG 1954 328.739 6.88345 14.2338 23.4879 70.0566 211.938
6 Afghanistan AFG 1955 320.919 6.55437 13.8029 22.8619 68.6513 208.779
7 Afghanistan AFG 1956 313.825 6.44994 13.5662 22.4995 67.7879 206.572
8 Afghanistan AFG 1957 306.734 6.31585 13.2953 22.0842 66.7916 204.067
9 Afghanistan AFG 1958 299.564 6.1871 13.0174 21.6528 65.7387 201.421
10 Afghanistan AFG 1959 292.941 6.06023 12.7585 21.249 64.7374 198.98
Code
make_title("Part 2: Plot")
Code
france=@chain df begin 
   @filter(Entity=="France")
   @select(3:9)
   @pivot_longer(_, 2:7, names_to = age_group, values_to =rate)
   #@group_by(age_group)
end

two_country=@chain df begin 
   @select(1,3:9)
   @pivot_longer(_, 3:8, names_to = age_group, values_to =rate)
   @filter(Entity  ["France","South Africa"])
   @group_by(Entity)
   @arrange(Year)
   @ungroup
end
1668×4 DataFrame
1643 rows omitted
Row Entity Year age_group rate
String Int64 String Float64
1 France 1816 age0 205.34
2 France 1816 age10 6.15
3 France 1816 age25 10.25
4 France 1816 age45 14.52
5 France 1816 age65 46.12
6 France 1816 age80 163.13
7 France 1817 age0 200.27
8 France 1817 age10 6.42
9 France 1817 age25 9.64
10 France 1817 age45 14.81
11 France 1817 age65 48.33
12 France 1817 age80 159.54
13 France 1818 age0 201.52
1657 South Africa 2020 age0 21.4911
1658 South Africa 2020 age10 0.40998
1659 South Africa 2020 age25 3.14994
1660 South Africa 2020 age45 9.43341
1661 South Africa 2020 age65 35.0478
1662 South Africa 2020 age80 86.7938
1663 South Africa 2021 age0 27.3008
1664 South Africa 2021 age10 0.46976
1665 South Africa 2021 age25 3.63036
1666 South Africa 2021 age45 10.8592
1667 South Africa 2021 age65 41.6478
1668 South Africa 2021 age80 109.58
Code
  data_layer=data(two_country)
  map_layer=mapping(:Year,:rate,layout=:age_group,color=:Entity)
  visual_layer = visual(Lines,linewidth=2)
  
  plt=data_layer*map_layer*visual_layer
  draw(plt,facet = (;linkyaxes = :none),legend=(position=:bottom, titleposition=:left, framevisible=true, padding=2))
┌ Warning: Keyword argument `bgcolor` is deprecated, use `backgroundcolor` instead.
└ @ Makie ~/.julia/packages/Makie/RgxaV/src/makielayout/blocks/legend.jl:23