forked from Edouard-Legoupil/unhcrdatapackage
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Origin_Destination.Rmd
84 lines (65 loc) · 2.91 KB
/
Origin_Destination.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
---
title: "Population origin-destination"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Population origin-destination}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
echo=TRUE,
comment = "#>"
)
```
## Load Packages
```{r message=FALSE, warning=FALSE}
library(tidyverse)
library(circlize)
```
## Prepare Data
```{r}
thisbureau <- "Americas"
lastyear <- max(unhcrdatapackage::end_year_population_totals_long$Year)
## https://jokergoo.github.io/circlize_book/book/
chords <- unhcrdatapackage::end_year_population_totals %>%
dplyr::left_join( unhcrdatapackage::reference %>%
select(coa_region = `UNHCRBureau`, iso_3), by = c("CountryAsylumCode" = "iso_3")) %>%
filter(coa_region == thisbureau & Year == lastyear) %>%
dplyr::mutate(across(REF:VDA, ~replace_na(as.numeric(.), 0)),
total = REF + ASY + VDA, #+ IDP + STA + OOC,
# Lump together factor levels into "other"
CountryAsylumName = forcats::fct_lump_prop(CountryAsylumName, prop = .02, w = total),
CountryOriginName = forcats::fct_lump_prop(CountryOriginName, prop = .02, w = total)) %>%
group_by(CountryOriginName, CountryAsylumName) %>%
summarize(total = sum(total), .groups = "drop") %>%
# CountryOriginName = fct_recode(CountryOriginName, "Other" = "China")
mutate(CountryOriginName = str_replace(CountryOriginName, " \\(Bolivarian Republic of\\)", ""),
CountryAsylumName = str_replace(CountryAsylumName, " \\(Bolivarian Republic of\\)", ""),
CountryOriginName = str_replace(CountryOriginName, " \\(Plurinational State of\\)", ""),
CountryAsylumName = str_replace(CountryAsylumName, " \\(Plurinational State of\\)", ""),
CountryOriginName = str_replace(CountryOriginName, "United States of America", "USA"),
CountryAsylumName = str_replace(CountryAsylumName, "United States of America", "USA"))
```
## Generate Plot
```{r ,fig.height=7, fig.width=7, warning=FALSE, echo = TRUE ,message=FALSE}
circlize::chordDiagram(chords,
self.link = 1,
grid.col = colorRampPalette(RColorBrewer::brewer.pal(11, "Paired"))(15),
annotationTrack = "grid" ,
# preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(chords))))),
preAllocateTracks = 1.6
)
circos.track(track.index = 1,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$ylim[1],
CELL_META$sector.index,
facing = "clockwise",
niceFacing = TRUE,
adj = c(0, 0.5))
},
bg.border = NA) # here set bg.border to NA is important
title(main = "Movement of Forcibly Displaced Population")
```