This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/
index.Rmd
149 lines (120 loc) · 6.16 KB
/
index.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
---
title: "Exploring the Geo-PKO dataset"
site: workflowr::wflow_site
output:
workflowr::wflow_html:
toc: false
editor_options:
chunk_output_type: console
---
This document contains a series of steps that the project members have performed to extract meaningful information the Geo-PKO dataset. More details on the dataset, as well as the version used here, can be found on its [homepage](https://www.pcr.uu.se/data/geo-pko/).
## Setting up
Load packages.
```{r, warning=FALSE, message=FALSE}
library(tidyverse)
library(readr)
library(ggthemes)
library(knitr)
library(kableExtra)
```
Import the dataset.
```{r, warning=FALSE, message =FALSE}
GeoPKO <- read_csv("data/geopko.csv")
```
## An overview
Let's have a quick look at the first few rows of the dataset.
```{r}
kable(GeoPKO[1:5,]) %>% kable_styling() %>%
scroll_box(width = "100%", height = "200px")
```
The dataset covers UN peacekeeping missions in Africa between 1994 and 2018. We can use the dataset to extract the number of active missions during this period.
```{r}
NoMission <- GeoPKO %>% select(year, Mission) %>% distinct(year, Mission) %>% count(year)
Plot1 <- ggplot(NoMission, aes(x=(as.numeric(year)), y=n)) + geom_point() + geom_line(size=0.5) +
scale_x_continuous("Year", breaks=seq(1994, 2018, 1))+theme_classic()+
scale_y_continuous("Number of missions", breaks=seq(0,10,1)) +
theme(panel.grid=element_blank(),
axis.text.x=element_text(angle=45, vjust=0.5))
Plot1
```
##Visualizing deployment locations
We want to have a quick snapshot of the deployment size in 2018, as well as the missions that were active in that year. We start by subsetting the main dataset to include entries for the year of 2018 and our variables of interests. GeoPKO 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.
```{r}
GeoPKO$No.troops <- as.numeric(GeoPKO$No.troops)
map2018df <- GeoPKO %>% filter(year==2018) %>%
select(Mission, year, location, latitude, longitude, No.troops, HQ)
map2018df1 <- map2018df %>% group_by(location, Mission) %>%
mutate(ave = mean(No.troops, na.rm=TRUE)) %>% distinct()
kable(map2018df1[90:95,], caption = "A preview of this dataframe") %>% kable_styling()
```
Next, we obtain the geometric shapes from `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")
Africa <- world %>% filter(region_un == "Africa")
```
Finally, we plot our data onto the map.
```{r, warning=FALSE, message=FALSE}
library(ggrepel)
library(viridis)
p2 <- ggplot(data=Africa) + geom_sf() +
geom_point(data = map2018df1, aes(x=longitude, y=latitude, size= ave, color= ave), 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 = map2018df1 %>% filter(HQ==3), aes (x=longitude, y=latitude), color = "red", shape = 4, size=7)+
geom_label_repel(data = map2018df1 %>% filter(HQ==3), aes(x=longitude, y=latitude, label=Mission)) +
labs (title ="UN Peacekeeping Deployment in Africa - 2018 (approx.)") +
labs(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.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
legend.key=element_blank()
)
p2
```
How has this changed over the period covered by the dataset? For that, we attempted to make an animated graph. The first step is to prepare a dataframe, much similar to what has been done above for 2018. First we would calculate the average number of troops that is deployed to a location per mission per year.
```{r}
gif_df <- GeoPKO %>% select(Mission, year, location, latitude, longitude, No.troops, HQ) %>%
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)
```
Next, we add animation to the above map using the package `gganimate`.
```{r, warning=FALSE, animation.hook='ffmpeg', ffmpeg.format='gif', dev='jpeg'}
library(gganimate)
library(gifski)
# Transforming the "year" variable into a discrete variable.
gif_df$year <- as.factor(gif_df$year)
p3 <- ggplot(data=Africa) + geom_sf() +
geom_point(data = gif_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(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())+
transition_states(states=year, transition_length = 3, state_length=3)+
labs(title="UN Peacekeeping in Africa: {closest_state}",
color="Average Deployment Size")+
enter_fade()
animate(p3)
#run the following command to save the plot
#anim_save("animatedUNPKO.gif", p3)
```