---
title: "Death Rates"
author: "Matteo Ceccarello"
date: "2022-11-11"
output: html_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(viridis)
```

```{r}
path <- "death_rates/Mx_1x1/ITA.Mx_1x1.txt"
read_table(path, skip=2, na=".") %>%
  filter(is.na(as.double(Female)))
```



```{r}
path <- "death_rates/Mx_1x1/ITA.Mx_1x1.txt"
plotdata <- read_table(path, skip=2) %>%
  filter(Age != "110+") %>%
  mutate(across(c("Female", "Male", "Total"), as.double)) %>% 
  mutate(Age = as.integer(Age), Year = as.integer(Year)) %>%
  drop_na() %>%
  filter(Age <= 90) %>%
  select(-Total) %>%
  pivot_longer(c("Female", "Male"),
               names_to = "Sex", values_to = "Mortality") %>%
  mutate(Percentile = ntile(Mortality, 100))
plotdata
```

```{r}
ggplot(plotdata, aes(x=Year,
                     y=Age,
                     fill=Percentile)) +
  geom_tile() +
  annotate(
    geom = "text",
    label = "Spanish flu",
    x = 1918,
    y = 30,
    size = 2,
    color = "white",
    hjust = 0
  ) +
  scale_fill_viridis(option="inferno", direction = -1) +
  scale_y_continuous(
    breaks = 1:9 * 10,
    label = c("","","","","","", "70", "80", "90")
  ) +
  facet_wrap(vars(Sex), ncol=1) +
  theme_classic() +
  theme(
    strip.background = element_rect(colour = NA, fill = "#fff1e6"),
    strip.text = element_text(hjust = 0),
    plot.background = element_rect(fill = "#fff1e6"),
    panel.background = element_rect(fill = "#fff1e6"),
    legend.position = "none"
  )
```

















