/
kenya.R
273 lines (224 loc) · 7.65 KB
/
kenya.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
# Kenya
library(tidyverse)
library(reshape2)
library(rnaturalearth)
library(sp)
library(sf)
library(raster)
library(rgdal)
library(dplyr)
library(cowplot)
library(extrafont)
library(here)
## Loading fonts
font_import()
loadfonts(device = "win")
fonts <- fonts()
theme_set(theme_bw())
#remotes::install_github("Shelmith-Kariuki/rKenyaCensus")
library(rKenyaCensus)
# Load maps ---------------------------------------------------------------
africa <- ne_countries(continent = "Africa")
# Kenya county
kenya <- raster::getData('GADM', country='KEN', level=1)
crs(kenya) <- crs(africa)
kenya_sf <- st_as_sf(kenya)
africa_sf <- st_as_sf(africa)
# dir.create(here("2021", "week04"))
# Load data ---------------------------------------------------------------
gender <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-19/gender.csv')
crops <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-19/crops.csv')
households <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-19/households.csv')
k_crops <-
crops %>%
filter(SubCounty != "KENYA") %>%
dplyr::select(-Farming) %>%
mutate(across(where(is.numeric),
~(.x/1000)),
SubCounty = str_replace_all(SubCounty, "[:punct:]", " ")
) %>%
rename(county = SubCounty,
Cashew = `Cashew Nut`,
Miraa = `Khat (Miraa)`)
kenya_sf$county <- toupper(str_replace_all(kenya_sf$NAME_1, "[:punct:]", " "))
kenya_sf_crop <- left_join(kenya_sf, k_crops)
# Set colors --------------------------------------------------------------
plot.bg <- "grey20"
text.col <- "grey99"
pol.border <- "grey70"
crop.col <- data.frame(
low.base = "grey50",
Tea = "#d64100",
Coffee = "#e30000",
Avocado = "#2ac81b",
Citrus = "#ffa700",
Mango = "#ffdb00",
Coconut = "#b27337",
Macadamia = "#fff6a0",
Cashew = "#ffd88f",
Miraa = "#b41467"
)
# Set theme ---------------------------------------------------------------
setings <- list( theme_void(),
theme(plot.background = element_rect(fill = plot.bg,
color = NA),
legend.title = element_text(color = text.col,
family = "Young"),
legend.text = element_text(color = text.col,
family = "Young"),
plot.title = element_text(color = text.col,
hjust = 0.5,
size = 20,
family = "Young",
face = "bold"),
plot.margin = margin(20,20,20,20))
)
# Maps for each crop ------------------------------------------------------
# Tea
k_tea <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Tea), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Tea,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 8) +
labs(title = "Tea") +
setings
# Coffee
k_coffee <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Coffee), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Coffee,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 8) +
labs(title = "Coffee") +
setings
# Avocado
k_avocado <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Avocado), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Avocado,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 5) +
labs(title = "Avocado") +
setings
# Citrus
k_citrus <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Citrus), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Citrus,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 5) +
labs(title = "Citrus") +
setings
# Mango
k_mango <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Mango), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Mango,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 10) +
labs(title = "Mango") +
setings
# Coconut
k_coconut <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Coconut), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Coconut,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 5) +
labs(title = "Coconut") +
setings
# Macadamia
k_macadamia <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Macadamia), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Macadamia,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 10) +
labs(title = "Macadamia") +
setings
# Cashew
k_cashew <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Cashew), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Cashew,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 6) +
labs(title = "Cashew nut") +
setings
# Miraa
k_miraa <-
ggplot(kenya_sf_crop) +
geom_sf(aes(fill = Miraa), color = pol.border) +
scale_fill_gradient(low = crop.col$low.base,
high = crop.col$Miraa,
na.value = "transparent",
name = "Pop. (k)",
n.breaks = 6) +
labs(title = "Khat (Miraa)") +
setings
# Chart crops -------------------------------------------------------------
crop_chart <- plot_grid(k_avocado, k_cashew, k_citrus,
k_coconut, k_coffee, k_macadamia,
k_mango, k_miraa, k_tea,
nrow = 3, ncol = 3)
k.col <- ifelse(africa_sf$sovereignt == "Kenya", "#CC0101", "grey30")
# Africa
kenya_map <-
ggplot(africa_sf) +
geom_sf(fill = k.col, color = "grey70") +
setings +
theme(plot.background = element_rect(fill = "transparent"))
description <- "Size of the population farming
permanent crops by type and county."
# Graph -------------------------------------------------------------------
crop_graph <-
ggdraw(crop_chart) +
labs(caption = "@avrodrigues_ | #TidyTuesday | Source: rKenyaCensus") +
setings +
theme(plot.margin = margin(250,10,50,10),
plot.caption = element_text(color = text.col,
family = "Segoe UI")
) +
draw_plot(kenya_map,
x = 0.185, y = 1.17,
hjust = 0.5,
vjust = 0.5,
scale = 0.35) +
draw_text("Permanent Crops in Kenya",
x = 0.615, y = 1.25,
size = 37,
fontface = "bold",
family = "Young",
color = text.col) +
draw_text(description,
x = 0.615, y = 1.13,
size = 18,
family = "Segoe UI",
color = text.col) +
draw_line(
x = c(0.1,0.9),
y = c(1,1),
linetype = 3, col = "grey90"
)
# Save -------------------------------------------------------------------
ggsave(here("2021", "week04", "kenya_crops.png"),
crop_graph,
width = 10,
height = 14)