/
mapping.Rmd
235 lines (192 loc) · 6.43 KB
/
mapping.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
---
title: "Mapping IGOs"
author: Diego Hernangómez
description: >
Integrating IGOs on mapping projects.
bibliography: refs_igo.bib
link-citations: yes
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Mapping IGOs}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<!-- mapping.Rmd is generated from mapping.Rmd.orig. Please edit that file -->
Maps are a powerful tool to show data. As the scope of **igoR** are the
Intergovernmental Organizations, mapping and IGOs are a perfect match.
This vignette provides some geospatial visualizations using the IGO data sets
[@pevehouse2020] included in this package. Specific packages used for geospatial
data:
- **giscoR** for extracting the shapefiles of the countries.
- **ggplot2** for plotting.
Also **countrycode** is a very handy package for translating between coding
schemes (CoW, ISO3, NUTS, FIPS) and country names.
```r
library(igoR)
# Helper packages
library(dplyr)
library(ggplot2)
library(countrycode)
# Geospatial packages
library(giscoR)
library(sf)
```
## Evolution of the composition of UN
The following maps shows the evolution of countries that are members of the
United Nations. First we should extract the data:
```r
# Extract shapes
world <- gisco_get_countries()
# Extract three dates - some errors given that ISO doesnt have every COW Code
un_all <- igo_members("UN", c(1950, 1980, 2010), status = "Full Membership") %>%
# Add ISO3 Code
mutate(ISO3_CODE = countrycode(ccode, "cown", "iso3c", warn = FALSE)) %>%
select(year, orgname, ISO3_CODE, category)
# Auxiliar data.frame to collect every ISO3-year pairs
base_df <- expand.grid(
ISO3_CODE = unique(world$ISO3_CODE),
year = unique(un_all$year),
stringsAsFactors = FALSE
) %>%
as_tibble()
# Merge everything with the spatial object
un_all_sf <- world %>%
# Expand to all cases
left_join(base_df, by = "ISO3_CODE") %>%
# Add info
left_join(un_all, by = c("ISO3_CODE", "year"))
```
Note that the map is not completely accurate, as the base shapefile contains the
countries that exists on 2016. Some countries, as Czechoslovakia, East or West
Germany are not included.
Now we are ready to plot with **ggplot2**:
```r
ggplot(un_all_sf) +
geom_sf(aes(fill = category), color = NA, show.legend = FALSE) +
# Robinson
coord_sf(crs = "ESRI:54030") +
facet_wrap(~year, ncol = 1, strip.position = "left") +
scale_fill_manual(
values = c("Full Membership" = "#74A9CF"),
na.value = "#E0E0E0",
) +
labs(
title = "UN Members", caption = gisco_attributions(),
) +
theme_minimal() +
theme(plot.caption = element_text(face = "italic", hjust = 0.15))
```
<div class="figure">
<img src="./UNMaps-1.png" alt="UN Members" width="100%" />
<p class="caption">UN Members</p>
</div>
## Number of shared memberships
Shared memberships are useful for identifying regional patterns. The following
code produces a map showing the number of full memberships shared with Australia
for each country on the world:
```r
## Number of igos shared - 2014
# Countries alive in 2014
states2014 <- states2016 %>%
filter(styear <= 2014 & endyear >= 2014)
# Shared memberships with Australia
shared <- igo_dyadic("AUL", as.character(states2014$statenme),
year = 2014
) %>%
rowwise() %>%
mutate(shared = sum(c_across(aaaid:wassen) == 1)) %>%
mutate(ISO3_CODE = countrycode(ccode2, "cown", "iso3c",
warn = FALSE
)) %>%
select(ISO3_CODE, shared)
# Merge with map
sharedmap <- world %>%
left_join(shared, by = "ISO3_CODE") %>%
select(ISO3_CODE, shared)
# Plot with custom palette
pal <- hcl.colors(10, palette = "Lajolla")
# Plot
ggplot(sharedmap) +
geom_sf(aes(fill = shared), color = NA) +
# Australia
geom_sf(
data = sharedmap %>% filter(ISO3_CODE == "AUS"),
fill = "black", color = NA,
) +
# Robinson
coord_sf(crs = "ESRI:54030") +
scale_fill_gradientn(colours = pal, n.breaks = 10) +
guides(fill = guide_legend(nrow = 1)) +
labs(
title = "Shared Full Memberships with Australia (2014)",
fill = "Number of IGOs shared",
caption = gisco_attributions()
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", size = 7, hjust = 0.15),
legend.title = element_text(size = 7),
legend.text = element_text(size = 8),
legend.position = "bottom",
legend.direction = "horizontal",
legend.title.position = "top",
legend.text.position = "bottom",
legend.key.width = unit(1.5, "lines"),
legend.key.height = unit(0.5, "lines")
)
```
<div class="figure">
<img src="./AustShared-1.png" alt="Shared Full Memberships with Australia (2014)" width="100%" />
<p class="caption">Shared Full Memberships with Australia (2014)</p>
</div>
## Cross-shared memberships
The following map shows how the relationships between the countries of North
America has flourished on the last 90 years, using a year as representative of
each decade.
```r
# Select years
years <- seq(1930, 2010, 10)
# Shared memberships
cntries <- c("USA", "CAN", "MEX")
all <- igo_dyadic(cntries, cntries, years) %>%
rowwise() %>%
mutate(value = sum(c_across(aaaid:wassen) == 1)) %>%
mutate(ISO3_CODE = countrycode(ccode1, "cown", "iso3c")) %>%
select(ISO3_CODE, year, value)
# Create map
# Get shapes
countries_sf <- gisco_get_countries(country = c("USA", "MEX", "CAN")) %>%
left_join(all, by = "ISO3_CODE")
# Map
ggplot(countries_sf) +
geom_sf(aes(fill = value), color = NA) +
coord_sf(crs = 2163, xlim = c(-3200000, 3333018)) +
facet_wrap(~year, ncol = 3) +
scale_fill_gradientn(
colors = hcl.colors(10, "YlGn", rev = TRUE),
breaks = seq(0, 100, 5)
) +
guides(fill = guide_legend(reverse = TRUE)) +
labs(
title = "Shared Full Memberships on North America",
subtitle = "(1930-2010)", fill = "Shared IGOs",
caption = gisco_attributions()
) +
theme_void() +
theme(
plot.title = element_text(face = "bold"),
plot.subtitle = element_text(margin = margin(t = 3, b = 10)),
plot.caption = element_text(face = "italic"),
legend.box.margin = margin(l = 20),
legend.title = element_text(size = 8),
legend.key.height = unit(1.5, "lines"),
legend.key.width = unit(1, "lines"),
strip.background = element_rect(fill = "grey90", colour = NA)
)
```
<div class="figure">
<img src="./NAShared-1.png" alt="Shared Full Memberships on North America (1930 - 2010)" width="100%" />
<p class="caption">Shared Full Memberships on North America (1930 - 2010)</p>
</div>
## References