This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/
viz.Rmd
251 lines (212 loc) · 11.3 KB
/
viz.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
---
title: "Mapping blue helmets"
site: workflowr::wflow_site
output:
workflowr::wflow_html:
toc: false
editor_options:
chunk_output_type: console
---
## Visualizing deployment locations
An advantage to the Geo-PKO dataset is that it records the numbers of troops by their specific deployment locations. Therefore, users can quickly visualize *where* active troops are in a mission, be it for a specific mission or a region. Below are some examples of visualization.
To start with, we can take a snapshot of the deployment of all the missions that were active in 2018 in Africa. We start by pulling the shapefiles for country outlines from the package `rnaturalearth`, and filter for countries in Africa.
```{r, warning=FALSE, message=FALSE}
library(rnaturalearth)
library(rnaturalearthdata)
library(sf)
world <- ne_countries(scale = "medium", returnclass = "sf")
library(dplyr)
AFR_sf <- world %>% filter(region_un == "Africa")
```
Did that work? We can plot the shapefiles to find out.
```{r}
library(ggplot2)
ggplot(data=AFR_sf) + geom_sf()
```
Next, we need to subset the main dataset to include only entries (1) for the year of 2018 and (2) in the continent Africa, all with (3) our variables of interests. Geo-PKO reports deployment sizes according to the available maps published by the UN. Therefore, to obtain the numbers of troop deployment at the yearly level, we calculate the average number of troops per location over the months recorded. We should end up with something similar to the table below.
```{r, warning=FALSE, message=FALSE}
library(readr)
library(knitr)
library(kableExtra)
GeoPKO <- readr::read_csv("data/Geo_PKO_v2.0.csv", col_types = cols(.default="c"))
GeoPKO2018 <- GeoPKO %>% filter(year==2018) %>%
select(mission, year, location, latitude, longitude, no.troops, hq, country) %>%
mutate_at(vars(latitude, longitude, no.troops), as.numeric) %>%
group_by(location, mission, latitude, longitude) %>%
mutate(YearlyAverage = round(mean(no.troops, na.rm=TRUE))) %>%
arrange(desc(hq)) %>% slice(1)
kable(GeoPKO2018, caption = "An extract of the 2018 dataframe") %>% kable_styling() %>%
scroll_box(width = "100%", height = "200px")
```
Hold up--the data frame still contains missions that did not take place in Africa. We need to filter this out somehow.
```{r, warning=FALSE, message=FALSE}
library(countrycode)
AFR_list <- codelist %>% filter(continent %in% "Africa") %>% select(country.name.en) %>% pull()
GeoPKO2018_AFR <- GeoPKO2018 %>%
mutate(country=case_when(country=="DRC" ~ "Congo-Kinshasa",
TRUE~ as.character(country))) %>%
filter(country %in% AFR_list)
```
With that set, we can start plotting the deployment locations and their respective sizes for UN peacekeeping missions in Africa in 2018.
```{r, warning=FALSE, message=FALSE}
library(ggrepel) #to nudge labels nicely away from geom_point
library(viridis) #for pretty colors
library(ggthemes)
ggplot(data=AFR_sf) + geom_sf() +
geom_point(data = GeoPKO2018_AFR, aes(x=longitude, y=latitude,
size=YearlyAverage, color= YearlyAverage), alpha=.7)+
scale_size_continuous(name="Average Troop Deployment", range=c(1,12),
breaks=c(0, 100, 300, 500, 1000, 2000, 3000, 4000,5000)) +
scale_color_viridis(option="cividis",
breaks=c(0, 100, 300, 500, 1000, 2000, 3000, 4000,5000),
name="Average Troop Deployment" ) +
guides( colour = guide_legend()) +
geom_point(data = GeoPKO2018_AFR %>% filter(hq==3), aes (x=longitude, y=latitude, shape="HQ"),
fill = "red", size=2, color="red", alpha=.8)+
scale_shape_manual(values=c(23), labels=c("HQ"="Mission HQ"), name="")+
geom_label_repel(data = GeoPKO2018_AFR %>% filter(hq==3), aes(x=longitude, y=latitude, label=mission),
min.segment.length = 0,
direction="both",
label.size = 0.5,
box.padding = 2,
size = 3,
fill = alpha(c("white"),0.5),
shape=16,
size=2) +
labs(title ="UN Peacekeeping in Africa - 2018", color='Average Troop Deployment') +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 14, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.8, l = 4, unit = "cm")),
panel.grid=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
axis.text=element_blank(),
legend.key=element_blank()
)
```
Here is a similar visualization, but this time the color aesthetic for geom_point is mapped to shown country instead.
```{r, warning=FALSE, message=FALSE}
p3 <- ggplot(data=AFR_sf) + geom_sf() +
geom_point(data=GeoPKO2018_AFR,
aes(x=longitude, y=latitude, size=YearlyAverage, color=country), alpha=.4, shape=20)+
geom_point(data=GeoPKO2018_AFR %>% filter(hq==3),
aes(x=longitude, y=latitude),
color="black", shape=16, size=2) +
geom_label_repel(data=GeoPKO2018_AFR %>% filter(hq==3),
min.segment.length = 0.2,
label.size = 0.5,
box.padding = 2,
size = 3,
fill = alpha(c("white"),0.7),
aes(x=longitude, y=latitude, label=mission)) +
labs(title="UN Peacekeeping Deployment and Mission HQs in Africa, 2018")+
scale_size(range = c(2, 16))+
labs(size="Average number of troops\n(continuous scale)",color="Country",shape="HQ")+
theme(
plot.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 14, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.8, l = 4, unit = "cm")),
panel.grid=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
axis.text=element_blank(),
panel.background=element_blank(),
legend.key = element_rect(fill = "#f5f5f2", color = NA),
legend.key.size = unit(1, 'lines')
)+
guides(colour=guide_legend(ncol=2,override.aes = list(size=5)),
size=guide_legend(ncol=2))
p3
```
How has this changed over the period covered by the dataset? An animated graph is great for this purpose. The first step is to prepare a dataframe, much similar to what has been done above for missions taking place in Africa in 2018. First we would calculate the average number of troops that is deployed to a location per mission per year, for every year between 1994 and 2018.
```{r, warning=FALSE}
AFR_GIF <- GeoPKO %>%
mutate(country=case_when(country=="DRC" ~ "Congo-Kinshasa",
TRUE~ as.character(country))) %>%
filter(country %in% AFR_list) %>%
select(mission, year, location, latitude, longitude, no.troops, hq) %>%
mutate_at(vars(latitude, longitude), as.numeric) %>%
group_by_at(vars(-no.troops)) %>%
summarise(ave.no.troops = round(mean(as.numeric(no.troops), na.rm=TRUE)))
```
The animated graph is built on the above code for static graphics, using the cool package `gganimate`.
```{r, animatedgraph, warning=FALSE, dev="png", interval=0.8}
library(gganimate)
# Transforming the "year" variable into a discrete variable.
AFR_GIF$year <- as.factor(AFR_GIF$year)
ggplot(AFR_sf) + geom_sf() +
geom_point(data = AFR_GIF, aes(x=longitude, y=latitude, size= ave.no.troops, color= ave.no.troops, group=year), alpha=.7)+
scale_size_continuous(name="Average Troop Deployment", range=c(1,12),
breaks=c(0, 100, 300, 500, 1000, 2000, 3000, 4000,5000)) +
scale_color_viridis(option="cividis",
breaks=c(0, 100, 300, 500, 1000, 2000, 3000, 4000,5000),
name="Average Troop Deployment" ) +
guides(colour = guide_legend()) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 14, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.8, l = 4, unit = "cm")),
panel.grid=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
legend.key=element_blank(),
plot.caption=element_text(hjust=1, face="italic"))+
transition_states(states=year, transition_length = 3, state_length=3)+
labs(title="UN Peacekeeping in Armed Conflicts in Africa: {closest_state}",
caption="Source: The Geo-PKO dataset v2.0")+
ease_aes()
#run the following command to save the plot
#anim_save("animatedUNPKO.gif", p4)
```
With the release of version 2.0, the Geo-PKO dataset has extended its coverage to missions taking place globally between 1994 and 2019. The above examples are but a few ways through which users can explore and exploit this rich dataset.
The following code shows how to make an animate plot with the global data by year, using the package "animate".
```{r}
library(viridis)
library(animate)
library(ggthemes)
#Create a new dataframe keeping all missions and regions
Global_df <- GeoPKO %>%
select(mission, year, location, latitude, longitude, no.troops) %>%
mutate_at(vars(latitude, longitude, no.troops), as.numeric) %>%
group_by(mission, year, location) %>%
mutate(ave.no.troops = as.integer(mean(no.troops, na.rm=TRUE))) %>%
select(-no.troops) %>% distinct() %>% drop_na(ave.no.troops) %>% filter(ave.no.troops>0)
#Make the year variable discrete
Global_df$year <- as.factor(Global_df$year)
#Make the plot
animated_global <- ggplot() +
borders("world", xlim = c(-130, 140), ylim = c(-50, 50), colour = "gray85", fill = "gray80") +
theme_map()+
geom_point(data = Global_df, aes(x=longitude, y=latitude, size= ave.no.troops, color= ave.no.troops, group=year), alpha=.7)+
scale_size_continuous(name="Average Troop Deployment", range=c(1,12), breaks=c(10, 100,300, 500, 1000, 3000, 4500, 6000, 7000)) +
scale_color_viridis(option="viridis", breaks=c(10, 100, 300, 500, 1000, 3000, 4500, 6000, 7000), name="Average Troop Deployment" ) +
guides(colour = guide_legend()) +
transition_manual(frames = year)+
enter_fade()+
labs(title="UN Peacekeeping in intrastate armed conflicts: {current_frame}",
color="Average Deployment Size",
caption="Source: The Geo-PKO dataset v2.0")+
theme(text = element_text(color = "#22211d"),
legend.position = c(1,0.2),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
legend.key = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 14, hjust=0.6, color = "#4e4d47",
margin = margin(b = -0.1, t = 0.8, l = 4, unit = "cm")),
panel.grid=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
plot.margin = margin(0, 4.5, 0, -1, "cm"))
#Animate the plot
animate(animated_global, height = 400, width =800, fps = 4)
##To save the plot run the following line of code
#anim_save("Animated_GeoPKO2.0.gif")
```