-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spain_maps.Rmd
420 lines (353 loc) · 11.8 KB
/
Spain_maps.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
---
title: "Mortality rate in Spain 2020"
authors: "Natalia Garcia, Ángela Gómez, Lucía Martín, Ana Solbas"
output:
html_document:
df_print: paged
---
### Libraries
```{r}
library(sf)
library(mapSpain) # https://doi.org/10.5281/zenodo.5366622, https://ropenspain.github.io/mapSpain/
library(tidyverse)
library(ggplot2)
```
## DATA
```{r}
mydata<-read.csv("Provincias_datos.csv")
mydata
```
```{r}
#Getting the total number of deaths by province differentiating between sex
totaldata_sex <- mydata[mydata$Mes == "Total",]
totaldata_sex <- totaldata_sex[totaldata_sex$Lugar != "Total",]
totaldata_sex
# getting the total number of deaths by province without sex
totaldata <- totaldata_sex[totaldata_sex$Sexo == "Total",]
totaldata
# Summary of totaldata
total_sum <- totaldata[,c(1,4)]
total_sum
```
```{r}
# Getting province codes and adding them to the summary table
cpro <- esp_dict_region_code(total_sum$Lugar, origin = "text", destination = "cpro")
total_sum$cpro <- cpro
total_sum <- total_sum[order(total_sum$cpro),] # order by cpro
total_sum
```
```{r}
# Extracting population in 2019 from the MapSpain package
census <- mapSpain::pobmun19
codelist <- mapSpain::esp_codelist
census <-
unique(merge(census, codelist[, c("cpro", "codauto")], all.x = TRUE))
census_prov <-
aggregate(cbind(pob19, men, women) ~ cpro, data = census, sum)
# Calculating the percentage of women per province
census_prov$porc_women <- census_prov$women / census_prov$pob19
census_prov$porc_women_lab <-
paste0(round(100 * census_prov$porc_women, 2), "%")
# Calculate death rate per 100.000 habitants
death_census <- merge(total_sum, census_prov)
death_census$porc_muerte <- death_census$Total / death_census$pob19
death_census$porc_muerte_lab <-
paste0(round(1000 * death_census$porc_muerte, 2))
death_census # table with all usefull information
```
## Mortality rate in 2020 (per 1000 population)
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, death_census)
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = porc_muerte), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Reds", rev = TRUE), # defining gradient of colors
n.breaks = 10,
labels = function(x) { # printing formatted labels
sprintf("%1.0f",1000 * x)
},
guide = guide_legend(title = "Mortality rate")
) +
labs( # labels and tittles
title = "Mortality rate in Spain (per 1000 population)",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./mortalityrateSpain.svg", width = 12, height = 6, device='svg', dpi=700)
```
## Total number of deaths per province in Spain in 2020
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, death_census)
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = Total), # coloring provinces by death number
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Reds", rev = TRUE), # defining gradient of colors
n.breaks = 10,
labels = function(x) { # printing formatted labels
sprintf("%1.0f", x)
},
guide = guide_legend(title = "Number of deaths")
) +
labs( # labels and tittles
title = "Total Number of deaths in Spain",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./totaldeathSpain.svg", width = 12, height = 6, device='svg', dpi=700)
```
## Population density in Spain, 2019
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, census_prov)
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = pob19), # coloring provinces by population
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Blues", rev = TRUE), # defining gradient of colors
n.breaks = 10,
labels = function(x) { # printing formatted labels
sprintf("%1.0f", x)
},
guide = guide_legend(title = "Population")
) +
labs( # labels and tittles
title = "Population density in Spain",
subtitle = "INE Data (2019)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./populationSpain.svg", width = 12, height = 6, device='svg', dpi=700)
```
## "Mortality rate in Spain (per 1000 population) in January 2020
```{r}
# Getting total number of deaths by province in January
enero <- mydata[mydata$Mes == "Enero",]
enero <- enero[enero$Lugar != "Total",]
enero <- enero[enero$Sexo == "Total",]
enero <- enero[,c(1,4)]
# Getting province codes
cpro <- esp_dict_region_code(enero$Lugar, origin = "text", destination = "cpro")
enero$cpro <- cpro
enero <- enero[order(enero$cpro),] # order by cpro
# Calcculating death rates in January
rate_enero <- merge(enero, census_prov)
rate_enero$porc_muerte <- rate_enero$Total / rate_enero$pob19
rate_enero$porc_muerte_lab <-
paste0(round(1000 * rate_enero$porc_muerte, 2), "%")
rate_enero
```
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, rate_enero)
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = porc_muerte), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Greens", rev = TRUE), # defining gradient of colors
n.breaks = 6,
limits = c(0, 1.3/1000),
labels = function(x) { # printing formatted labels
sprintf("%s",1000 * x)
},
guide = guide_legend(title = "Mortality rate")
) +
labs( # lables and titles
title = "Mortality rate in Spain (per 1000 population) in January 2020",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./mortalityJanuary.svg", width = 12, height = 6, device='svg', dpi=700)
```
## Mortality rate in Spain (per 1000 population) in April 2020
```{r}
# Getting total number of deaths by province in April
abril <- mydata[mydata$Mes == "Abril",]
abril <- abril[abril$Lugar != "Total",]
abril <- abril[abril$Sexo == "Total",]
abril <- abril[,c(1,4)]
# Getting province codes
cpro <- esp_dict_region_code(abril$Lugar, origin = "text", destination = "cpro")
abril$cpro <- cpro
abril <- abril[order(abril$cpro),] # order by cpro
# Calculating death rates in April
rate_abril <- merge(abril, census_prov)
rate_abril$porc_muerte <- rate_abril$Total / rate_abril$pob19
rate_abril$porc_muerte_lab <-
paste0(round(1000 * rate_abril$porc_muerte, 2), "%")
rate_abril
```
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, rate_abril)
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = porc_muerte), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Greens", rev = TRUE), # defining gradient of colors
n.breaks = 6,
limits = c(0, 1.3/1000),
labels = function(x) { # printing formatted labels
sprintf("%s",1000 * x)
},
guide = guide_legend(title = "Mortality rate")
) +
labs( # lables and titles
title = "Mortality rate in Spain (per 1000 population) in April 2020",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./mortalityApril.svg", width = 12, height = 6, device='svg', dpi=700)
```
## Mortality in Spain per Autonomous Community
```{r}
ccaa_mort <-read.csv("CCAA_DR_Mortality_Population.csv")
codauto <- esp_dict_region_code(ccaa_mort$CCAA, origin = "text", destination = "codauto")
ccaa_mort$codauto <- codauto
ccaa_mort<- ccaa_mort[order(ccaa_mort$codauto),] # order by cpro
ccaa_mort
```
### Mortality rate per Autonomous Comunity
```{r}
CCAA <- esp_get_ccaa()
CCAA <- merge(CCAA, ccaa_mort)
Can <- esp_get_can_box()
can_prov <- esp_get_can_provinces()
ggplot(CCAA) +
geom_sf(aes(fill =DR), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Reds", rev = TRUE), # defining gradient of colors
n.breaks = 10,
labels = function(x) { # printing formatted labels
sprintf("%1.0f", x)
},
guide = guide_legend(title = "Mortality rate")
) +
labs( # lables and titles
title = "Mortality rate in Spain (per 1000 population) in 2020 per A.C.",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./mortalityrateSpainccaa.svg", width = 12, height = 6, device='svg', dpi=700)
```
### Total number of deaths per Autonomous Community
```{r}
CCAA <- esp_get_ccaa()
CCAA <- merge(CCAA, ccaa_mort)
Can <- esp_get_can_box()
can_prov <- esp_get_can_provinces()
ggplot(CCAA) +
geom_sf(aes(fill = sum.Total), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10, "Reds", rev = TRUE), # defining gradient of colors
n.breaks = 10,
labels = function(x) { # printing formatted labels
sprintf("%1.0f", x)
},
guide = guide_legend(title = "Number of deaths")
) +
labs( # lables and titles
title = "Total Number of deaths in Spain per A.C.",
subtitle = "INE Data (2020)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./totaldeathSpainccaa.svg", width = 12, height = 6, device='svg', dpi=700)
```
## Average age in 2019 rate per Province
```{r}
prov_age <-read.csv("AvgAgeProvince2019.csv")
colnames(prov_age) <- c("prov.shortname.es", "Age")
```
Plotting
```{r}
# Plotting spain map divided by provinces
esp_prov <- esp_get_prov_siane()
esp_prov <- merge(esp_prov, prov_age, by="prov.shortname.es")
can_prov <- esp_get_can_provinces()
Can <- esp_get_can_box()
ggplot(esp_prov) +
geom_sf(aes(fill = Age), # coloring provinces by death rate
color = "grey70",
linewidth = .3,
) +
geom_sf(data = Can, color = "grey70") +
scale_fill_gradientn(
colors = hcl.colors(10,"BrwnY", rev = TRUE), # defining gradient of colors
n.breaks = 6,
limits = c(35, 51),
labels = function(x) { # printing formatted labels
sprintf("%s years", x)
},
guide = guide_legend(title = "Averate Age")
) +
labs( # lables and titles
title = "Averate Age per province in Spain at the end of 2019",
subtitle = "INE Data (2019)"
) +
geom_sf(data = can_prov) +
geom_sf(data = Can) +
theme_void() +
theme(legend.position = c(0.1, 0.6))
ggsave(file="./ageprovince.svg", width = 12, height = 6, device='svg', dpi=700)
```