-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsing_world_map2_script.R
273 lines (187 loc) · 6.58 KB
/
Using_world_map2_script.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
# Using world_map2 script version -----------------------------------------
# Libraries ---------------------------------------------------------------
library(tidyverse)
library(here)
library(visdat)
# Data --------------------------------------------------------------------
# our custom map data
load(here::here("data",
"tidy_data",
"maps",
"world_map2_project.rda"))
# raw Global Studies dat from Gapminder.org
gni_percapita <- read_csv(here::here("data",
"raw_data",
"gnipercapita_ppp_current_international.csv") )
# raw Global Studies dat from Gapminder.org
water_access <- read_csv(here::here("data",
"raw_data",
"at_least_basic_water_source_overall_access_percent.csv") )
# raw Global Studies dat from Gapminder.org
energy_capita <- read_csv(here::here("data",
"raw_data",
"energy_use_per_person.csv"))
# Energy Case Study -------------------------------------------------------
energy_capita %>% vis_dat()
energy_capita %>% glimpse()
setdiff(energy_capita$country,world_map2$country)
setdiff(world_map2$country, energy_capita$country) %>%
enframe(name = NULL, value ="diff")
energy_tidy <- energy_capita %>%
pivot_longer(cols = !country,
names_to = "year",
names_transform = list(year = as.integer),
values_to = "energy")
energy_tidy %>% vis_dat()
energy_tidy %>% glimpse()
#
# ## This is our bad example
#
energy_tidy %>%
filter(year == 2004) %>%
left_join(world_map2, by = "country") %>%
ggplot(aes(x = long,
y = lat,
group = group,
label = country)) +
geom_polygon(aes(fill = energy) )+
scale_fill_viridis_c(option = "C") +
labs(fill = "Energy Use\nPer Capita",
title = "Gapminder Data: 2004") +
theme_void()
#
# ## This is our good example: graceful failure
#
energy_tidy %>%
filter(year == 2004) %>%
complete(country = world_map2$country,
fill = (list(energy = NA )) ) %>%
left_join(world_map2, by = "country") %>%
replace_na(list(year = 2004)) %>%
ggplot(aes(x = long,
y = lat,
group = group,
label = country)) +
geom_polygon(aes(fill = energy) )+
scale_fill_viridis_c(option = "C") +
labs(fill = "Energy Use\nPer Capita",
title = "Gapminder Data: 2000") +
theme_void()
#
# ## Easy interactivity & no Anart mapped
#
# Map data
enegry_dat_2004 <- energy_tidy %>%
filter(year == 2004) %>%
complete(country = world_map2$country,
fill = (list(energy = NA )) ) %>%
left_join(world_map2, by = "country") %>%
replace_na(list(year = 2004))
# ggplot object
enegry_map_2004 <- enegry_dat_2004 %>%
filter(code_3 != "ATA") %>%
ggplot(aes(x = long,
y = lat,
group = group,
label = country)) +
geom_polygon(aes(fill = energy) )+
scale_fill_viridis_c(option = "C") +
labs(fill = "",
title = "Energy Use Per Capita (2004)") +
theme_void()
# interactive version
plotly::ggplotly(enegry_map_2004)
# Water Acess Case Study --------------------------------------------------
water_access %>% vis_dat()
water_access %>% glimpse()
setdiff(water_access$country,world_map2$country)
setdiff(world_map2$country, water_access$country) %>%
enframe(name = NULL, value ="diff")
## Tidy it
water_tidy <- water_access %>%
pivot_longer(cols = !country,
names_to = "year",
names_transform = list(year = as.integer),
values_to = "water")
water_tidy %>%
vis_dat()
water_tidy %>%
glimpse()
## Map data
water_dat_2010 <- water_tidy %>%
filter(year == 2010) %>%
complete(country = world_map2$country,
fill = (list(water = NA )) ) %>%
left_join(world_map2, by = "country") %>%
replace_na(list(year = 2010))
## Plot Object
water_mpa_2010 <- water_dat_2010 %>%
filter(code_3 != "ATA") %>%
ggplot(aes(x = long,
y = lat,
group = group,
label = country)) +
geom_polygon(aes(fill = water) )+
scale_fill_viridis_c(option = "C") +
labs(fill = "",
title = "Basic Water Access (2010)") +
theme_void()
# Basic plot
water_mpa_2010
# interactive
plotly::ggplotly(water_mpa_2010)
# GNI Per Capita PPP Case Study -------------------------------------------
gni_percapita %>%
vis_dat()
gni_percapita %>%
glimpse()
setdiff(gni_percapita$country, world_map2$country) %>%
enframe(name = NULL, value ="diff")
setdiff(world_map2$country, gni_percapita$country) %>%
enframe(name = NULL, value ="diff")
#
## Troubleshoot to TIDY
#
# convert char data to numeric
gni_tidy <- gni_percapita %>%
pivot_longer(cols = !country,
names_to = "year",
names_transform = list(year = as.integer),
values_to = "gni_ppp_cap") %>%
mutate(gni_ppp_cap = readr::parse_number(gni_ppp_cap) )%>%
mutate(gni_ppp_cap = case_when(gni_ppp_cap < 200 ~ gni_ppp_cap * 1000,
TRUE ~ gni_ppp_cap) )
# reconcile names
gni_tidy <- gni_tidy %>%
mutate( country = case_when(country == "Curaçao" ~ "Curacao",
country == "Sint Maarten (Dutch part)" ~ "Sint Maarten" ,
TRUE ~ country) )
# Brief check
gni_tidy %>% slice_min(gni_ppp_cap, n = 4)
gni_tidy %>% slice_max(gni_ppp_cap, n = 4)
setdiff(gni_tidy$country, world_map2$country)
gni_tidy %>%
vis_dat()
## Map Data and Plot Object
gni_dat_2017 <- gni_tidy %>%
filter(year == 2017) %>%
complete(country = world_map2$country,
fill = (list(gni_ppp_cap = NA)) ) %>%
left_join(world_map2, by = "country") %>%
replace_na(list(year = 2017))
gni_map_2017 <- gni_dat_2017%>%
filter(code_3 != "ATA") %>%
ggplot(aes(x = long,
y = lat,
group = group,
label = country)) +
geom_polygon(aes(fill = gni_ppp_cap) )+
scale_fill_viridis_c(option = "C") +
labs(fill = "",
title = "GNI Per Capita for 2017 (in PPP dollars)") +
theme_void()
# Basic
gni_map_2017
# interactice
plotly::ggplotly(gni_map_2017)
############ END ############