-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_biscale.R
217 lines (197 loc) · 4.55 KB
/
map_biscale.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
# 1. Requirements ---------------------------------------------------------
library(biscale)
library(ggplot2)
library(tidyverse)
library(cowplot)
library(sf)
source("utils.R")
# 2. Reading and processing data ------------------------------------------
data <- st_read("data/biscale_bogota.gpkg") %>%
drop_na(CNSST1517L3,APSPM25MEAN2018L3)
# 3. Bar plot by categories -----------------------------------------------
data <- bi_class(
data,
x = CNSST1517L3,
y = APSPM25MEAN2018L3,
style = "quantile",
dim = 3
)
newdata <- data %>%
st_set_geometry(NULL) %>%
group_by(bi_class) %>%
summarise(total = n())
## Bar plot by categories:
##> categories: 1-3 | 2-3 | 3-3
cat1 <- newdata %>%
filter(bi_class %in% sprintf("%s-3",1:3))
b1 <- gg_barplot(x = cat1,nrow = 1)
##> categories: 1-2 | 2-2 | 3-2
cat2 <- newdata %>%
filter(bi_class %in% sprintf("%s-2",1:3))
b2 <- gg_barplot(x = cat2,nrow = 2)
##> categories: 1-1 | 2-1 | 3-1
cat3 <- newdata %>%
filter(bi_class %in% sprintf("%s-1",1:3))
b3 <- gg_barplot(x = cat3,nrow = 3)
##> Customization of bar plot
plot <- plot_grid(b1, b2, b3, ncol = 1)
barras <- plot +
draw_plot_label(
label = "Regions",
vjust = 0.1,
x = 0.08,
y = -0.25,
angle = 90,
size = 7
) +
draw_plot_label(
label = "Higher Index →",
x = 0.34,
y = 0.06,
size = 7
) +
draw_plot_label(
label = "Number PM2.5 →",
x = 0.90,
y = -0.1,
angle = 90,
size = 7
)
barras
# 4. Biscale map ---------------------------------------------------------
##> Customization of color palette
paleta <- bi_pal2(
pal = "DkViolet",
dim = 3
) +
theme(
panel.background = element_blank(),
axis.text.x = element_blank()
) +
labs(
x = "Index → ",
y = "MHI →"
)
##> Cooking maps
basemap <- ggplot() +
geom_sf(
data = dep,
lwd = 0.5,
fill = "#d9d9d9",
show.legend = FALSE,
color = "white"
) +
geom_sf(
data = data,
lwd = 0.0,
aes(fill = bi_class),
show.legend = FALSE
) +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()
) +
bi_scale_fill2(pal = "DkViolet", dim = 3)
basemap
##> North zone
data$title <- "North zone"
p1 <- gg_bimap(
data = data,
xlim = c(-77.10173463,-77.04276369),
ylim = c(-11.85954471,-11.81815091)
)
##> South zone
data$title <- "South zone"
p2 <- gg_bimap(
data = data,
xlim = c(-77.03788877,-76.95226743),
ylim = c(-12.22157088,-12.16004115)
)
##> East zone
data$title <- "East zone"
p3 <- gg_bimap(
data = data,
xlim = c(-76.82754759,-76.80022516),
ylim = c(-12.03109002,-12.01191144)
)
##> West zone
data$title <- "West zone"
p4 <- gg_bimap(
data = data,
xlim = c(-77.10442439,-77.05030405),
ylim = c(-12.09190529,-12.05301300)
)
# 5. Final plot -----------------------------------------------------------
end <- ggdraw() +
# North - - - - - - - - - - - - - - - - - -
draw_plot(
p1 +
theme(
legend.justification = "left",
plot.background = element_blank()
),
0.07, 0.6, 0.95, 0.30,
scale = 1.4
) +
# South - - - - - - - - - - - - - - - - - -
draw_plot(
p2 +
theme(
legend.justification = "left",
plot.background = element_blank()
),
0.38, 0.6, 0.95, 0.30,scale = 1.4
) +
# East - - - - - - - - - - - - - - - - - - -
draw_plot(
p3 +
theme(
legend.justification = "left",
plot.background = element_blank()
),
0.38, 0.15, 0.95, 0.30,scale = 1.4
) +
# West - - - - - - - - - - - - - - - - - - -
draw_plot(
p4 +
theme(
legend.justification = "left",
plot.background = element_blank()
),
0.07, 0.15, 0.95, 0.30,scale = 1.4
) +
# Map - - - - - - - - - - - - - - - - - - - -
draw_plot(
basemap + theme(
legend.justification = "top",
plot.background = element_blank()
), -0.25, 0.05, 0.95, 0.95,scale = 1.0
) +
# bar plot - - - - - - - - - - - - - - - - - -
draw_plot(
barras + theme(
legend.justification = "bottom",
plot.background = element_blank()
),
-0.32, -0.085, 0.9, 0.60,
scale = 0.30
) +
# legend - - - - - - - - - - - - - - - - - - -
draw_plot(
paleta + theme(
legend.justification = "bottom",
plot.background = element_blank()
),
-0.01, 0.53, 0.7, 0.7,
scale = 0.30
)
##> Export final plot in a png format
ggsave(
filename = "ouput/png/MHI_biscalemap.png",
plot = end,
width = 14,
height = 8,
bg = "white"
)