-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-wgi-mapping.R
363 lines (323 loc) · 13.3 KB
/
script-wgi-mapping.R
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Start
# title: "World Governance Indicator Mapping"
# author: "Raquel Baeta"
# date: "2024-07-25"
# Install
install.packages(c("readr", "countrycode", "rnaturalearth", "sf", "ggplot2", "dplyr", "maps", "mapproj"))
library(c(readr, dplyr, rnaturalearth, sf, countrycode, ggplot2, maps, mapproj, tidyverse, scales))
# Set the working directory
setwd("~/Desktop/working-sessions")
# Load data
data <- read_csv("~/Desktop/working-sessions/cleaning_data/cleaned_data.csv")
# Data Wrangling
data$year_interval_3yr <- cut(
data$year,
breaks = seq(1996, 2019, by = 3),
include.lowest = TRUE,
labels = FALSE
)
grouped_data_3yr_country <- data %>%
group_by(region, code, country, year_interval_3yr, any_UN) %>%
summarise(
mean_seizures = mean(seizures, na.rm = TRUE),
mean_log_adjusted_gdp = mean(log_adjusted_gdp, na.rm = TRUE),
mean_milex_gdp = mean(milex_gdp, na.rm = TRUE),
mean_trade_ratio = mean(trade_ratio, na.rm = TRUE),
mean_CC_EST = mean(CC.EST, na.rm = TRUE),
mean_GE_EST = mean(GE.EST, na.rm = TRUE),
mean_RQ_EST = mean(RQ.EST, na.rm = TRUE),
mean_RL_EST = mean(RL.EST, na.rm = TRUE),
mean_VA_EST = mean(VA.EST, na.rm = TRUE),
mean_PV_EST = mean(PV.EST, na.rm = TRUE)
)
# Fill missing values in "year_interval_3yr"
grouped_data_3yr_country$year_interval_3yr <- ifelse(
is.na(grouped_data_3yr_country$year_interval_3yr), "2017-2019", grouped_data_3yr_country$year_interval_3yr
)
# Calculate the average of the six columns and create a new column called "wgi"
grouped_data_3yr_country$mean_wgi <- rowMeans(
grouped_data_3yr_country[, c("mean_CC_EST", "mean_GE_EST", "mean_RQ_EST", "mean_RL_EST", "mean_VA_EST", "mean_PV_EST")], na.rm = TRUE
)
# Identify and count duplicate rows within each group
duplicates <- grouped_data_3yr_country %>%
group_by(region, code, country, year_interval_3yr) %>%
filter(n() > 1) %>%
arrange(region, code, country, year_interval_3yr)
# Load the world map
world <- ne_countries(scale = "medium", returnclass = "sf")
# Merge world map with data
wgi_world_mapping <- merge(
world,
grouped_data_3yr_country,
by.x = "iso_a3",
by.y = "code",
all.x = TRUE
)
# Convert mean_wgi to numeric
wgi_world_mapping$mean_wgi <- as.numeric(wgi_world_mapping$mean_wgi)
# Create breaks and labels based on quartiles
wgi_world_mapping$mean_wgi <- cut(
wgi_world_mapping$mean_wgi,
breaks = c(-Inf, -0.66341, -0.23938, -0.01904, 0.66184, Inf),
labels = c("Very Low", "Low", "Medium", "High", "Very High")
)
# Define individual colors for each category
colors <- c("Very Low" = "#B3446C", "Low" = "#DC3545", "Medium" = "#F08030", "High" = "#FFC107", "Very High" = "#C7B8E6")
# Create the plot with dots
wgi_plot <- ggplot() +
geom_sf(
data = wgi_world_mapping,
aes(fill = mean_wgi),
color = "white") +
coord_sf(crs = "+proj=robin") +
labs(
title = "Exploring Global Governance: A Geospatial Analysis of World Governance Index (WGI) from 1996 to 2019",
subtitle = "Patterns and Trends Through Shading: An Examination of World Governance Indicator Estimates Across Nations",
caption = "Source: World Bank (WB) Data Bank") +
theme_minimal() +
theme(
legend.position = "top",
panel.grid.major = element_line(color = "lightgrey", linetype = "dotted")) +
scale_fill_manual(values = colors, name = "WGI Estimates")
# Print and save the plot
print(wgi_plot)
ggsave("avg_wgi_plot_region_plot.pdf", plot = wgi_plot, width = 12, height = 8)
ggsave("avg_wgi_plot_region_plot.png", plot = wgi_plot, width = 12, height = 8)
#
# Plot Indicators by Region
#
# Define custom labels for year intervals
custom_labels <- c("1996-1998", "1999-2001", "2002-2004", "2005-2007", "2008-2010", "2011-2013", "2014-2016", "2017-2019")
# Define custom colors for each region
colors <- c("#FFC107", "#38A3A5", "#B3446C", "#DC3545", "#007BFF", "#F08030", "#C7B8E6")
# [1] CC Est
grouped_data_3yr_country_mean_CC_EST <- grouped_data_3yr_country %>%
group_by(region, year_interval_3yr) %>%
summarise(mean_CC_EST = mean(mean_CC_EST, na.rm = TRUE))
# Arrange and keep only the last observation for each region
last_points <- grouped_data_3yr_country_mean_CC_EST %>%
group_by(region) %>%
slice(n())
# Plotting
mean_cc_est_plot <- ggplot(
grouped_data_3yr_country_mean_CC_EST,
aes(x = year_interval_3yr, y = mean_CC_EST, fill = region, group = region)) +
geom_ribbon(aes(ymin = 0, ymax = mean_CC_EST), alpha = 0.2) +
geom_hline(yintercept = 0, color = "#FF0000", alpha = 0.5) +
geom_text(
data = last_points,
aes(label = region),
vjust = -0.5,
size = 3.5,
font = "bold",
show.legend = FALSE) +
labs(
x = "",
y = "Mean Control of Corruption (CC) Estimate",
title = "Mean Control of Corruption (CC) Estimate Trends (1996-2019)",
subtitle = "Patterns and Anomalies of Control of Corruption through Geospatial Exploration by Region",
caption = "Source: Baeta, using data from World Bank, World Development Indicators, and United Nations (2024)") +
theme_bw() +
theme(
legend.position = "top",
plot.title = element_text(size = 16),
panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),
panel.border = element_blank()) +
scale_fill_manual(values = colors, name = "") +
scale_x_discrete(labels = custom_labels) +
scale_y_continuous(
breaks = seq(
min(grouped_data_3yr_country_mean_CC_EST$mean_CC_EST, na.rm = TRUE),
max(grouped_data_3yr_country_mean_CC_EST$mean_CC_EST, na.rm = TRUE),
by = 0.2),
labels = scales::number_format(accuracy = 0.1)
)
# Print and save the plot
print(mean_cc_est_plot)
ggsave("avg_mean_CC_est_region_plot.pdf", plot = mean_cc_est_plot, width = 12, height = 8)
ggsave("avg_mean_CC_est_region_plot.png", plot = mean_cc_est_plot, width = 12, height = 8)
# [2] GE Est
grouped_data_3yr_country_mean_GE_EST <- grouped_data_3yr_country %>%
group_by(region, year_interval_3yr) %>%
summarise(mean_GE_EST = mean(mean_GE_EST, na.rm = TRUE))
# Arrange and keep only the last observation for each region
last_points <- grouped_data_3yr_country_mean_GE_EST %>%
group_by(region) %>%
slice(n())
# Plotting
mean_GE_est_plot <- ggplot(
grouped_data_3yr_country_mean_GE_EST,
aes(x = year_interval_3yr, y = mean_GE_EST, fill = region, group = region)) +
geom_ribbon(aes(ymin = 0, ymax = mean_GE_EST), alpha = 0.2) +
geom_hline(yintercept = 0, color = "#FF0000", alpha = 0.5) +
geom_text(
data = last_points,
aes(label = region),
vjust = -0.5,
size = 3.5,
font = "bold",
show.legend = FALSE) +
labs(
x = "",
y = "Mean Government Effectiveness (GE) Estimate",
title = "Mean Government Effectiveness (GE) Estimate Trends (1996-2019)",
subtitle = "Patterns and Anomalies of Government Effectiveness through Geospatial Exploration by Region",
caption = "Source: Baeta, using data from World Bank, World Development Indicators, and United Nations (2024)") +
theme_bw() +
theme(
legend.position = "top",
plot.title = element_text(size = 16),
panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),
panel.border = element_blank()) +
scale_fill_manual(values = colors, name = "") +
scale_x_discrete(labels = custom_labels) +
scale_y_continuous(
breaks = seq(
min(grouped_data_3yr_country_mean_GE_EST$mean_GE_EST, na.rm = TRUE),
max(grouped_data_3yr_country_mean_GE_EST$mean_GE_EST, na.rm = TRUE),
by = 0.2),
labels = scales::number_format(accuracy = 0.1)
)
# Print and save the plot
print(mean_GE_est_plot)
ggsave("avg_mean_GE_est_region_plot.pdf", plot = mean_GE_est_plot, width = 12, height = 8)
ggsave("avg_mean_GE_est_region_plot.png", plot = mean_GE_est_plot, width = 12, height = 8)
# [3] RQ Est
grouped_data_3yr_country_mean_RQ_EST <- grouped_data_3yr_country %>%
group_by(region, year_interval_3yr) %>%
summarise(mean_RQ_EST = mean(mean_RQ_EST, na.rm = TRUE))
# Arrange and keep only the last observation for each region
last_points <- grouped_data_3yr_country_mean_RQ_EST %>%
group_by(region) %>%
slice(n())
# Plotting
mean_RQ_est_plot <- ggplot(
grouped_data_3yr_country_mean_RQ_EST,
aes(x = year_interval_3yr, y = mean_RQ_EST, fill = region, group = region)) +
geom_ribbon(aes(ymin = 0, ymax = mean_RQ_EST), alpha = 0.2) +
geom_hline(yintercept = 0, color = "#FF0000", alpha = 0.5) +
geom_text(
data = last_points,
aes(label = region),
vjust = -0.5,
size = 3.5,
font = "bold",
show.legend = FALSE) +
labs(
x = "",
y = "Mean Regulatory Quality (RQ) Estimate",
title = "Mean Regulatory Quality (RQ) Estimate Trends (1996-2019)",
subtitle = "Patterns and Anomalies of Regulatory Quality through Geospatial Exploration by Region",
caption = "Source: Baeta, using data from World Bank, World Development Indicators, and United Nations (2024)") +
theme_bw() +
theme(
legend.position = "top",
plot.title = element_text(size = 16),
panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),
panel.border = element_blank()) +
scale_fill_manual(values = colors, name = "") +
scale_x_discrete(labels = custom_labels) +
scale_y_continuous(
breaks = seq(
min(grouped_data_3yr_country_mean_RQ_EST$mean_RQ_EST, na.rm = TRUE),
max(grouped_data_3yr_country_mean_RQ_EST$mean_RQ_EST, na.rm = TRUE),
by = 0.2),
labels = scales::number_format(accuracy = 0.1)
)
# Print and save the plot
print(mean_RQ_est_plot)
ggsave("avg_mean_RQ_est_region_plot.pdf", plot = mean_RQ_est_plot, width = 12, height = 8)
ggsave("avg_mean_RQ_est_region_plot.png", plot = mean_RQ_est_plot, width = 12, height = 8)
# [4] RL Est
grouped_data_3yr_country_mean_RL_EST <- grouped_data_3yr_country %>%
group_by(region, year_interval_3yr) %>%
summarise(mean_RL_EST = mean(mean_RL_EST, na.rm = TRUE))
# Arrange and keep only the last observation for each region
last_points <- grouped_data_3yr_country_mean_RL_EST %>%
group_by(region) %>%
slice(n())
# Plotting
mean_RL_est_plot <- ggplot(
grouped_data_3yr_country_mean_RL_EST,
aes(x = year_interval_3yr, y = mean_RL_EST, fill = region, group = region)) +
geom_ribbon(aes(ymin = 0, ymax = mean_RL_EST), alpha = 0.2) +
geom_hline(yintercept = 0, color = "#FF0000", alpha = 0.5) +
geom_text(
data = last_points,
aes(label = region),
vjust = -0.5,
size = 3.5,
font = "bold",
show.legend = FALSE) +
labs(
x = "",
y = "Mean Rule of Law (RL) Estimate",
title = "Mean Rule of Law (RL) Estimate Trends (1996-2019)",
subtitle = "Patterns and Anomalies of Rule of Law through Geospatial Exploration by Region",
caption = "Source: Baeta, using data from World Bank, World Development Indicators, and United Nations (2024)") +
theme_bw() +
theme(
legend.position = "top",
plot.title = element_text(size = 16),
panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),
panel.border = element_blank()) +
scale_fill_manual(values = colors, name = "") +
scale_x_discrete(labels = custom_labels) +
scale_y_continuous(
breaks = seq(
min(grouped_data_3yr_country_mean_RL_EST$mean_RL_EST, na.rm = TRUE),
max(grouped_data_3yr_country_mean_RL_EST$mean_RL_EST, na.rm = TRUE),
by = 0.2),
labels = scales::number_format(accuracy = 0.1)
)
# Print and save the plot
print(mean_RL_est_plot)
ggsave("avg_mean_RL_est_region_plot.pdf", plot = mean_RL_est_plot, width = 12, height = 8)
ggsave("avg_mean_RL_est_region_plot.png", plot = mean_RL_est_plot, width = 12, height = 8)
# [5] VA Est
grouped_data_3yr_country_mean_VA_EST <- grouped_data_3yr_country %>%
group_by(region, year_interval_3yr) %>%
summarise(mean_VA_EST = mean(mean_VA_EST, na.rm = TRUE))
# Arrange and keep only the last observation for each region
last_points <- grouped_data_3yr_country_mean_VA_EST %>%
group_by(region) %>%
slice(n())
# Plotting
mean_VA_est_plot <- ggplot(
grouped_data_3yr_country_mean_VA_EST,
aes(x = year_interval_3yr, y = mean_VA_EST, fill = region, group = region)) +
geom_ribbon(aes(ymin = 0, ymax = mean_VA_EST), alpha = 0.2) +
geom_hline(yintercept = 0, color = "#FF0000", alpha = 0.5) +
geom_text(
data = last_points,
aes(label = region),
vjust = -0.5,
size = 3.5,
font = "bold",
show.legend = FALSE) +
labs(
x = "",
y = "Mean Voice and Accountability (VA) Estimate",
title = "Mean Voice and Accountability (VA) Estimate Trends (1996-2019)",
subtitle = "Patterns and Anomalies of Voice and Accountability through Geospatial Exploration by Region",
caption = "Source: Baeta, using data from World Bank, World Development Indicators, and United Nations (2024)") +
theme_bw() +
theme(
legend.position = "top",
plot.title = element_text(size = 16),
panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),
panel.border = element_blank()) +
scale_fill_manual(values = colors, name = "") +
scale_x_discrete(labels = custom_labels) +
scale_y_continuous(
breaks = seq(
min(grouped_data_3yr_country_mean_VA_EST$mean_VA_EST, na.rm = TRUE),
max(grouped_data_3yr_country_mean_VA_EST$mean_VA_EST, na.rm = TRUE),
by = 0.2),
labels = scales::number_format(accuracy = 0.1)
)
# Print and save the plot
print(mean_VA_est_plot)
ggsave("avg_mean_VA_est_region_plot.pdf", plot = mean_VA_est_plot, width = 12, height = 8)
ggsave("avg_mean_VA_est_region_plot.png", plot = mean_VA_est_plot, width = 12, height = 8)
# End