-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuality of life indicators.qmd
831 lines (678 loc) · 24.1 KB
/
Quality of life indicators.qmd
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
---
title: "Quality of life indicators"
format: html
editor: visual
---
# Loading Packages
```{r}
#| warning: false
#| message: false
require(conflicted)
require(restatapi)
require(giscoR)
require(data.table)
require(chron)
require(reshape2)
require(kableExtra)
require(tidyverse)
require(tmap)
require(highcharter)
require(plotly)
conflicts_prefer(dplyr::filter)
```
# Quality of life indicators - natural and living environment
<https://ec.europa.eu/eurostat/statistics-explained/index.php?title=Quality_of_life_indicators_-_natural_and_living_environment>
## Figure 1
Population reporting exposure to pollution, grime or other environmental problems, EU, 2011-2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_exposure_to_pollution,_grime_or_other_environmental_problems,>*EU*(%C2%B9),*2011-2020*(%25).png
### Retrieving data
```{r data-fig1}
#| eval: true
id1<-"ilc_mddw02"
date1 = 2011:2020
data1 = get_eurostat_data(
id1,
filters = list(incgrp = "TOTAL",
hhtyp = "TOTAL",
geo = "EU27_2020"),
date_filter = date1,
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = F,
keep_flags = T
)
data1 = data1 |>
group_by(time) |>
summarise(exp = mean(values)) |>
ungroup()
```
### Plot
```{r}
#| eval: true
# Define color
line_color <- "#B09120"
# Define title, subtitle, and caption
title <- paste0("Population reporting exposure to pollution, grime or other\nenvironmental problems, EU (\U00B9) ", date1[1], "-", date1[length(date1)])
subtitle <- "(%)"
caption <- paste0("\n(\U00B9) Estimate.",
"\nSource: Eurostat (online data code: ", id1, ")")
# Create the plot
p1 <- ggplot(data = data1, aes(x = time, y = exp, group = 1)
) +
geom_line(color = line_color,linewidth = 1) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max(data1$exp) + 2),
breaks = seq(0, max(data1$exp) + 2, 2)
) +
theme_bw() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
axis.text.x = element_text(hjust = 1.5)
) +
labs(
title = title,
subtitle = subtitle,
caption = caption
)
# Save the plot
output_file <- "quality_of_life/p1.png"
ggsave(filename = output_file, plot = p1,
width = 8, height = 6, dpi = 1000, create.dir = TRUE)
# Include the plot in the document
knitr::include_graphics(output_file)
```
## Figure 2
Population reporting exposure to pollution, grime or other environmental problems, by income situation, 2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_exposure_to_pollution,_grime_or_other_environmental_problems,>*by_income_situation,2020*(%25).png
### Retrieving data
```{r}
#| eval: true
# Define parameters
id2 <- "ilc_mddw02"
date2 <- 2020
# Get country codes for EU and EFTA
eu_cc <- get("cc", envir = .restatapi_env)$EU27_2020
efta_cc <- c("CH", "NO", "IS", "LI")
# Fetch the data from Eurostat
data2 <- get_eurostat_data(
id2,
filters = list(
incgrp = c("TOTAL", "B_MD60"),
freq = "Annual",
hhtyp = "TOTAL",
unit = "Percentage"
),
date_filter = date2,
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = F,
keep_flags = TRUE
)
# Get the data structure definition (DSD) for geographic codes
dsd <- get_eurostat_dsd(id2) |>
as.data.table() |>
filter(concept == "geo") |>
select(code, name)
# Merge the main data with DSD to get full names for geos
datafig2 <- merge(data2, dsd, by.x = "geo", by.y = "name", all.x = TRUE)
# Filter out unwanted geo codes
datafig2 <- datafig2[!code %in% c("EA19", "EA18", "EA", "EU28", "EU27_2007", "EU")]
# Clean geo column by removing unwanted text and standardizing EU names
datafig2$geo <- datafig2$geo %>%
gsub(" \\(.*\\)", "", .) %>%
gsub("^Eu.*", "EU", .)
# Order the names for EU, EFTA, and other countries
name_ord_eu <- datafig2 |>
filter(code %in% eu_cc, incgrp == "Total") |>
arrange(values) |>
pull(geo)
name_ord_efta <- datafig2 |>
filter(code %in% efta_cc, incgrp == "Total") |>
arrange(values) |>
pull(geo)
name_ord_othr <- datafig2 |>
filter(!code %in% c(efta_cc, eu_cc, "EU27_2020"), incgrp == "Total") |>
arrange(values) |>
pull(geo)
# Combine the ordered names
name_ord <- c(name_ord_othr, " ", name_ord_efta, " ", name_ord_eu, " ", "EU")
# Prepare the final dataset for plotting
datafig2 <- datafig2 |>
select(geo, incgrp, values) |>
mutate(
incgrp = recode(incgrp,
"Below 60% of median equivalised income" = "Population at-risk-of-poverty (\U00B2)")) |>
add_row(
geo = c(" ", " ", " "),
incgrp = "Total",
values = NA
)
# Factorize the geo and ind_type columns
datafig2$geo <- factor(datafig2$geo, levels = rev(name_ord))
datafig2$incgrp <- factor(datafig2$incgrp, levels = c("Total",
"Population at-risk-of-poverty (\U00B2)"))
# Recode specific geo values for the final plot
datafig2 <- datafig2 |>
mutate(
geo = recode(geo,
"France" = "France (\U00B3)",
"Luxembourg" = "Luxembourg (\U00B3)",
"Germany" = "Germany (\U00B3)(\U2075)",
"Ireland" = "Ireland (\U00B3)",
"EU" = "EU (\U00B9)")
)
```
### Plot
```{r}
#| eval: true
# Define color palette
colors <- c("#B09120", "#2644A7")
# Define title, subtitle, and caption
title <- paste0("Population reporting exposure to pollution, grime or other \nenvironmental problems, by income situation, ", date2, " (% share)")
subtitle <- "(%)"
caption <- paste0("\n(\U00B9) Estimate.",
"\n(\U00B2) People living below the national poverty threshold (60% of median equivalised income).",
"\n(\U00B3) Break in series.",
"\n(\U2074) 2019 instead of 2020.",
"\n(\U2075) Low reliability.",
"\n(\U2076) 2018 instead of 2020.",
"\nSource: Eurostat (online data code: ", id2, ")")
# Create the plot
p2 <- ggplot(data = datafig2, aes(x = geo, y = values, fill = incgrp)) +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
scale_fill_manual(values = colors) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max(data2$values, na.rm = TRUE) + 5),
breaks = seq(0, max(data2$values, na.rm = TRUE) + 5, 5)
) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 60, hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
legend.position = "bottom",
legend.title = element_blank()
) +
labs(
title = title,
subtitle = subtitle,
caption = caption
)
# Save the plot
output_file <- "quality_of_life/p2.png"
ggsave(filename = output_file, plot = p2, width = 8, height = 8, dpi = 1000)
# Include the plot in the document
knitr::include_graphics(output_file)
```
## Figure 3
Population reporting exposure to pollution, grime or other environmental problems, by degree of urbanisation, 2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_exposure_to_pollution,_grime_or_other_environmental_problems,>*by_degree_of_urbanisation,2020*(%25).png
### Retrieving data
```{r}
#| eval: true
# Define parameters
id3 <- "ilc_mddw05"
date3 <- 2020
# Get country codes for EU and EFTA
eu_cc <- get("cc", envir = .restatapi_env)$EU27_2020
efta_cc <- c("CH", "NO", "IS", "LI")
# Fetch the data from Eurostat
data3 <- get_eurostat_data(
id3,
filters = list(
incgrp = "Total",
freq = "Annual",
deg_urb = c("Cities", "Towns and suburbs", "Rural areas"),
unit = "Percentage"
),
date_filter = date3,
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = F,
keep_flags = T
)
# Get the data structure definition (DSD) for geographic codes
dsd <- get_eurostat_dsd(id3) |>
as.data.table() |>
filter(concept == "geo") |>
select(code, name)
# Merge the main data with DSD to get full names for geos
datafig3 <- merge(data3, dsd, by.x = "geo", by.y = "name", all.x = TRUE)
# Filter out unwanted geo codes
datafig3 <- datafig3[!code %in% c("EA19", "EA18", "EA", "EU28", "EU27_2007", "EU")]
# Clean geo column by removing unwanted text and standardizing EU names
datafig3$geo <- datafig3$geo %>%
gsub(" \\(.*\\)", "", .) %>%
gsub("^Eu.*", "EU", .)
# Order the names for EU, EFTA, and other countries
name_ord_eu <- datafig3 |>
filter(code %in% eu_cc, deg_urb == "Cities") |>
arrange(values) |>
pull(geo)
name_ord_efta <- datafig3 |>
filter(code %in% efta_cc, deg_urb == "Cities") |>
arrange(values) |>
pull(geo)
name_ord_othr <- datafig3 |>
filter(!code %in% c(efta_cc, eu_cc, "EU27_2020"), deg_urb == "Cities") |>
arrange(values) |>
pull(geo)
# Combine the ordered names
name_ord <- c(name_ord_othr, " ", name_ord_efta, " ", name_ord_eu, " ", "EU")
# Prepare the final dataset for plotting
datafig3 <- datafig3 |>
select(geo, deg_urb, values) |>
add_row(
geo = c(" ", " ", " "),
deg_urb = "Cities",
values = NA
)
# Factorize the geo and deg_urb columns
datafig3$geo <- factor(datafig3$geo, levels = rev(name_ord))
datafig3$deg_urb <- factor(datafig3$deg_urb, levels = c("Cities", "Towns and suburbs", "Rural areas"))
# Recode specific geo values for the final plot
datafig3 <- datafig3 |>
mutate(
geo = recode(geo,
"France" = "France (\U00B3)",
"Luxembourg" = "Luxembourg (\U00B3)",
"Germany" = "Germany (\U00B3)(\U2075)",
"Ireland" = "Ireland (\U00B3)",
"EU" = "EU (\U00B9)")
)
```
### Plot
```{r}
#| eval: true
# Define color palette
colors <- c("#B09120", "#2644A7", "#E04040")
# Define title, subtitle, and caption
title <- paste0("Population reporting exposure to pollution, grime or other \nenvironmental problems, by degree of urbanisation, ", date3)
subtitle <- "(%)"
caption <- paste0("\n(\U00B9) Estimate.",
"\n(\U00B2) Break in series.",
"\n(\U00B3) 2019 instead of 2020.",
"\n(\U2074) Low reliability.",
"\n(\U2075) 2018 instead of 2020.",
"\nSource: Eurostat (online data code: ", id3, ")")
# Create the plot
p3 <- ggplot(data = datafig3, aes(x = geo, y = values, fill = deg_urb)) +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
scale_fill_manual(values = colors) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max(datafig3$values, na.rm = TRUE) + 5),
breaks = seq(0, max(datafig3$values, na.rm = TRUE) + 5, 5)
) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 60, hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
legend.position = "bottom",
legend.title = element_blank()
) +
labs(
title = title,
subtitle = subtitle,
caption = caption
)
# Save the plot
output_file <- "quality_of_life/p3.png"
ggsave(filename = output_file, plot = p3, width = 8, height = 8, dpi = 1000)
# Include the plot in the document
knitr::include_graphics(output_file)
```
## Figure 4
Population reporting noise from neighbours or from the street, EU, 2011-2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_noise_from_neighbours_or_from_the_street,>*EU*(%C2%B9)\_,*2011-2020*(%25).png
### Retrieving data
```{r}
#| eval: true
# Define parameters
id4="ilc_mddw01"
date4=2011:2020
# Fetch the data from Eurostat
data4 <- get_eurostat_data(
id4,
filters = list(
ihhtyp="TOTAL",
incgrp="TOTAL",
geo="EU27_2020",
hhtyp="TOTAL"
),
date_filter = date4,
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = F,
keep_flags = T
)
```
### Plot
```{r}
#| eval: true
# Define color
line_color <- "#B09120"
# Define title, subtitle, and caption
title=paste0("Population reporting noise from neighbours or from the street, \n EU(\U00B9)",date4[1],"-",date4[length(dates)])
subtitle=paste("(%)")
caption=paste0("(\U00B9) Estimate \n Source: Eurostat (online data code: ",id4, ")")
# Create the plot
p4 <- ggplot(data = data4, aes(x = time, y = values, group = 1)
) +
geom_line(color = line_color,linewidth = 1) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max(data4$values) + 2),
breaks = seq(0, max(data4$values) + 2, 2)
) +
theme_bw() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
axis.text.x = element_text(hjust = 1.5)
) +
labs(
title = title,
subtitle = subtitle,
caption = caption
)
# Save the plot
output_file <- "quality_of_life/p4.png"
ggsave(filename = output_file, plot = p4,
width = 8, height = 6, dpi = 1000, create.dir = TRUE)
# Include the plot in the document
knitr::include_graphics(output_file)
```
## Figure 5
Population reporting noise from neighbours or from the street, by income situation, 2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_noise_from_neighbours_or_from_the_street,_by_income_situation,>*2020*(%25).png
### Retrieving data
```{r}
#| eval: true
# Define parameters
id5="ilc_mddw01"
date5=2018:2020
# Get country codes for EU and EFTA
eu_cc<-get("cc",envir=.restatapi_env)$EU27_2020
efta_cc<-c("CH","NO","IS","LI")
# Fetch the data from Eurostat
data5 <- get_eurostat_data(
id5,
filters = list(
incgrp = c("TOTAL", "B_MD60"),
freq = "Annual",
hhtyp = "TOTAL",
unit = "Percentage"
),
date_filter = date5,
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = F,
keep_flags = TRUE
)
# Filter the data
data5 <- data5 |>
filter(unit == "Percentage") |>
filter(time == 2020 | (time == 2019 & geo %in% c("Poland")) | (time == 2018 & geo %in% c("Iceland")) )
# Get the data structure definition (DSD)
dsd <- get_eurostat_dsd(id5) |>
as.data.table() |>
filter(concept == "geo") |>
select(code, name)
# Merge data with the DSD
datafig5 <- merge(data5, dsd, by.x = "geo", by.y = "name", all.x = TRUE)
# Filter out geo codes
datafig5 <- datafig5[!code %in% c("EA19", "EA18", "EA", "EU28", "EU27_2007", "EU")]
# Clean geo column
datafig5$geo <- gsub(" \\(.*\\)", "", datafig5$geo)
datafig5$geo <- gsub("^Eu.*", "EU", datafig5$geo)
# Order the names for EU, EFTA, and other countries
name_ord_eu <- datafig5 |>
filter(code %in% eu_cc, incgrp == "Total") |>
arrange(values) |>
pull(geo)
name_ord_efta <- datafig5 |>
filter(code %in% efta_cc, incgrp == "Total") |>
arrange(values) |>
pull(geo)
name_ord_othr <- datafig5 |>
filter(!code %in% c(efta_cc, eu_cc, "EU27_2020"), incgrp == "Total") |>
arrange(values) |>
pull(geo)
# Combine the ordered names
name_ord <- c(name_ord_othr, " ", name_ord_efta, " ", name_ord_eu, " ", "EU")
# Prepare the final dataset for plotting
datafig5 <- datafig5 |>
select(geo, incgrp, values) |>
mutate(
incgrp = recode(incgrp,
"Below 60% of median equivalised income" = "Population at-risk-of-poverty (\U00B2)")) |>
add_row(
geo = c(" ", " ", " "),
incgrp = "Total",
values = NA
)
# Factorize the geo and ind_type columns
datafig5$geo <- factor(datafig5$geo, levels = rev(name_ord))
datafig5$incgrp <- factor(datafig5$incgrp, levels = c("Total",
"Population at-risk-of-poverty (\U00B2)"))
datafig5 <- datafig5 |>
mutate(
geo = recode(geo,
"EU" = "EU (\U00B9)",
"Germany" = "Germany (\U00B3)(\U2074)",
"France" = "France (\U00B3)",
"Luxembourg" = "Luxembourg (\U00B3)",
"Poland"="Poland(\U2075)",
"Iceland" = "Iceland (\U2076)")
)
```
### Plot
```{r}
#| eval: true
# Define color palette
colors <- c("#B09120", "#2644A7")
# Define title, subtitle, and caption
title=paste0("Population reporting noise from neighbours or from the street by, \n income situation, ", tail(5, n = 1))
subtitle=paste("(%)")
caption=paste0("(\U00B9) Estimate.
\n(\U00B2) People living below the national poverty treshold (60% of median equivalised income).
\n(\U00B3) Break in series.
\n(\U2074) Low reliability.
\n(\U2075) 2019 instead of 2020.
\n(\U2076) 2018 instead of 2020.
\nSource: Eurostat (online data code: ",id5, ")")
# Create the plot
p5 <- ggplot(data = datafig5, aes(x = geo, y = values, fill = incgrp)) +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
scale_fill_manual(values = colors) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max(data5$values, na.rm = TRUE) + 5),
breaks = seq(0, max(data5$values, na.rm = TRUE) + 5, 5)
) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 60, hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
legend.position = "bottom",
legend.title = element_blank()
) +
labs(
title = title,
subtitle = subtitle,
caption = caption
)
# Save the plot
output_file <- "quality_of_life/p5.png"
ggsave(filename = output_file, plot = p5, width = 8, height = 8, dpi = 1000)
# Include the plot in the document
knitr::include_graphics(output_file)
```
## Figure 6
Population reporting noise from neighbours or from the street, by degree of urbanisation, 2020 <https://ec.europa.eu/eurostat/statistics-explained/index.php?title=File:Population_reporting_noise_from_neighbours_or_from_the_street,_by_degree_of_urbanisation,>*2020*(%25).png
### Retrieving data
```{r}
id="ilc_mddw04"
date=2020
eu_cc<-get("cc",envir=.restatapi_env)$EU27_2020
efta_cc<-c("CH","NO","IS")
data <- get_eurostat_data(
id,
filters = list(
incgrp = c("TOTAL"),
freq = "A",
deg_urb = c("DEG1", "DEG2", "DEG3"),
unit = "PC"
),
label = TRUE,
ignore.case = TRUE,
exact_match = FALSE,
perl = TRUE,
stringsAsFactors = FALSE,
force_local_filter = TRUE,
verbose = FALSE
)
dsd<-as.data.table(get_eurostat_dsd(id))[concept=="geo",c("code","name")]
data<-merge(data,
dsd,
by.x="geo",
by.y="name",
all.x=T,
all.y = F)
data<- data[!code %in% c("EA19", "EA18", "EA", "EU28", "EU27_2007", "EU")]
data$geo<-gsub(" \\(.*\\)","",data$geo)
data$geo<-gsub("^Eu.*","EU",data$geo)
# Order the names within the EU
ordered_name <- data[(code %in% eu_cc) & (deg_urb == "Cities")]
ordered_name_eu <- ordered_name[order(-values)]$geo
# Order the names within EFTA
ordered_name <- data[(code %in% efta_cc) & (deg_urb == "Cities")]
ordered_name_efta <- ordered_name[order(-values)]$geo
# Order the names for other countries
ordered_name <- data[!(code %in% c(efta_cc, eu_cc, "EU27_2020")) & (deg_urb == "Cities")]
ordered_name_other <- ordered_name[order(-values)]$geo
# Combine the ordered names into a single vector
ordered_name <- c("EU", " ", ordered_name_eu, " ", ordered_name_efta, " ", ordered_name_other)
# Ensure ordered_name contains unique values
ordered_name <- unique(ordered_name)
ordered_name <- unique(ordered_name)
data$geo <- factor(data$geo, levels = rev(ordered_name))
data <- data %>%
select(geo, deg_urb, values) %>%
mutate(
geo = factor(geo, levels = rev(ordered_name))
)
data <- data |>
select(geo, deg_urb, values) |>
(\(df){
dt_sep <- data.table::data.table(
geo = c(" ", " ", " "),
deg_urb = rep("Cities", 3),
values = rep(NA, 3)
)
rbind(df, dt_sep)
})()
data$geo <- factor(data$geo,
levels = ordered_name)
data$deg_urb <- factor(data$deg_urb,
levels = c("Cities",
"Towns and suburbs",
"Rural areas"))
```
### Plot
```{r}
p6=ggplot(data = data, aes(x = geo, y = values, fill = deg_urb)) +
theme_minimal() +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
scale_fill_manual(values = c("#B09120", "#2644A7", "#E04040"),
labels = c("Cities", "Towns and Suburbs", "Rural Areas")) +
scale_y_continuous(expand = c(0, 0),
limits = c(0, max(data$values, na.rm = TRUE) + 5),
breaks = seq(0, max(data$values, na.rm = TRUE) + 5, 5)) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 60, hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.line.x = element_line(colour = "black", linetype = "solid"),
plot.caption = element_text(hjust = 0),
plot.title = element_text(hjust = 0, face = "bold"),
legend.position = "bottom",
legend.title = element_blank()
) +
labs(
title = "Population reporting noise from neighbours or from the street, by degree of urbanisation, 2020",
caption = "\nEU: Estimate\nDE: Low Reliability\nPL: 2019 instead of 2020\nIS: 2018 instead of 2020\nSource: Eurostat (online data code: ilc_mddw04)"
) +
theme(
legend.title = element_blank(), # Remove legend title
legend.position = "bottom", # Place legend below the plot
plot.caption = element_text(hjust = 0, face = "italic")
)
# Save the plot
output_file <- "quality_of_life/p6.png"
ggsave(filename = output_file, plot = p6, width = 8, height = 8, dpi = 1000)
# Include the plot in the document
knitr::include_graphics(output_file)
```