This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(readxl) | |
library(janitor) | |
library(ggridges) | |
# let's download the table | |
download.file("https://www.istat.it/it/files//2020/03/Tavola-sintetica-decessi.xlsx", | |
"Tavola-sintetica-decessi.xlsx") | |
# we prefer to work with clean names | |
deaths <- read_excel("Tavola-sintetica-decessi.xlsx", | |
skip = 1) %>% | |
clean_names() %>% | |
rename(m_2019 = m_7,f_2019 = f_8,tot_2019 = m_f_9, | |
m_2020 = m_10,f_2020 = f_11,tot_2020 = m_f_12) | |
# we define a pipe to calculate the change from 2019 to 2020 | |
get_tots <- . %>% | |
summarise(m_tot_2019 = sum(m_2019), | |
f_tot_2019 = sum(f_2019), | |
tot_tot_2019 = sum(tot_2019), | |
m_tot_2020 = sum(m_2020), | |
f_tot_2020 = sum(f_2020), | |
tot_tot_2020 = sum(tot_2020)) | |
# we define a pipe to calculate the total deaths | |
get_changes <- . %>% | |
mutate(m_change = m_tot_2020 / m_tot_2019, | |
f_change = f_tot_2020 / f_tot_2019, | |
tot_change = tot_tot_2020 / tot_tot_2019) %>% | |
select(starts_with("nome"), ends_with("change")) | |
# we create three ridges plot (one for municipalities, one for provincces, one for regions) | |
comune <- deaths %>% | |
mutate(m_change = m_2020 / m_2019, | |
f_change = f_2020 / f_2019, | |
tot_change = tot_2020 / tot_2019) %>% | |
select(starts_with("nome"), ends_with("change")) %>% | |
pivot_longer(ends_with("change"), names_to = "Categoria", values_to = "Variazione") %>% | |
filter(Variazione > 0, Variazione < Inf) %>% | |
ggplot(aes(x = Variazione, y = Categoria)) + | |
geom_density_ridges() + | |
scale_x_log10() + | |
theme_minimal() + | |
labs(subtitle = "Per Comune", | |
y = "Quanti comuni?", | |
x = "Variazione") | |
provincia <- deaths_by_province %>% | |
get_changes() %>% | |
pivot_longer(ends_with("change"), names_to = "Categoria", values_to = "Variazione") %>% | |
filter(Variazione < 100) %>% | |
ggplot(aes(x = Variazione, y = Categoria)) + | |
scale_x_log10() + | |
geom_density_ridges() + | |
theme_minimal() + | |
labs(subtitle = "Per Provincia", | |
y = "Quante provincie?", | |
x = "Variazione") | |
regione <- deaths_by_region %>% | |
get_changes() %>% | |
pivot_longer(ends_with("change"), names_to = "Categoria", values_to = "Variazione") %>% | |
filter(Variazione < 100) %>% | |
ggplot(aes(x = Variazione, y = Categoria)) + | |
scale_x_log10() + | |
geom_density_ridges() + | |
theme_minimal() + | |
labs(subtitle = "Per Regione", | |
y = "Quante regioni?", | |
x = "Variazione") | |
# and we plot them together | |
library(patchwork) | |
(comune / provincia / regione) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment