This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/
viz.Rmd
181 lines (156 loc) · 8.7 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
---
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 subsetting the main dataset to include only entries for the year of 2018 in the continent Africa, as well as 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(dplyr)
library(tidyr)
library(readr)
library(knitr)
library(kableExtra)
GeoPKO <- readr::read_csv("data/Geo_PKO_v2_ISO3.csv", col_types = cols(.default="c"),
locale=readr::locale(encoding="latin1")) #importing the dataset
GeoPKO2018 <- GeoPKO %>% filter(year==2018) %>%
select(Mission, year, location, latitude, longitude, No.troops, HQ, country, iso3c) %>%
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 subset also includes other missions elsewhere in the world. We want to somehow leave out these observations. In the next step, we're pulling the shapefiles for country outlines from the package `rnaturalearth`, and filter for countries in Africa. We can also use this data to extract a list of ISO3 codes for African countries and use it to filter out our main dataframe.
```{r, warning=FALSE, message=FALSE}
library(rnaturalearth)
library(rnaturalearthdata)
library(sf)
world <- ne_countries(scale = "medium", returnclass = "sf")
AFR_sf <- world %>% filter(region_un == "Africa")
AFR_list <- AFR_sf %>% pull(iso_a3)
GeoPKO2018 <- GeoPKO2018 %>% filter(iso3c %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(ggplot2) #to make plots
library(ggthemes)
ggplot(data=AFR_sf) + geom_sf() +
geom_point(data = GeoPKO2018, 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 %>% 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 %>% 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,
aes(x=longitude, y=latitude, size=YearlyAverage, color=country), alpha=.4, shape=20)+
geom_point(data=GeoPKO2018 %>% filter(HQ==3),
aes(x=longitude, y=latitude),
color="black", shape=16, size=2) +
geom_label_repel(data=GeoPKO2018 %>% 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}
gif_df <- GeoPKO %>%
filter(iso3c %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, fig.path="figure/viz.Rmd", warning=FALSE, dev="png", interval=0.8}
library(gganimate)
# Transforming the "year" variable into a discrete variable.
gif_df$year <- as.factor(gif_df$year)
ggplot(AFR_sf) + 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(),
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.