-
Notifications
You must be signed in to change notification settings - Fork 2
/
mortalityRatePerYear.Rmd
158 lines (126 loc) · 5.68 KB
/
mortalityRatePerYear.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Mortality - causes of death, life expectancy per year periods in Spain and mortality in population age groups in 2020"
authors: "Natalia Garcia, Ángela Gómez, Lucía Martín, Ana Solbas"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#Import libraries----------------------------------------------
library(ggplot2)
library(dplyr)
library(stringr)
library(scales)
library(RColorBrewer)
library(ggbreak)
```
## Import data (Mortality per Year, Age and Sex)
```{r}
mortalityYear_df <- read.csv("mortalityRate.csv", header=TRUE, sep=';')
head(mortalityYear_df)
```
## Data preprocessing
```{r}
#convert numbers into english format
mortalityYear_df$Total <- as.numeric(str_replace(mortalityYear_df$Total,",","."))
mortalityYear_df$Edad <- as.numeric(mortalityYear_df$Edad)
#remove rows with NA values
mortalityYear_df <- na.omit(mortalityYear_df)
#translate Sex values to English
colnames(mortalityYear_df)[2] <- 'Sex'
mortalityYear_df <- mortalityYear_df %>%
mutate(Sex = ifelse(Sex == 'Ambos sexos', "Both",
ifelse(Sex == 'Hombres','Male','Female')))
head(mortalityYear_df)
```
## Plot mortality rate per age (2019 and 2020)
````{r}
plot_mortalityAge <- mortalityYear_df[mortalityYear_df$Funciones=='Tasa de mortalidad'
& mortalityYear_df$Sex=='Both'
& (mortalityYear_df$Periodo=='2020' |
mortalityYear_df$Periodo=='2019'),] %>%
mutate(Periodo = factor(Periodo)) %>%
ggplot( aes(x=Edad, y=Total, group=Periodo, color=Periodo)) +
geom_line(size=1.25) +
scale_color_brewer(palette="Paired") +
scale_y_continuous(trans = 'log2', breaks=c(0.01, 0.1, 1, 10, 100, 1000)) +
xlab("Age (Years)") + ylab("Death rate per 1000 inhabitants (log scale)") +
ggtitle("Death rate per age in Spain (2019-2020)") +
labs(color='Year')
plot_mortalityAge + theme_bw()
ggsave(file="./deathR_age.png", width = 8, height = 4, device='png', dpi=700)
````
##Plot death rate per age and sex (2020)
````{r}
mortalityAgeSex_df <- mortalityYear_df[mortalityYear_df$Funciones=='Tasa de mortalidad'
& mortalityYear_df$Sex!='Both'
& mortalityYear_df$Periodo=='2020',] %>%
mutate(Total= ifelse(Sex=="Male",-1*Total,Total))
plot_mortalityAgeSex <- ggplot(mortalityAgeSex_df, aes(x=Edad, y=Total, fill=Sex)) +
geom_bar(data = subset(mortalityAgeSex_df, Sex == "Female"), stat = "identity") +
geom_bar(data = subset(mortalityAgeSex_df, Sex == "Male"), stat = "identity") +
scale_x_continuous(breaks=seq(0,100,10)) +
scale_y_continuous(breaks=c(-300, -200,-100, 0,100, 200,300),
labels=c(300,200,100,0,100,200,300)) +
xlab("Age (Years)") + ylab("Death rate (per 1000 people)") +
ggtitle("Death rate per age and sex in Spain (2020)") +
coord_flip()
plot_mortalityAgeSex + theme_bw()
ggsave(file="./deathR_age_sex.png", width = 8, height = 5, device='png', dpi=700)
````
## Import data (Crude mortality per Year)
```{r}
crudeMortality_df <- read.csv("crudeMortality.csv", header=TRUE, sep=';')
crudeMortality_df$Total <- as.numeric(str_replace(crudeMortality_df$Total,",","."))
head(crudeMortality_df)
```
## Plot crude mortality rate per year
````{r}
plot_crudeMort <- crudeMortality_df %>%
ggplot( aes(x=Periodo, y=Total)) +
geom_line(size=1.25) +
scale_y_continuous(limits=c(0,10.5)) +
xlab("Year") + ylab("Crude death rate (per 1000 people)") +
ggtitle("Evolution of crude death rate in Spain")
plot_crudeMort + theme_bw()
ggsave(file="./deathR_evo.png", width = 10, height = 6, device='png', dpi=700)
````
## Import data (Total number of deaths caused by Infectious diseases by Year)
```{r}
mortalityID_df <- read.csv("causas_muerte.csv", header=TRUE, sep=',')
mortalityID_df$Total <- as.numeric(mortalityID_df$Total)
head(mortalityID_df)
```
````{r}
plot_mortalityID <- mortalityID_df[(mortalityID_df$Causa=="001-008 IEnfermedades infecciosas y parasitarias" |
mortalityID_df$Causa=="001-102 I-XXIITodas las causas") &
mortalityID_df$Provincias=="Nacional" & mortalityID_df$Sexo=="Total",] %>%
mutate(Causa = ifelse(Causa == "001-008 IEnfermedades infecciosas y parasitarias", "Infectious diseases",
"Total")) %>%
ggplot( aes(x=Periodo, y=Total, group=Causa, color=Causa)) +
geom_line(size=1.25) +
scale_color_brewer(palette="Accent") +
xlab("Year") + ylab("Total number of deaths") +
scale_y_continuous(labels = label_comma()) +
ggtitle("Evolution of the number of deaths caused by infectious diseases in Spain") +
labs(color='Cause of death')
plot_mortalityID + theme_bw()
ggsave(file="./mortality_cause.png", width = 8, height = 4, device='png', dpi=700)
````
## Life expectancy
````{r}
plot_mortalityAge <- mortalityYear_df[mortalityYear_df$Funciones=='Esperanza de vida'
& mortalityYear_df$Sex!='Both'
& mortalityYear_df$Edad==0
& mortalityYear_df$Periodo>=1975,] %>%
ggplot( aes(x=Periodo, y=Total, color=Sex)) +
geom_line(size=1.25) +
ylim(0,90) +
scale_y_break(c(20, 70)) +
scale_x_continuous(labels = function(x) round(x)) +
xlab("Year") + ylab("Life expectancy") +
ggtitle("Evolution of life expectancy in Spain")
plot_mortalityAge + theme_bw()
ggsave(file="./life_expectancy.png", width = 8, height = 4, device='png', dpi=700)
````