forked from Edouard-Legoupil/unhcrdatapackage
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Migrant_and_Forcible_Displacement.Rmd
195 lines (167 loc) · 7.53 KB
/
Migrant_and_Forcible_Displacement.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
---
title: "Migrant and Forcible Displacement"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Migrant and Forcible Displacement}
%\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)
```
## Prepare Data
```{r, cache = TRUE, message=FALSE, warning=FALSE}
# Population, GDP & GNP per Capita from WorldBank
wb_data <- wbstats::wb( indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD", "NY.GDP.PCAP.CD", "NY.GNP.PCAP.CD"),
startdate = 1990, enddate = 2020, return_wide = TRUE)
# Renaming variables for further matching
names(wb_data)[1] <- "iso_3"
names(wb_data)[2] <- "Year"
wb_data$Year <- as.numeric(wb_data$Year)
```
```{r message=FALSE, warning=FALSE}
thisbureau <- "Americas"
displaced <- dplyr::left_join( x= unhcrdatapackage::end_year_population_totals_long,
y= unhcrdatapackage::reference,
by = c("CountryAsylumCode" = "iso_3")) %>%
filter(Population.type %in% c("ASY", "REF", "VDA")) %>%
mutate( iso_3 = CountryAsylumCode)%>%
group_by(Year, iso_3,CountryAsylumName, UNHCRBureau, hcr_subregion, INCOME_GRP) %>%
summarise(Asylum_Refugee_in = sum(Value) ) %>%
## Joint for calculation of Immigration
dplyr::left_join(
dplyr::left_join( x= unhcrdatapackage::end_year_population_totals_long,
y= unhcrdatapackage::reference,
by = c("CountryOriginCode" = "iso_3")) %>%
filter(Population.type %in% c("ASY", "REF", "VDA")) %>%
mutate( iso_3 = CountryOriginCode)%>%
group_by(Year, iso_3,CountryOriginName, UNHCRBureau, hcr_subregion, INCOME_GRP) %>%
summarise(Asylum_Refugee_out = sum(Value) ),
by = c("iso_3", "Year", "UNHCRBureau", "hcr_subregion", "INCOME_GRP")) %>%
# filter(!(is.na(UNHCRBureau))) %>%
# filter( UNHCRBureau == "Americas" ) %>%
# filter( hcr_subregion == "Latin America") %>%
as.data.frame()
#str(displaced)
```
```{r message=FALSE, warning=FALSE}
# ref <- unhcrdatapackage::reference
migrant <- dplyr::left_join( x= unhcrdatapackage::migrants,
y= unhcrdatapackage::reference,
by = c("CountryDestinationM49" = "M49_code")) %>%
group_by(Year, iso_3,CountryDestinationName, UNHCRBureau, hcr_subregion, INCOME_GRP) %>%
summarise(Emigrant = sum(Value) ) %>%
filter( !(is.na(iso_3))) %>%
## Joint for calculation of Immigration
dplyr::left_join(
dplyr::left_join( x= unhcrdatapackage::migrants,
y= unhcrdatapackage::reference,
by = c("CountryOriginM49" = "M49_code")) %>%
group_by(Year, iso_3,CountryOriginName, UNHCRBureau, hcr_subregion, INCOME_GRP) %>%
summarise(Immigrant = sum(Value) ),
by = c("iso_3", "Year", "UNHCRBureau", "hcr_subregion", "INCOME_GRP")) %>%
## Calculate Net migration
mutate( netmigration = Emigrant - Immigrant ) %>%
# filter(!(is.na(UNHCRBureau))) %>%
# filter( UNHCRBureau == "Americas" )%>%
# filter( hcr_subregion == "Latin America") %>%
as.data.frame()
migrant$Year <- as.numeric(migrant$Year)
```
```{r message=FALSE, warning=FALSE}
migProfile <- migrant %>%
dplyr::left_join( displaced, by = c("Year","iso_3", "UNHCRBureau", "hcr_subregion", "INCOME_GRP")) %>%
dplyr::left_join( wb_data, by = c("Year","iso_3"))%>%
## Calculate a few ration
mutate( ratioAsylum_Refugee_in = (Asylum_Refugee_in / SP.POP.TOTL)*100,
ratioImmigrant = (Immigrant / SP.POP.TOTL)*100,
ratioAsylum_Immigrant = (Asylum_Refugee_in / Immigrant)*100,
ratioAsylum_Refugee_out = (Asylum_Refugee_out / SP.POP.TOTL)*100,
ratioEmigrant = (Emigrant / SP.POP.TOTL)*100,
ratioAsylum_Emigrant = (Asylum_Refugee_out / Emigrant)*100,
ratioNetMigration = (netmigration / SP.POP.TOTL)*100 ) %>%
select( Year, iso_3, country,
UNHCRBureau, hcr_subregion,INCOME_GRP,
SP.POP.TOTL,
Asylum_Refugee_in, ratioAsylum_Refugee_in,
Immigrant, ratioImmigrant,
Asylum_Refugee_out, ratioAsylum_Refugee_out,
Emigrant, ratioEmigrant,
ratioAsylum_Immigrant,
ratioAsylum_Emigrant,
ratioNetMigration) %>%
# filter(Year == 2020) %>%
arrange(ratioNetMigration ) %>%
filter( SP.POP.TOTL > 3000000 )
thismigProfile <- migProfile %>%
filter(!(is.na(UNHCRBureau))) %>%
filter( UNHCRBureau == thisbureau )%>%
#filter( hcr_subregion == "Latin America") %>%
#filter( Year > 2000)
filter( Year == 2020) %>%
filter( Asylum_Refugee_in > 50000 )
#names(migProfile)
```
## Generate plot
```{r}
# ggplot(thismigProfile,
# aes(x= reorder(country, pcnetmigration),
# y= pcnetmigration,
# label= pcnetmigration,
# fill = Year)) +
# # facet_wrap( ~ Year, ncol =4) +
# geom_bar(stat='identity',
# aes(fill= Year,
# #fill= pcAsylum_Refugee,
# color= pcAsylum_Refugee),
# position="dodge" ) +
# coord_flip() +
# scale_color_brewer( type = "seq") +
# labs(subtitle="Net Migration",
# title= "Net Migration",
# x="",
# y = "",
#
# caption="UNDESA")
```
```{r echo=TRUE, fig.height=7, fig.width=7, warning=FALSE, ,message=FALSE}
ggplot(thismigProfile,
aes(x= ratioAsylum_Immigrant,
y= ratioImmigrant,
size= Asylum_Refugee_in,
fill= INCOME_GRP,
label = iso_3)) +
geom_point(alpha=0.5,
shape=21,
color="black") +
scale_size(range = c(.1, 24), guide=FALSE, ## Do not show Legend
name="# of Forcibly Displaced People") +
viridis::scale_fill_viridis(discrete=TRUE,
name="Country Income Classification") +
# facet_wrap( vars(Year ), ncol = 2) +
coord_cartesian(clip = "off") +
ggrepel::geom_label_repel(box.padding = 0.5,
size =4,
# max.overlaps = 2
fill = "white",
xlim = c(-Inf, Inf),
ylim = c(-Inf, Inf)) +
labs(title = "Share of Forcibly Displaced People within Migrants",
subtitle = "Per Country of Destination as of 2020. \n The size of the circle represents the number of Refugees & Asylum Seekers ",
x = "Ratio Asylum / Immigrant",
y ="Ratio Immigrant / Host Population",
caption = "UNDESA Migrant Stock & UNHCR (includes VDA in calculation).\n Only countries with more than 5oK Forcibly Displaced Accorss borders are presented.") +
unhcRstyle::unhcr_theme(base_size = 9) +
theme(legend.position="bottom",
panel.grid.major.y = element_line(color = "#cbcbcb"),
panel.grid.major.x = element_line(color = "#cbcbcb"),
panel.grid.minor = element_blank()) ### changing grid line that should appear
```