-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_plot.R
397 lines (328 loc) · 16.9 KB
/
04_plot.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright 2016 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
## General and plotting packages
library(readr) # write_csv
library(dplyr) # data muanipulation and summaries
library(ggplot2) # Plottiong
library(envreportutils) # for order_df and theme_soe ###
library(RColorBrewer) # Colour for plotting
library(ggthemes) # theme_map
## Spatial packages
library(sp)
library(geojsonio) # writing out geojson file of ecoregions
library(rmapshaper) # ms_dissolve
library(maptools) ###
library(bcmaps) ###
library(rgeos) ###
load("tmp/prot_areas_clean.rda")
load("tmp/ecoregions_clean.rda")
load("tmp/bec_clean.rda")
load("tmp/analyzed.rda")
source("fun.R")
## Terrestrial ---------------------------------------------------------------------
## Prep data frame for visualizations
cum_summary_t_eco <- order_df(cum_summary_t_eco, "ecoregion", "cum_percent_protected",
max, na.rm = TRUE, desc = TRUE)
cum_summary_t_eco$decade <- floor(cum_summary_t_eco$prot_date / 10) * 10
#########################################################################################
## Facet line-chart by ecoregion of cumulative percent protected over time (Print)
# Make a data frame of labels for current % protected
current_eco_t <- cum_summary_t_eco[cum_summary_t_eco$prot_date == max(cum_summary_t_eco$prot_date), ]
(ecoregion_t_facet_plot <- ggplot(cum_summary_t_eco,
aes(x = prot_date, y = cum_percent_protected)) +
geom_path(colour = "forestgreen") +
facet_wrap(~ecoregion, labeller = label_wrap_gen(width = 20), ncol = 6) +
scale_x_continuous(expand = c(0,0), breaks = function(x) round(seq(min(x),max(x), length.out = 5))) +
scale_y_continuous(breaks = seq(0,100, length.out = 3)) +
labs(x = "Year", y = "Cumulative Percent of Ecoregion Protected") +
theme_minimal() +
theme(axis.text = element_text(size = 8),
plot.margin = unit(c(5,5,5,5),"mm"),
panel.margin.x = unit(1, "lines")) +
geom_text(data = current_eco_t, x = 2002, y = 80,
aes(label = paste0(round(cum_percent_protected, 1), "%")),
size = 2.5))
# theme_soe_facet() +
# theme(panel.margin = unit(1, "mm"))
# plot(ecoregion_t_facet_plot)
###############################################################################
## Bar chart of current protection by ecoregion
# (summary_eco_t_plot <- ggplot(current_eco_t, aes(x = ecoregion, y = cum_percent_protected, colour = is_bc)) +
# scale_colour_manual(guide = "none", values = c(NA, "royalblue3")) +
# geom_bar(stat = "identity", fill = "#008000", size = 1) +
# coord_flip() +
# scale_y_continuous(breaks = seq(0, 100, by = 20), expand = c(0, 1.2)) +
# labs(x = "Ecoregion", y = "Percent Ecoregion Protected") +
# theme_soe() +
# theme(axis.text.y = element_text(colour = ifelse(current_eco_t$is_bc, "royalblue3", "black")),
# axis.line = element_blank(), panel.grid.major.y = element_blank()))
# plot(summary_eco_t_plot)
###############################################################################
## Make a facetted map of protection level by decade (Print)
carts_eco_t_by_decade <- cum_summary_t_eco %>%
group_by(ecoregion_code, ecoregion, decade) %>%
summarise(percent_protected = max(cum_percent_protected))
ecoregions_t_gg <- fortify(ecoregions_t_simp, region = "CRGNCD")
ecoregions_t_gg <- left_join(ecoregions_t_gg, carts_eco_t_by_decade, by = c("id" = "ecoregion_code"))
#
# (decade_t_facet_map <- ggplot(ecoregions_t_gg, aes(x = long, y = lat, group = group, fill = percent_protected)) +
# facet_wrap(~decade) +
# geom_polygon(colour = "grey80") +
# scale_fill_continuous(low = "white", high = "#008000") +
# coord_equal() +
# theme_map())
# plot(decade_t_facet_map)
###############################################################################
## Map of current level of protection by ecoregion (Print)
(current_t_map <- ecoregions_t_gg %>%
filter(decade == 2010) %>%
ggplot(aes(x = long, y = lat, group = group, fill = percent_protected)) +
geom_polygon(colour = "grey80") +
scale_fill_continuous(low = "white", high = "#008000") +
coord_equal() +
theme_map())
# plot(current_t_map)
# Marine ------------------------------------------------------------------
## Prep data frame for visualizations
cum_summary_m_eco <- order_df(cum_summary_m_eco, "ecoregion", "cum_percent_protected", max, na.rm = TRUE, desc = TRUE)
cum_summary_m_eco$decade <- floor(cum_summary_m_eco$prot_date / 10) * 10
# Make a data frame of labels for current % protected
current_eco_m <- cum_summary_m_eco[cum_summary_m_eco$prot_date == max(cum_summary_m_eco$prot_date), ]
###############################################################################
## Facet line-chart by ecoregion of cumulative percent protected over time (Print)
#
# (ecoregion_m_facet_plot <- ggplot(cum_summary_m_eco,
# aes(x = prot_date, y = cum_percent_protected)) +
# geom_path(colour = "#253494") +
# facet_wrap(~ecoregion, labeller = label_wrap_gen(width = 20), ncol = 6) +
# scale_x_continuous(expand = c(0,0), breaks = function(x) round(seq(min(x),max(x), length.out = 5))) +
# # scale_y_continuous(breaks = seq(0,100, length.out = 5)) +
# labs(x = "Year", y = "Cumulative Percent of Ecoregion Protected") +
# theme_minimal() +
# theme(panel.margin.x = unit(1.5, "lines"),
# axis.text = element_text(size = 8)) +
# geom_text(data = current_eco_m, x = 1980, y = 80,
# aes(label = paste(round(cum_percent_protected, 1), "%")),
# size = 3))
# theme_soe_facet() +
# theme(panel.margin = unit(1, "mm"))
# plot(ecoregion_m_facet_plot)
###############################################################################
## Bar chart of current protection by ecoregion (Print & Web)
(summary_eco_m_plot <- ggplot(current_eco_m,
aes(x = ecoregion, y = cum_percent_protected, fill = cum_percent_protected)) +
scale_fill_distiller(limits = c(0, max(current_eco_m$cum_percent_protected, na.rm = TRUE)),
palette = "YlGnBu", direction = 1, guide = "none") +
geom_bar(stat = "identity") +
coord_flip() +
geom_text(aes(label = sprintf("%.1f%s", cum_percent_protected, "%")),
nudge_y = c(rep(-0.7, 5), 0.7), colour = c(rep("white", 5), "black"), size = 5) +
#scale_y_continuous(breaks = seq(0, 100, by = 20), expand = c(0, 1.2)) +
labs(x = "Marine Ecoregion", y = "Percent Protected") +
theme_soe() +
theme(axis.line = element_blank(), panel.grid.major.y = element_blank(),
plot.margin = unit(c(2,1,1,1), "lines"),
axis.title = element_text(size = 14),
axis.text = element_text(size = 12)))
# plot(summary_eco_m_plot)
###############################################################################
## Make a facetted map of protection level by decade
carts_eco_m_by_decade <- cum_summary_m_eco %>%
group_by(ecoregion_code, ecoregion, decade) %>%
summarise(percent_protected = max(cum_percent_protected))
ecoregions_m_gg <- fortify(ecoregions_m_simp, region = "CRGNCD")
ecoregions_m_gg_decade <- left_join(ecoregions_m_gg, carts_eco_m_by_decade, by = c("id" = "ecoregion_code"))
# (decade_m_facet_map <- ggplot(ecoregions_m_gg_decade, aes(x = long, y = lat, group = group, fill = percent_protected)) +
# facet_wrap(~decade) +
# geom_polygon(data = ecoregions_m_gg_decade[!ecoregions_m_gg_decade$hole, ],
# aes(fill = percent_protected), colour = "grey70") +
# geom_polygon(data = ecoregions_m_gg_decade[ecoregions_m_gg_decade$hole, ], fill = "white",
# colour = "grey70") +
# scale_fill_distiller(limits = c(0, max(ecoregions_m_gg_decade$percent_protected, na.rm = TRUE)),
# palette = "YlGnBu", direction = 1, na.value = brewer.pal(6, "YlGnBu")[1]) +
# coord_equal() +
# labs(fill = "Percent of Marine\nEcoregion Protected\n") +
# theme_map() +
# theme(legend.key = element_rect(colour = "grey70", size = 2), legend.direction = "horizontal",
# legend.title = element_text(size = 11), legend.position = c(0.7,0.1)))
# plot(decade_m_facet_map)
###############################################################################
## Map of current level of marine protection by ecoregion (Print & Web)
eco_m_gg_current <- left_join(ecoregions_m_gg, current_eco_m, by = c("id" = "ecoregion_code"))
## Print Version
(current_m_map <- ggplot(eco_m_gg_current, aes(x = long, y = lat, group = group)) +
geom_polygon(data = eco_m_gg_current[!eco_m_gg_current$hole, ],
aes(fill = cum_percent_protected), colour = "grey70") +
geom_polygon(data = eco_m_gg_current[eco_m_gg_current$hole, ], fill = "white",
colour = "grey70") +
scale_fill_distiller(limits = c(0, max(eco_m_gg_current$cum_percent_protected, na.rm = TRUE)),
palette = "YlGnBu", direction = 1, na.value = brewer.pal(6, "YlGnBu")[1]) +
coord_equal() +
labs(fill = "Percent of Marine\nEcoregion Protected\n") +
theme_map() +
theme(legend.key = element_rect(colour = "grey70", size = 2), legend.direction = "horizontal",
legend.title = element_text(size = 14, face = "bold"), legend.text = element_text(size = 14),
legend.key.height = unit(1, "cm"), legend.key.width = unit(0.58, "cm"),
plot.margin = unit(c(0,0,0,0), "lines")))
# plot(current_m_map)
## Web Version
endeavour <- coordinates(bc_carts[bc_carts$ZONE_ID == "700020100", ])
(annotated_m_map <- current_m_map +
geom_point(aes(x = endeavour[1], y = endeavour[2]), inherit.aes = FALSE,
colour = "#253494", size = 2) +
annotate("text", x = endeavour[1], y = endeavour[2] - 40000, hjust = 0.2, size = 4.5,
label = "Endeavour Hydrothermal Vents\nMarine Protected Area"))
# BEC ---------------------------------------------------------------------
# aggregate prot_areas_bec by poly_id
# prot_areas_bec_agg <- raster::aggregate(prot_areas_bec, by = "poly_id",
# sums = list(list(sum, "prot_area")))
#
# bec_t_prot_simp <- merge(bec_t_simp, prot_areas_bec_agg, by = "poly_id")
# bec_t_prot_simp$poly_id <- as.character(bec_t_prot_simp$poly_id)
# bec_t_prot_simp$percent_protected <- bec_t_prot_simp$prot_area / bec_t_prot_simp$area * 100
#
# bec_t_gg <- fortify(bec_t_prot_simp, region = "poly_id")
#
# bec_t_gg <- left_join(bec_t_gg, bec_t_prot_simp@data, by = c("id" = "poly_id"))
# bec_t_gg$percent_protected[is.na(bec_t_gg$percent_protected)] <- 0
#
# (bec_prot_map <- ggplot(bec_t_gg, aes(x = long, y = lat, group = group, fill = percent_protected)) +
# geom_polygon() +
# scale_fill_distiller(palette = "YlGn", direction = 1) +
# guides(fill = guide_colourbar(title = "Percent of\nEcosystem\nProtected",
# title.position = "left", label.position = "right")) +
# coord_fixed() +
# theme_map() +
# theme(legend.title = element_text(size = 12, vjust = 1),
# legend.text = element_text(size = 11), legend.position = c(0,0.03),
# legend.background = element_rect(fill = NA),
# legend.key.width = unit(1, "cm"), legend.key.height = unit(1, "cm"),
# # panel.background = element_rect(fill = "grey90", colour = NA),
# plot.margin = margin(0,0,0,0)))
#plot(bec_prot_map)
#####################################################
## Biogeoclimatic Zones for BC (Print & Web)
bec_zone_gg <- fortify(bec_zone_simp, region = "ZONE")
(bec_zone_map <- ggplot(bec_zone_gg, aes(x = long, y = lat, group = group, fill = id)) +
geom_polygon() +
scale_fill_manual(values = bgc_colours(), guide = "none") +
coord_fixed() +
ggtitle("Biogeoclimatic Zones of B.C.") +
theme_map() +
theme(plot.margin = margin(2,0,1,0, "lines"),
plot.title = element_text(hjust = 0.3, size = 14,
margin = margin(0,0,0,0, "pt"))))
# Temp fix for UTF8 chars in Zone Summary
## Status of PPA by BGZ bar chart (Print and Web)
prot_areas_bec_summary$ZONE_NAME <- gsub(" \x97 ", "--", prot_areas_bec_summary$ZONE_NAME)
prot_areas_bec_summary$ZONE_NAME <- gsub("--", " — ", prot_areas_bec_summary$ZONE_NAME)
zone_summary <- prot_areas_bec_summary %>%
order_df("ZONE_NAME", "percent_protected", fun = max)
(zone_barplot <- ggplot(zone_summary, aes(x = ZONE_NAME, y = percent_protected,
fill = ZONE)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = bgc_colours(), guide = "none") +
coord_flip() +
# ggtitle("Percent of Biogeoclimatic Zones\nDesignated Within\nParks & Protected Areas") +
labs(x = "Biogeoclimatic Zone\n", y = "Percent Protected") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0),
panel.grid.major.x = element_line(colour = "grey85"),
panel.grid.minor.x = element_line(colour = "grey90"),
panel.grid.major.y = element_blank(),
plot.margin = unit(c(3,1,1,1), "lines"),
axis.title = element_text(size = 14),
axis.text = element_text(size = 12))
)
#plot(zone_barplot)
## Plot protected areas
bc_bound_hres <- bc_bound_hres(class = "sp")
bc_bound_hres <- fix_geo_problems(bc_bound_hres)
bc_fortified <- fortify(bc_bound_hres, region = "PRUID")
# (gg_bc <- ggplot(bc_fortified, aes(x = long, y = lat, group = group)) +
# geom_polygon(fill = NA, colour = "grey70", size = 0.5) +
# theme_map() +
# coord_fixed())
## Make a simplified spdf of terrestrial and marine protected areas
prot_areas_t <- rmapshaper::ms_dissolve(prot_areas_eco_t)
prot_areas_t$BIOME <- "Terrestrial"
prot_areas_m <- rmapshaper::ms_dissolve(prot_areas_eco_m)
prot_areas_m$BIOME <- "Marine"
prot_areas_map <- bind_spdf(prot_areas_t, prot_areas_m)
prot_areas_map <- fix_geo_problems(prot_areas_map)
gg_prot <- gg_fortify(prot_areas_map)
(prot_map <- gg_bc +
geom_polygon(data = gg_prot, aes(x = long, y = lat, group = group, fill = BIOME)) +
scale_fill_manual(name = "Biome",
values = c("Terrestrial" = "#006837", "Marine" = "#253494")) +
coord_fixed() +
theme_map() +
theme(legend.title = element_text(size = 14, vjust = 1, face = "bold"),
legend.text = element_text(size = 14),
legend.position = c(0.05,0.05),
legend.background = element_rect(fill = NA),
legend.key.width = unit(1, "cm"), legend.key.height = unit(1, "cm")))
# Output charts ----------------------------------------
# png("out/prot_map.png", width = 600, height = 550, units = "px", type = "cairo-png")
# plot(prot_map)
# dev.off()
png_retina("out/prot_map.png", width = 600, height = 550, units = "px", type = "cairo-png")
plot(prot_map)
dev.off()
## Multiplot of marine map and bar chart
# png(filename = "out/marine_chart.png", width = 900, height = 550, units = "px", type = "cairo-png")
# multiplot(annotated_m_map, summary_eco_m_plot, cols = 2, widths = c(3,2))
# dev.off()
png_retina(filename = "out/marine_map.png", width = 500, height = 500, units = "px", type = "cairo-png")
annotated_m_map
dev.off()
# svg_px(file = "out/marine_map.svg", width = 500, height = 500)
# annotated_m_map
# dev.off()
# png_retina(filename = "out/marine_chart.png", width = 500, height = 500, units = "px", type = "cairo-png")
# summary_eco_m_plot
# dev.off()
# png_retina(filename = "out/marine_chart.png", width = 500, height = 500, units = "px", type = "cairo-png")
# summary_eco_m_plot
# dev.off()
svg_px(file = "out/marine_chart.svg", width = 500, height = 500)
summary_eco_m_plot
dev.off()
## BGC plots
# png("out/bgc_multiplot.png", width = 930, height = 430, units = "px")
# multiplot(zone_barplot, bec_zone_map, cols = 2)
# dev.off()
svg_px("out/bec_zone_chart.svg", width = 500, height = 500)
zone_barplot
dev.off()
png_retina("out/bec_zone_map.png", width = 500, height = 500, units = "px")
plot(bec_zone_map)
dev.off()
library(magick)
beczomemap <- image_read("out/bec_zone_map.png")
(bec_zone_map_small <- image_resize(beczomemap, "1000x1000"))
image_write(bec_zone_map_small,
path = "out/bec_zone_map_small.png",
format = "jpg")
## Output terrestrial ecoregions as geojson for the visualization:
ecoregions_t_out <- ecoregions_t_simp[, "CRGNCD"]
names(ecoregions_t_out) <- "ECOREGION_CODE"
file.remove("out/ecoregions.geojson")
spTransform(ecoregions_t_out, CRS("+init=epsg:4326")) %>%
geojson_write(file = "out/ecoregions.geojson", precision = 5)
# Function to list objects of a certain class in the global environment
ls_class <- function(cls) {
ls(.GlobalEnv)[sapply(ls(.GlobalEnv),function(x) cls %in% class(get(x)))]
}
save(list = ls_class("ggplot"), file = "tmp/out_gg.rda")
save(list = ls_class("SpatialPolygonsDataFrame"), file = "tmp/out_spatial.rda")
save(list = ls_class("data.frame"), file = "tmp/out_data.rda")