-
Notifications
You must be signed in to change notification settings - Fork 7
/
design-unhcr-charts.Rmd
1560 lines (1400 loc) · 37.6 KB
/
design-unhcr-charts.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
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Designing UNHCR Branded Charts using unhcrthemes"
---
```{r, include = FALSE}
library(ragg)
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
collapse = TRUE,
comment = "#>",
dev = "ragg_png",
fig.retina = 2,
dpi = 300
)
```
Data visualization is a powerful tool for conveying information and insights from complex datasets. The [United Nations High Commissioner for Refugees](https://www.unhcr.org/) (UNHCR) provides crucial [refugee statistics](https://www.unhcr.org/refugee-statistics/download/) that can be presented in an informative and visually appealing manner. The `{unhcrthemes}` package, in combination with the `{refugees}` package and the versatile `{ggplot2}` library, offers an excellent solution to create UNHCR-branded charts with ease.
## Setup
Before we start creating UNHCR branded charts, make sure you have the **necessary packages installed**:
```{r install, eval=FALSE}
# Install CRAN packages
packages <- c("refugees", "tidyverse", "scales", "ragg", "pak")
install.packages(setdiff(packages, rownames(installed.packages())))
# Install unhcrthemes with pak
pak::pkg_install("unhcr-dataviz/unhcrthemes")
```
**Load the packages** into your R environment:
```{r setup}
# Load required packages
library(unhcrthemes)
library(refugees)
library(tidyverse)
```
Some packages installed above will be loaded using the `::` notation to make the code and intentions clearer.
For this tutorial, we'll use statistics provided by UNHCR. The refugee package allows us to access and work with this data. You can find more information on the datasets available and the use of the `{refugees}` package in [this blog post](https://www.unhcr.org/refugee-statistics/insights/explainers/refugees-r-package.html).
```{r caption}
# Set caption for all charts, as we will only use the refugees package
caption <- "Source: Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
```
## Bar chart
### Single bar chart
Let's start by creating a simple bar chart to visualize the top 10 refugee and other people in need of international protection by country of origin in 2022.
**Preparing the data:**
```{r bar-data}
# Keep only 2022 records, Sum refugees and oip,
# Keep only top 10, Wrap long label
bar_df <- refugees::population |>
filter(year == 2022) |>
summarise(
refugees = sum(refugees, na.rm = TRUE) + sum(oip, na.rm = TRUE),
.by = coo_name
) |>
slice_max(order_by = refugees, n = 10) |>
mutate(coo_name = str_wrap(coo_name, 25))
```
**Plot:**
```{r bar-plot, out.width="100%"}
# Set title and subtitle
title_bar <- "Refugees and other people in need of international protection"
subtitle_bar <- "By country of origin at the end of 2022"
# Plot
ggplot(
bar_df,
aes(
x = refugees,
y = reorder(coo_name, refugees)
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = 0.8
) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
breaks = seq(0, 7e6, by = 1e6),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
labs(
title = title_bar,
subtitle = subtitle_bar,
caption = caption,
x = "Number of people"
) +
theme_unhcr(grid = "X", axis = "Y", axis_title = "X")
```
**Plot with labels:**
```{r bar-label, out.width="100%"}
# Plot with labels
ggplot(
bar_df,
aes(
x = refugees,
y = reorder(coo_name, refugees)
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = 0.8
) +
geom_text(
aes(
label = scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = .1
)(refugees)
),
hjust = -.2
) +
scale_x_continuous(expand = expansion(mult = c(0, .1))) +
labs(
title = title_bar,
subtitle = subtitle_bar,
caption = caption
) +
theme_unhcr(grid = FALSE, axis = "Y", axis_text = "Y", axis_title = FALSE)
```
### Grouped bar chart
Let’s compare the refugee population originating in each UNHCR region for both 2012 and 2022. We will use a grouped bar chart.
**Preparing the data:**
```{r group-bar-data}
# Keep 2012 and 2022 records, Load and join region names,
# Group by year and region and sum refugees,
# Wrap long label, Remove NA
group_bar_df <- refugees::population |>
filter(year == 2012 | year == 2022) |>
left_join(refugees::countries, by = c("coo_iso" = "iso_code")) |>
group_by(year, unhcr_region) |>
summarise(refugees = sum(refugees, na.rm = TRUE)) |>
mutate(unhcr_region = str_wrap(unhcr_region, 20)) |>
filter(!is.na(unhcr_region))
```
**Plot:**
```{r group-bar-plot, out.width="100%"}
# Set title and subtitle
title_group_bar <- paste0(
"Comparison of the total refugee population in <span style='color:",
unhcr_pal(n = 2, "pal_blue")[1],
"'>2012</span> and <span style='color:",
unhcr_pal(n = 2, "pal_blue")[2],
"'>2022</span> by region of origin"
)
# Plot
ggplot(
group_bar_df,
aes(
x = refugees,
y = fct_rev(as_factor(unhcr_region)),
fill = as_factor(year)
)
) +
geom_col(
width = .7,
position = position_dodge(.8),
) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_fill_unhcr_d(direction = -1) +
labs(
title = title_group_bar,
caption = caption,
x = "Number of people"
) +
theme_unhcr(
grid = "X", axis = "Y", axis_title = "X",
legend = FALSE
)
```
**Plot with labels:**
```{r group-bar-label, out.width="100%"}
# Plot with labels
ggplot(
group_bar_df,
aes(
x = refugees,
y = fct_rev(as_factor(unhcr_region)),
fill = as_factor(year)
)
) +
geom_col(
width = .7,
position = position_dodge(.8),
) +
geom_text(
aes(
label = scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = .1
)(refugees)
),
position = position_dodge(.8),
hjust = -.2
) +
scale_x_continuous(expand = expansion(mult = c(0, .1))) +
scale_fill_unhcr_d(direction = -1) +
labs(
title = title_group_bar,
caption = caption,
) +
theme_unhcr(
grid = FALSE, axis = "Y", axis_title = FALSE,
axis_text = "Y", legend = FALSE
)
```
### Stacked bar chart
Let’s make a stacked bar chart comparing the top 10 countries of origin for all people displaced across borders, by type, in 2022.
**Preparing the data:**
```{r stack-bar-data}
# Keep only 2022 records, Summarize popolutaion type by coo,
# Keep only top 10, Pivot for ggplot2,
# Wrap long label and make factor of pop_type, Remove 0
stack_bar_df <- refugees::population |>
filter(year == 2022) |>
summarise(
ref = sum(refugees, na.rm = TRUE),
oip = sum(oip, na.rm = TRUE),
asy = sum(asylum_seekers, na.rm = TRUE),
tot = ref + oip + asy,
.by = coo_name
) |>
slice_max(order_by = tot, n = 10) |>
pivot_longer(
cols = c(ref, oip, asy),
names_to = "pop_type",
values_to = "pop_num"
) |>
mutate(
coo_name = str_wrap(coo_name, 25),
pop_type = factor(pop_type, levels = c("oip", "asy", "ref"))
) |>
select(-tot) |>
filter(pop_num != 0)
```
**Plot:**
```{r stack-bar-plot, out.width="100%"}
# Set title and subtitle
title_stack_bar <- "Top 10 country of origin of people displaced across borders in 2022"
# Plot
ggplot(
stack_bar_df,
aes(
x = pop_num,
y = fct_rev(as_factor(coo_name)),
fill = pop_type
)
) +
geom_col(
width = 0.8
) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
breaks = seq(0, 7e6, by = 1e6),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_poc", nmax = 9,
order = c(9, 3, 1), labels = c("Other people in need of international protection", "Asylum-seekers", "Refugees"),
guide = guide_legend(reverse = TRUE)
) +
labs(
title = title_stack_bar,
caption = caption,
x = "Number of people"
) +
theme_unhcr(
grid = "X", axis = "Y", axis_title = "X",
legend_text_size = 9
)
```
**Plot with labels:**
```{r stack-bar-label, out.width="100%"}
# Add subtitle instead of legend
subtitle_stack_bar <- paste0(
"People displaced across borders asr composed of <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[1],
"'>Refugees</span>, <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[3],
"'>Asylum-seekers</span> and <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[9],
"'>Other people in need of international protection</span>."
)
# Plot with labels
ggplot(
stack_bar_df,
aes(
x = pop_num,
y = fct_rev(as_factor(coo_name)),
fill = pop_type,
group = pop_type
)
) +
geom_col(width = 0.8) +
geom_text(
aes(
label = case_when(
pop_num >= 1e6 ~ scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = .1
)(pop_num),
TRUE ~ scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = 1
)(pop_num)
),
hjust = case_when(
pop_num < 2e5 & pop_type == "asy" ~ -0.25,
pop_num < 5e5 & pop_type == "asy" ~ -0.55,
pop_num < 5e5 & pop_type == "ref" ~ 0.15,
TRUE ~ .5
),
color = case_when(
pop_num < 5e5 & pop_type == "asy" ~ "#1a1a1a",
TRUE ~ "#ffffff"
)
),
position = position_stack(vjust = .5),
size = 9 / .pt
) +
scale_x_continuous(
expand = expansion(mult = c(0, .1))
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_poc", nmax = 9,
order = c(9, 3, 1)
) +
scale_color_identity() +
labs(
title = title_stack_bar,
subtitle = subtitle_stack_bar,
caption = caption
) +
theme_unhcr(
grid = FALSE, axis = "Y", axis_title = FALSE,
axis_text = "y", legend = FALSE
)
```
### Population pyramid
This type of chart is perfect to represent age & gender disagregation. Let's make a population pyramid of populations protected and/or assisted by UNHCR in 2022.
**Preparing the data:**
```{r pyramid-data}
# Filter 2022 records, Keep only relevant pop_type,
# Summarise age group, pivot and separate age/gender,
# Calculate percent, put ages in a factor
pyramid_df <- refugees::demographics |>
filter(year == 2022 &
pop_type %in% c("REF", "ASY", "IDP", "OIP", "RDP", "RET", "STA", "OOC")) |>
summarise(
"male 0-4" = sum(m_0_4, na.rm = TRUE),
"male 5-11" = sum(m_5_11, na.rm = TRUE),
"male 12-17" = sum(m_12_17, na.rm = TRUE),
"male 18-59" = sum(m_18_59, na.rm = TRUE),
"male 60+" = sum(m_60, na.rm = TRUE),
"female 0-4" = sum(f_0_4, na.rm = TRUE),
"female 5-11" = sum(f_5_11, na.rm = TRUE),
"female 12-17" = sum(f_12_17, na.rm = TRUE),
"female 18-59" = sum(f_18_59, na.rm = TRUE),
"female 60+" = sum(f_60, na.rm = TRUE)
) |>
pivot_longer(
cols = everything(),
names_sep = " ",
names_to = c(".value", "ages")
) |>
mutate(
male_p = round(male / (sum(female) + sum(male)), 2),
female_p = round(female / (sum(female) + sum(male)), 2),
ages = factor(ages, levels = c("0-4", "5-11", "12-17", "18-59", "60+"))
)
```
**Plot:**
```{r pyramid-plot, out.width="100%"}
# Plot
ggplot(
pyramid_df,
aes(y = ages)
) +
geom_col(aes(x = -male_p),
fill = unhcr_pal(n = 1, "pal_navy"),
width = .7
) +
geom_col(aes(x = female_p),
fill = unhcr_pal(n = 1, "pal_blue"),
width = .7
) +
geom_text(
aes(
x = -male_p,
label = scales::percent(abs(male_p))
),
hjust = 1.25,
color = unhcr_pal(n = 1, "pal_navy"),
fontface = "bold"
) +
geom_text(
aes(
x = female_p,
label = scales::percent(abs(female_p))
),
hjust = -.25,
color = unhcr_pal(n = 1, "pal_blue"),
fontface = "bold"
) +
scale_x_continuous(expand = expansion(c(0.2, 0.2))) +
coord_cartesian(clip = "off") +
labs(
title = "Demographics of populations protected and/or assisted by UNHCR at the end of 2022",
caption = "Note: Age and sex disaggregated data is not available for all countries of asylum. Figures do not add up to 100 per cent due to rounding.<br>Source: UNHCR Refugee Data Finder - © UNHCR, The UN Refugee Agency"
) +
annotate("text", x = -.01, y = 5.65, label = "Male", size = 12 / .pt, family = "Lato", fontface = "bold", hjust = 1, color = unhcr_pal(n = 1, "pal_navy")) +
annotate("text", x = .01, y = 5.65, label = "Female", size = 12 / .pt, family = "Lato", fontface = "bold", hjust = 0, color = unhcr_pal(n = 1, "pal_blue")) +
theme_unhcr(
axis_text = "Y", axis_title = FALSE, grid = FALSE,
plot_title_margin = 24
)
```
## Column chart
### Single column chart
A column chart is quite similar to a bar chart, but the bars are vertical. Let's make a column chart of the last 10 years of total refugee population.
**Preparing the data:**
```{r column-data}
# Keep only 2013-2022 records, Sum refugees, Make year factor
column_df <- refugees::population |>
filter(year >= 2013) |>
summarise(
refugees = sum(refugees, na.rm = TRUE),
.by = year
) |>
mutate(year = as_factor(year))
```
**Plot:**
```{r column-plot, out.width="100%"}
# Set title
title_column <- "Total number of refugees | 2013-2022"
# Plot
ggplot(
column_df,
aes(x = year, y = refugees)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = 0.8
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1)),
breaks = seq(0, 30e6, by = 5e6),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
labs(
title = title_column,
caption = caption,
Y = "Number of people"
) +
theme_unhcr(grid = "Y", axis = "X", axis_title = FALSE)
```
**Plot with labels:**
```{r column-label, out.width="100%"}
# Plot with labels
ggplot(
column_df,
aes(x = year, y = refugees)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = 0.8
) +
geom_text(
aes(
label = scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = .1
)(refugees)
),
vjust = -.4
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1)),
breaks = seq(0, 30e6, by = 5e6),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
labs(
title = title_column,
caption = caption,
Y = "Number of people"
) +
theme_unhcr(grid = FALSE, axis = "X", axis_title = FALSE, axis_text = "X")
```
### Grouped column chart
Let’s compare the 2018-2022 refugee population, originating in the three UNHCR regions in Africa. We will use a grouped column chart.
**Preparing the data:**
```{r group-col-data}
# Keep 2018-2022 records, Load and join region names,
# Group by year and region and sum refugees,
# Keep only regions in Africa
group_col_df <- refugees::population |>
filter(year >= 2018 & year <= 2022) |>
left_join(refugees::countries, by = c("coo_iso" = "iso_code")) |>
group_by(year, unhcr_region) |>
summarise(refugees = sum(refugees, na.rm = TRUE)) |>
ungroup() |>
filter(unhcr_region %in% c(
"East and Horn of Africa",
"Southern Africa",
"West and Central Africa"
))
```
**Plot:**
```{r group-col-plot, out.width="100%"}
# Set title
region_name <- group_col_df |>
distinct(unhcr_region) |>
pull(unhcr_region)
title_group_col <- "Refugees from the different regions of Africa"
subtitle_group_col <- paste0(
"Number of refugees in <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[1], "'>",
region_name[1], "</span>, <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[2], "'>",
region_name[2], "</span> and <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[3], "'>",
region_name[3], "</span> at the end of 2018-2022"
)
# Plot
ggplot(
group_col_df,
aes(
x = year,
y = refugees,
fill = unhcr_region
)
) +
geom_col(
width = .7,
position = position_dodge(.8),
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_region", nmax = 3
) +
labs(
title = title_group_col,
subtitle = subtitle_group_col,
caption = caption,
x = "Number of people"
) +
theme_unhcr(
grid = "Y", axis = "X", axis_title = FALSE,
legend = FALSE
)
```
**Plot with labels:**
```{r group-col-label, out.width="100%"}
# Plot with labels
ggplot(
group_col_df,
aes(
x = year,
y = refugees,
fill = unhcr_region
)
) +
geom_col(
width = .7,
position = position_dodge(.8),
) +
geom_text(
aes(
label = scales::label_number(
scale = .000001,
accuracy = .1,
suffix = "M"
)(refugees)
),
position = position_dodge(.8),
vjust = -.3
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1))
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_region", nmax = 3
) +
labs(
title = title_group_col,
subtitle = subtitle_group_col,
caption = caption,
x = "Number of people"
) +
theme_unhcr(
grid = FALSE, axis_text = "x", axis = "X",
axis_title = FALSE, legend = FALSE
)
```
### Stacked column chart
Let's plot the forcibly displaced people by type of population from 2013 to 2022.
**Preparing the data:**
```{r stack-col-data}
# UNHCR data: Keep 2013-2022 records, pivot longer pop_type,
# Summarise by year and pop_type, drop 0s, rename pop_type
unhcr_df <- refugees::population |>
filter(year >= 2013 & year <= 2022) |>
pivot_longer(
cols = c(refugees, oip, asylum_seekers),
names_to = "pop_type",
values_to = "pop_num"
) |>
summarise(pop_num = sum(pop_num, na.rm = TRUE), .by = c(year, pop_type)) |>
filter(pop_num != 0)
# IDMC data: Keep 2013-2022 records,
# Summarise by year, create pop_type
idmc_df <- refugees::idmc |>
filter(year >= 2013 & year <= 2022) |>
summarise(pop_num = sum(total, na.rm = TRUE), .by = year) |>
mutate(pop_type = "idmc")
# UNRWA data: Keep 2013-2022 records,
# Summarise by year, create pop_type
unrwa_df <- refugees::unrwa |>
filter(year >= 2013 & year <= 2022) |>
summarise(pop_num = sum(total, na.rm = TRUE), .by = year) |>
mutate(pop_type = "unrwa")
# Append dataframes together, level factor pop_type, make year factor
stack_col_df <- bind_rows(unhcr_df, idmc_df, unrwa_df) |>
mutate(
pop_type = factor(pop_type, levels = c(
"idmc",
"refugees",
"unrwa",
"oip",
"asylum_seekers"
)),
year = as_factor(year)
)
```
**Plot:**
```{r stack-col-plot, out.width="100%", fig.height=6}
# Set title
stack_col_title <- "People forced to flee worldwide | 2013-2022"
# Plot
ggplot(
stack_col_df,
aes(
x = year,
y = pop_num,
fill = pop_type
)
) +
geom_col(
width = .7,
position = position_stack(reverse = TRUE)
) +
scale_y_continuous(
expand = expansion(mult = c(0, 0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
breaks = scales::pretty_breaks(n = 5)
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_poc", nmax = 9,
order = c(4, 1, 2, 9, 3),
labels = c(
"IDPs",
"Refugees under\nUNHCR's mandate",
"Refugees under\nUNRWA's mandate",
"Other people in need\nof international protection",
"Asylum-seekers"
)
) +
labs(
title = stack_col_title,
caption = caption,
) +
theme_unhcr(
grid = "Y", axis = "X", axis_title = FALSE,
legend_text_size = 9
) +
guides(fill = guide_legend(ncol = 3))
```
**Plot with category in subtitle:**
```{r stack-col-label, out.width="100%"}
# Set subtitle
stack_col_subtitle <- paste0(
"By: <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[4],
"'>IDPs</span>, <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[1],
"'>Refugees under UNHCR's mandate</span>, <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[2],
"'>Refugees under UNWRA's mandate</span>,<br><span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[9],
"'>Other people in need of international protection</span> and <span style='color:",
unhcr_pal(n = 9, "pal_unhcr_poc")[3],
"'>Asylum-seekers</span>."
)
# Plot with labels
ggplot(
stack_col_df,
aes(
x = year,
y = pop_num,
fill = pop_type
)
) +
geom_col(
width = .7,
position = position_stack(reverse = TRUE)
) +
scale_y_continuous(
expand = expansion(mult = c(0, 0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
breaks = scales::pretty_breaks(n = 5)
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_poc", nmax = 9,
order = c(4, 1, 2, 9, 3)
) +
labs(
title = stack_col_title,
subtitle = stack_col_subtitle,
caption = caption,
) +
theme_unhcr(
grid = "Y", axis = "X", axis_title = FALSE, legend = FALSE
)
```
### 100% stacked column chart
Let’s create a 100% stacked column chart showing the total refugee population for the last 10 years, by the each of the three UNHCR regions in Africa.
**Preparing the data:**
```{r percent-stack-data}
# Keep 2013-2022 records, Load and join region names,
# Keep only regions in Africa, Group by year and region and sum refugees,
# Group by year and calculate percent, make year as factor
percent_stack_df <- refugees::population |>
filter(year >= 2013 & year <= 2022) |>
left_join(refugees::countries, by = c("coo_iso" = "iso_code")) |>
filter(unhcr_region %in% c(
"East and Horn of Africa",
"Southern Africa",
"West and Central Africa"
)) |>
group_by(year, unhcr_region) |>
summarise(refugees = sum(refugees, na.rm = TRUE)) |>
ungroup() |>
group_by(year) |>
mutate(refugees = refugees / sum(refugees)) |>
ungroup() |>
mutate(year = as_factor(year))
```
**Plot:**
```{r percent-stack-plot, out.width="100%"}
# Set title
region_name <- percent_stack_df |>
distinct(unhcr_region) |>
pull(unhcr_region)
title_percent_stack <- "Refugees from the different regions of Africa"
subtitle_percent_stack <- paste0(
"Percentage of refugees in <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[1], "'>",
region_name[1], "</span>, <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[2], "'>",
region_name[2], "</span> and <span style='color:",
unhcr_pal(n = 3, "pal_unhcr_region")[3], "'>",
region_name[3], "</span> that originated from these regions at the end of 2013-2022"
)
# Plot
ggplot(
percent_stack_df,
aes(
x = year,
y = refugees,
fill = unhcr_region
)
) +
geom_col(
width = .7,
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_percent()
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_region", nmax = 3
) +
labs(
title = title_percent_stack,
subtitle = subtitle_percent_stack,
caption = caption
) +
theme_unhcr(
grid = "Y", axis = "X", axis_title = FALSE,
legend = FALSE
)
```
**Plot with labels:**
```{r percent-stack-label, out.width="100%"}
# Plot with labels
ggplot(
percent_stack_df,
aes(
x = year,
y = refugees,
fill = unhcr_region
)
) +
geom_col(
width = .7,
) +
geom_text(
aes(
label = scales::label_percent(
accuracy = .1
)(refugees)
),
hjust = .5,
position = position_stack(vjust = .5),
color = if_else(
percent_stack_df$unhcr_region == "West and Central Africa",
"#1a1a1a",
"#ffffff"
)
) +
scale_y_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_percent()
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_region", nmax = 3
) +
labs(
title = title_percent_stack,
subtitle = subtitle_percent_stack,
caption = caption
) +
theme_unhcr(
grid = FALSE, axis = "X", axis_title = FALSE,
axis_text = "x", legend = FALSE
)
```
## Line chart
### Single line chart
Let's plot the evolution of the total refugee population from 1993 to 2022.
**Preparing the data:**
```{r single-line-data}
# Keep data from 1993 to 2022,
# Sum refugees by year, year as factor
single_line_df <- refugees::population |>
filter(year >= 1993 & year <= 2022) |>
summarise(
refugees = sum(refugees, na.rm = TRUE),
.by = year
) |>
mutate(year = as_factor(year))
```
**Plot:**
```{r single-line-plot, out.width="100%"}
# Set title
title_single_line <- "Total number of refugees | 1993-2022"
# Plot
ggplot(
single_line_df,
aes(
x = year,
y = refugees,
group = 1
)
) +
geom_line(
color = unhcr_pal(n = 1, "pal_blue"),
size = 1
) +
scale_x_discrete(
breaks = scales::pretty_breaks(n = 5)
) +
scale_y_continuous(
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
labs(
title = title_single_line,
caption = caption
) +
theme_unhcr(grid = "Y", axis = "X", axis_title = FALSE)
```
### Multiple line chart
Let's plot the evolution of the total refugee and IDP population from 1993 to 2022.
**Preparing the data:**
```{r multi-line-data}
# Keep data from 1993 to 2022, Sum refugees and IDPs by year,
# Pivot longer, year as factor
multi_line_df <- refugees::population |>
filter(year >= 1993 & year <= 2022) |>
summarise(
refugees = sum(refugees, na.rm = TRUE),
idps = sum(idps, na.rm = TRUE),
.by = year
) |>
pivot_longer(