-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppendix.Rmd
2843 lines (2258 loc) · 116 KB
/
Appendix.Rmd
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "HWI_NDVI_parks_Appendix"
author: "Yan Lam Grace, Lou"
date: "2024-04-19"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# **Overview**
In this study, we will be modelling the monthly mean Normalized Difference Vegetation Index (NDVI) of 25 selected Canadian national parks over 22 years with the frequency of HWIs in each park over 12 years to identify trends.
```{r loading packages, message = TRUE, warning = FALSE}
library(ggplot2) # for plots
library(dplyr) # for pipes
library(lubridate) #convert whole columns to dates
library(zoo) #dates as year month
library(canadianmaps) #import annotated map of Canada
library(sf) # spatial data
library(sp) #Spatial Points function
library(rstudioapi) #for creating colour palette
library(grDevices) #for creating colour palette
library(fBasics) #for creating colour palette
library(mgcv) #gam
library(geodata) # for downloading provinces
library(terra) #shape file
library(xml2)
library(rvest)
library(raster)
library(tidyterra) #for geom_spat*() functions (study site map)
library(mgcViz)
library(gratia) #for plotting gam smooths in ggplot2
library(viridis)
library(gridExtra) # for paired plots (Banff case study)
```
```{r loading data, echo = FALSE, warning = FALSE}
# these files are all obtained through the code below, they are loaded in advanced to reduce computation time
park_coordinates <- read.csv("data/park_coordinates.csv")
model1 <- readRDS("data/models/model1")
provinces <- readRDS("data/shapefiles/CAprovinces_map.rds")
provinces_bg <- readRDS("data/shapefiles/Canmap.rds")
bg_reproject <- readRDS("figures/old_figures/bg_reproject.rds")
Can_crop <- readRDS("figures/old_figures/Can_crop.rds")
park_coordinates_esri <- readRDS("data/park_coordinates_esri.rds")
provinces_sf <- readRDS("data/shapefiles/provinces_sf.rds")
RESULTS_df <- read.csv("C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/models/model_results/parksndvi.csv")
RESULTS2_df <- read.csv("C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/models/model_results/morendvi.csv")
ndvi2010_2021_gam <- readRDS("data/models/ndvi2010_2021_gam")
NDVI_2000_2021 <- read.csv("C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/models/model_results/NDVI_2000_2021.csv")
all_ndvi_gam <- readRDS("data/models/all_ndvi_gam")
hwi_ndvi <- readRDS("C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/hwi_ndvi.rds")
all_parks_model <- readRDS("data/models/all_parks_model.rds")
RESULTS3 <- readRDS("data/models/model_results/RESULTS3")
max <- readRDS("data/models/model_results/max")
```
# **Data collection and visualization**
We obtained daily human-wildlife coexistence data between 2010-2021 in 35 selected Canadian national parks and historical sites from the Government of Canada Open Government database. Among the 9 recorded incident types, only those classified as “Human Wildlife Interaction” (HWI) were selected for. Incidents that involved unknown species or NA values were further omitted from the dataset, resulting in a total of 47,626 incidents for 152 species across the 12 years in 30 parks.
```{r loading HWI data and cleaning the data, echo = TRUE}
# importing data
animals_involved <- read.csv("data/hwi/pca-human-wildlife-coexistence-animals-involved-detailed-records-2010-2021.csv")
# filter out all the human wildlife interactions ----
HWI <- animals_involved %>%
filter(Incident.Type %in% c("Human Wildlife Interaction"))
# Cleaning the first nations heritage site in HWI data ----
HWI$Protected.Heritage.Area[HWI$Protected.Heritage.Area == "Saoy\xfa-?ehdacho National Historic Site of Canada"]<- "Grizzly Bear Mountain and Scented Grass Hills"
# Convert dates in HWI from characters to date ----
HWI$Incident.Date <- ymd(HWI$Incident.Date)
# Add a column "Incident Year" to HWI ----
HWI$Incident.Year <- as.numeric(format(HWI$Incident.Date, "%Y"))
# Add a colume "Incident Month" to HWI ----
HWI$Incident.Month <- as.numeric(format(HWI$Incident.Date, "%m"))
# Combine year and month into a single column
HWI$year_month <- as.yearmon(paste(HWI$Incident.Year, HWI$Incident.Month), "%Y %m")
# Renaming columns in HWI
HWI_parks <- HWI %>%
rename("park" = "Protected.Heritage.Area") %>%
rename("species" = "Species.Common.Name") %>%
rename("year" = "Incident.Year") %>%
rename("month" = "Incident.Month") %>%
rename("HWI" = "Incident.Type")
# Shortening park names
HWI_parks$park[HWI_parks$park == "Banff National Park of Canada"]<- "Banff"
HWI_parks$park[HWI_parks$park == "Pacific Rim National Park Reserve of Canada"]<- "Pacific_Rim"
HWI_parks$park[HWI_parks$park == "Waterton Lakes National Park of Canada"]<- "Waterton_Lakes"
HWI_parks$park[HWI_parks$park == "Kejimkujik National Park and National Historic Site of Canada"]<- "Kejimkujik"
HWI_parks$park[HWI_parks$park == "Jasper National Park of Canada"]<- "Jasper"
HWI_parks$park[HWI_parks$park == "Forillon National Park of Canada"]<- "Forillon"
HWI_parks$park[HWI_parks$park == "Prince Albert National Park of Canada"]<- "Prince_Albert"
HWI_parks$park[HWI_parks$park == "Kootenay National Park of Canada"]<- "Kootenay"
HWI_parks$park[HWI_parks$park == "Glacier National Park of Canada"]<- "Glacier"
HWI_parks$park[HWI_parks$park == "Wapusk National Park of Canada"]<- "Wapusk"
HWI_parks$park[HWI_parks$park == "Grasslands National Park of Canada"]<- "Grasslands"
HWI_parks$park[HWI_parks$park == "Bruce Peninsula National Park of Canada"]<- "Bruce_Peninsula"
HWI_parks$park[HWI_parks$park == "Yoho National Park of Canada"]<- "Yoho"
HWI_parks$park[HWI_parks$park == "Terra Nova National Park of Canada"]<- "Terra_Nova"
HWI_parks$park[HWI_parks$park == "Mount Revelstoke National Park of Canada"]<- "Mount_Revelstoke"
HWI_parks$park[HWI_parks$park == "Elk Island National Park of Canada"]<- "Elk_Island"
HWI_parks$park[HWI_parks$park == "Georgian Bay Islands National Park of Canada"]<- "Georgian_Bay_Islands"
HWI_parks$park[HWI_parks$park == "Prince of Wales Fort National Historic Site of Canada"]<- "Prince_of_Wales_Fort"
HWI_parks$park[HWI_parks$park == "Point Pelee National Park of Canada"]<- "Point_Pelee"
HWI_parks$park[HWI_parks$park == "Thousand Islands National Park of Canada"]<- "Thousand_Islands"
HWI_parks$park[HWI_parks$park == "Wood Buffalo National Park of Canada"]<- "Wood_Buffalo"
HWI_parks$park[HWI_parks$park == "Prince Edward Island National Park of Canada"]<- "Prince_Edward_Island"
HWI_parks$park[HWI_parks$park == "Ivvavik National Park of Canada"]<- "Ivvavik"
HWI_parks$park[HWI_parks$park == "Kouchibouguac National Park of Canada"]<- "Kouchibouguac"
HWI_parks$park[HWI_parks$park == "Grizzly Bear Mountain and Scented Grass Hills"]<- "Grizzly_Bear_Mountain"
HWI_parks$park[HWI_parks$park == "Fundy National Park of Canada"]<- "Fundy"
HWI_parks$park[HWI_parks$park == "Nahanni National Park Reserve of Canada"]<- "Nahanni"
HWI_parks$park[HWI_parks$park == "Aulavik National Park of Canada"]<- "Aulavik"
HWI_parks$park[HWI_parks$park == "Sable Island National Park Reserve"]<- "Sable_Island"
HWI_parks$park[HWI_parks$park == "Fathom Five National Marine Park of Canada"]<- "Fathom_Five"
HWI_parks$park[HWI_parks$park == "Fort Walsh National Historic Site of Canada"]<- "Fort_Walsh"
# Cleaning the species by omitting unknowns
HWI_parks <- HWI_parks[HWI_parks$species != "None",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown bear",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown bird",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown bat",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown ungulate",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown gull",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown canid",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown snake",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown fish",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown Duck",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown grouse",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown rodent",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown Myotis bat",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown raptor",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown owl",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown sea lion",]
HWI_parks <- HWI_parks[HWI_parks$species != "Unknown deer",]
# save HWI_parks ----
write.csv(HWI_parks, "C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/old/hwi_parks.csv", row.names=FALSE)
HWI_parks <- read.csv("data/old/hwi_parks.csv")
```
We explored the data by plotting a simple map using coordinates from Google Maps to visualise the locations of the parks, and grouped the incidents according to province, year, month, seasons, species for more data exploration.
```{r data exploration visualisations, echo = TRUE, warning = FALSE}
#Count number of incidents by park
incident_count <- HWI_parks %>%
count(HWI_parks$park)
# import Canada shape and extract boundaries only
canadashape <- st_as_sf(PROV) %>%
st_geometry()
# import coordinates of all national parks, coordinates obtained from Google Maps
park_coordinates <- read.csv("data/park_coordinates.csv")
# convert coordinates into spatial data
park_location <- SpatialPoints(park_coordinates[, c("longitude", "latitude")])
# plot parks
plot(canadashape)
sp::plot(park_location, add = TRUE, col = 'coral', pch = 19, cex = 0.5)
#Other visualisations ----
#Create another data frame by grouping according to months and years
HWI_grouped_date <- aggregate(HWI ~ year_month + park, data = HWI_parks, FUN = "length")
HWI_grouped_date$year_month <- as.yearmon(HWI_grouped_date$year_month)
HWI_grouped_date$year <- lubridate::year(HWI_grouped_date$year_month)
HWI_grouped_date$month <- lubridate::month(HWI_grouped_date$year_month)
# plot HWI across years and months ----
ggplot() +
geom_point(data = HWI_grouped_date, aes(x = year_month, y = HWI, col = park)) +
xlab("Time") +
ylab("Human-wildlife interactions") +
# using heat palette for parks
scale_color_manual(values = heatPalette(n=31)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
# group parks according to province ----
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Terra_Nova")] <- "NL"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Prince_Edward_Island")] <- "PE"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Kejimkujik", "Sable_Island")] <- "NS"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Kouchibouguac", "Fundy")] <- "NB"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Forillon")] <- "QC"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Bruce_Peninsula", "Fathom_Five", "Georgian_Bay_Islands", "Point_Pelee", "Thousand_Islands")] <- "ON"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Prince_of_Wales_Fort", "Wapusk")] <- "MB"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Prince_Albert")] <- "SK"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Banff", "Elk_Island", "Grasslands","Jasper", "Waterton_Lakes", "Wood_Buffalo")] <- "AB"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Glacier", "Kootenay", "Mount_Revelstoke", "Pacific_Rim", "Yoho")] <- "BC"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Ivvavik")] <- "YT"
HWI_grouped_date$park[HWI_grouped_date$park %in% c("Aulavik", "Grizzly_Bear_Mountain", "Nahanni")] <- "NT"
# plot parks according to province by month ----
HWI_province <- HWI_grouped_date %>%
rename("province" = "park")
ggplot() +
geom_point(data = HWI_province, aes(x = year_month, y = HWI, col = province), alpha = 0.25) +
xlab("Time") +
ylab("Human-wildlife interactions") +
# using rainbow palette for parks
scale_color_manual(values = rainbowPalette(n=12)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
# plot species sightings ----
HWI_grouped_species <- aggregate(HWI ~ year_month + park + species + month + year, data = HWI_parks, FUN = "length")
ggplot() +
geom_point(data = HWI_grouped_species, aes(x = species, y = HWI, col = species), alpha = 0.8) +
xlab("Time") +
ylab("Human-wildlife interactions") +
# using rainbow palette for parks
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
#too much data
#still too much data
ggplot(data = HWI_grouped_species, aes(x = species, y = HWI)) +
geom_bar(stat = "identity", position = position_dodge(), width=0.5) +
scale_x_discrete(guide = guide_axis(n.dodge=1.5)) +
ylab("No. of Sightings") +
xlab("Species") +
theme(axis.text.x = element_text(angle = 90), axis.title.x = element_text(size=2, family = "sans", face = "bold"),)
# count no. of sightings per species
sightings <- HWI_grouped_species %>%
count(HWI_grouped_species$species)
# filter the sightings >10 by creating a subset (top 14 species)
filtered_sightings <- subset(HWI_grouped_species, HWI_grouped_species$HWI>10)
# plot species with >10 sightings (top 14 species) ----
ggplot(data = filtered_sightings, aes(x = species, y = HWI)) +
geom_bar(stat = "identity", position = position_dodge(), width=0.5) +
scale_x_discrete(guide = guide_axis(n.dodge=1.5)) +
ylab("No. of Sightings") +
xlab("Species") +
theme(axis.text.x = element_text(angle = 90), axis.title.x = element_text(size=2, family = "sans", face = "bold"),)
# plot species with >10 sightings (top 8 species) with time ----
ggplot() +
geom_point(data = filtered_sightings, aes(x = year_month, y = HWI, col = species), alpha = 0.8) +
xlab("Time") +
ylab("Frequency") +
# using rainbow palette for parks
scale_color_manual(values = rainbowPalette(n=14)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
# plot HWI according to seasons ----
# creating a new dataset with seasons
HWI_with_season <- HWI_parks %>%
mutate(season = case_when(month %in% 3:5 ~ 'Spring',
month %in% 6:8 ~ 'Summer',
month %in% 9:11 ~ 'Autumn',
TRUE ~ 'Winter'))
#creating a new dataframe with seasons
HWI_grouped_season <- aggregate(HWI ~ year_month + park + species + season, data = HWI_with_season, FUN = "length")
HWI_grouped_season %>%
count(HWI_grouped_season$season)
sum(HWI_grouped_season$HWI)
#plotting the number of HWI according to season
ggplot(data = HWI_grouped_season, aes(x = season, y = HWI)) +
geom_bar(stat = "identity", position = position_dodge(), width=0.5) +
scale_x_discrete(guide = guide_axis(n.dodge=1.5)) +
ylab("HWI Frequency") +
xlab("Season") +
theme(axis.title.x = element_text(size=8, family = "sans", face = "bold"),)
# filtering the species with >10 sightings ----
filtered_season_sightings <- subset(HWI_grouped_season, HWI_grouped_season$HWI>10)
# plotting the number of sightings of species >10 sightings according to season ----
ggplot() +
geom_point(data = filtered_season_sightings, aes(x = season, y = HWI, col = species), alpha = 0.8) +
xlab("Season") +
ylab("No. of Sightings") +
# using rainbow palette for parks
scale_color_manual(values = rainbowPalette(n=14)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
```
A Poisson generalized additive model (GAM) was fitted to the data of Jasper National Park to visualize the normal trend of HWIs across the years, with park and species included as random effects to account for the variability.
```{r model for visualising normal trend, eval = FALSE}
# models for visualising the normal trend ----
HWI_grouped_species <- HWI_grouped_species %>%
mutate(park = factor(park))
HWI_grouped_species$species <- as.factor(HWI_grouped_species$species)
model1 <- gam(HWI ~
s(park, bs = "fs") +
s(species, bs = "fs") +
#Add a random effect for species
ti(year, park, k = 12, bs = "fs") +
#Adjust for a random effect of park, done
ti(month, park, k = 8, bs = "fs"),
family = "poisson",
data = HWI_grouped_species, method = "REML")
summary(model1)
plot(model1, pages = 1)
```
```{r visualise the normal trend in Jasper, echo = TRUE, message = FALSE}
# first 6 residuals of model 1
head(residuals(model1))
```
```{r}
# add the residuals as a new column into the HWI_grouped_species dataframe ----
HWI_grouped_species$residuals <- residuals(model1)
# looking at the distribution of the residuals
hist(HWI_grouped_species$residuals)
#look at the trend in Jasper ----
Jasper_trend <- HWI_grouped_species %>%
filter(park %in% c("Jasper"))
#plot the trend of residuals by year in Jasper ----
ggplot() +
geom_hline(aes(yintercept = 0), col = "grey70", linetype = "dashed") +
geom_point(data = Jasper_trend, aes(x = year_month, y = residuals, col = species)) +
xlab("Date") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "none",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
# Look at the trend of residuals by month in Jasper ----
Jasper_jan_trend <- Jasper_trend %>%
filter(month %in% c("1"))
Jasper_feb_trend <- Jasper_trend %>%
filter(month %in% c("2"))
Jasper_mar_trend <- Jasper_trend %>%
filter(month %in% c("3"))
Jasper_apr_trend <- Jasper_trend %>%
filter(month %in% c("4"))
Jasper_may_trend <- Jasper_trend %>%
filter(month %in% c("5"))
Jasper_jun_trend <- Jasper_trend %>%
filter(month %in% c("6"))
Jasper_jul_trend <- Jasper_trend %>%
filter(month %in% c("7"))
Jasper_aug_trend <- Jasper_trend %>%
filter(month %in% c("8"))
Jasper_sep_trend <- Jasper_trend %>%
filter(month %in% c("9"))
Jasper_oct_trend <- Jasper_trend %>%
filter(month %in% c("10"))
Jasper_nov_trend <- Jasper_trend %>%
filter(month %in% c("11"))
Jasper_dec_trend <- Jasper_trend %>%
filter(month %in% c("12"))
# plot the monthly residual trend data in Jasper by year ----
ggplot() +
geom_point(data = Jasper_jan_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_feb_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_mar_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_apr_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_may_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_jun_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_jul_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_aug_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_sep_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_oct_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_nov_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
ggplot() +
geom_point(data = Jasper_dec_trend, aes(x = year, y = residuals)) +
xlab("Year") +
ylab("Residuals") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size=12, family = "sans", face = "bold"),
axis.title.x = element_text(size=12, family = "sans", face = "bold"),
axis.text.y = element_text(size=10, family = "sans"),
axis.text.x = element_text(size=10, family = "sans"),
legend.position = "right",
legend.title = element_text(face = "bold"),
legend.background = element_blank(),
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA),
plot.margin = unit(c(0.2,0.1,0.2,0.2), "cm"))
```
To prepare for modelling, we obtained polygons for the National Parks and National Park Reserves of Canada Legislative Boundaries from the Government of Canada Open Government database. Among the 30 sites recorded in the HWI data, boundaries for 5 of them were unavailable. These sites were excluded from the analysis. Our final study area therefore consists of 25 parks across the country.
```{r importing province polygons and extracting park polygons, echo = TRUE}
# importing polygons with sf ----
ABpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_AB_2023-09-08/CLAB_AB_2023-09-08.shp")
plot(ABpolygon)
saveRDS(ABpolygon,file ="data/old/ABpolygon.rds")
BCpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_BC_2023-09-08/CLAB_BC_2023-09-08.shp")
plot(BCpolygon)
saveRDS(BCpolygon,file ="data/old/BCpolygon.rds")
MBpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_MB_2023-09-08/CLAB_MB_2023-09-08.shp")
plot(MBpolygon)
saveRDS(MBpolygon,file ="data/old/MBpolygon.rds")
NBpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_NB_2023-09-08/CLAB_NB_2023-09-08.shp")
plot(NBpolygon)
saveRDS(NBpolygon,file ="data/old/NBpolygon.rds")
NLpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_NL_2023-09-08/CLAB_NL_2023-09-08.shp")
plot(NLpolygon)
saveRDS(NLpolygon,file ="data/old/NLpolygon.rds")
NSpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_NS_2023-09-08/CLAB_NS_2023-09-08.shp")
plot(NSpolygon)
saveRDS(NSpolygon,file ="data/old/NSpolygon.rds")
NTpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_NT_2023-09-08/CLAB_NT_2023-09-08.shp")
plot(NTpolygon)
saveRDS(NTpolygon,file ="data/old/NTpolygon.rds")
NUpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_NU_2023-09-08/CLAB_NU_2023-09-08.shp")
plot(NUpolygon)
saveRDS(NUpolygon,file ="data/old/NUpolygon.rds")
ONpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_ON_2023-09-08/CLAB_ON_2023-09-08.shp")
plot(ONpolygon)
saveRDS(ONpolygon,file ="data/old/ONpolygon.rds")
PEpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_PE_2023-09-08/CLAB_PE_2023-09-08.shp")
plot(PEpolygon)
saveRDS(PEpolygon,file ="data/old/PEpolygon.rds")
QCpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_QC_2023-09-08/CLAB_QC_2023-09-08.shp")
plot(QCpolygon)
saveRDS(QCpolygon,file ="data/old/QCpolygon.rds")
SKpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_SK_2023-09-08/CLAB_SK_2023-09-08.shp")
plot(SKpolygon)
saveRDS(SKpolygon,file ="data/old/SKpolygon.rds")
YTpolygon <- st_read("data/shapefiles/ca_provinces/CLAB_YT_2023-09-08/CLAB_YT_2023-09-08.shp")
plot(YTpolygon)
saveRDS(YTpolygon,file ="data/old/YTpolygon.rds")
# fitering for my 30 parks out of all the parks in each polygon ----
#AB
waterton_lakes <- ABpolygon[ABpolygon$CLAB_ID == "WATE", ]
plot(waterton_lakes)
saveRDS(waterton_lakes,file ="data/old/waterton_lakes.rds")
elk_island <- ABpolygon[ABpolygon$CLAB_ID == "ELKI", ]
plot(elk_island)
saveRDS(elk_island,file ="data/old/elk_island.rds")
jasper <- ABpolygon[ABpolygon$CLAB_ID == "JASP", ]
plot(jasper)
saveRDS(jasper,file ="data/old/jasper.rds")
wood_buffalo <- ABpolygon[ABpolygon$CLAB_ID == "WOOD", ]
plot(wood_buffalo)
saveRDS(wood_buffalo,file ="data/old/wood_buffalo.rds")
banff <- ABpolygon[ABpolygon$CLAB_ID == "BANF", ]
plot(banff)
saveRDS(banff,file ="data/old/banff.rds")
# no polygon for grasslands
# BC
yoho <- BCpolygon[BCpolygon$CLAB_ID == "YOHO", ]
plot(yoho)
saveRDS(yoho,file ="data/old/yoho.rds")
kootenay <- BCpolygon[BCpolygon$CLAB_ID == "KOOT", ]
plot(kootenay)
saveRDS(kootenay,file ="data/old/kootenay.rds")
mount_revelstoke <- BCpolygon[BCpolygon$CLAB_ID == "REVE", ]
plot(mount_revelstoke)
saveRDS(mount_revelstoke,file ="data/old/mount_revelstoke.rds")
pacific_rim <- BCpolygon[BCpolygon$CLAB_ID == "PRIM", ]
plot(pacific_rim)
saveRDS(pacific_rim,file ="data/old/pacific_rim.rds")
glacier <- BCpolygon[BCpolygon$CLAB_ID == "GLAC", ]
plot(glacier)
saveRDS(glacier,file ="data/old/glacier.rds")
# MB
wapusk <- MBpolygon[MBpolygon$CLAB_ID == "WAPU", ]
plot(wapusk)
saveRDS(wapusk,file ="data/old/wapusk.rds")
# no polygon for prince of wales fort
# NB
fundy <- NBpolygon[NBpolygon$CLAB_ID == "FUND", ]
plot(fundy)
saveRDS(fundy,file ="data/old/fundy.rds")
kouchibouguac <- NBpolygon[NBpolygon$CLAB_ID == "KOUC", ]
plot(kouchibouguac)
saveRDS(kouchibouguac,file ="data/old/kouchibouguac.rds")
# NL
terra_nova <- NLpolygon[NLpolygon$CLAB_ID == "NOVA", ]
plot(terra_nova)
saveRDS(terra_nova,file ="data/old/terra_nova.rds")
# NS
kejimkujik <- NSpolygon[NSpolygon$CLAB_ID == "KEJI", ]
plot(kejimkujik)
saveRDS(kejimkujik,file ="data/old/kejimkijik.rds")
# no polygon on sable island
# NT
aulavik <- NTpolygon[NTpolygon$CLAB_ID == "AULA", ]
plot(aulavik)
saveRDS(aulavik,file ="data/old/aulavik.rds")
nahanni <- NTpolygon[NTpolygon$CLAB_ID == "NAHA", ]
plot(nahanni)
saveRDS(nahanni,file ="data/old/nahanni.rds")
# no polygon for grizzly bear
# no NU parks in my data
#ON
fathom_five <- ONpolygon[ONpolygon$CLAB_ID == "FIVE", ]
plot(fathom_five)
saveRDS(fathom_five,file ="data/old/fathom_five.rds")
point_pelee <- ONpolygon[ONpolygon$CLAB_ID == "PELE", ]
plot(point_pelee)
saveRDS(point_pelee,file ="data/old/point_pelee.rds")
georgian_bay_islands <- ONpolygon[ONpolygon$CLAB_ID == "GBIS", ]
plot(georgian_bay_islands)
saveRDS(georgian_bay_islands,file ="data/old/georgian_bay_islands.rds")
thousand_islands <- ONpolygon[ONpolygon$CLAB_ID == "THIS", ]
plot(thousand_islands)
saveRDS(thousand_islands,file ="data/old/thousand_islands.rds")
# no polygon for bruce peninsula
# PE
prince_edward_island <- PEpolygon[PEpolygon$CLAB_ID == "PEIS", ]
plot(prince_edward_island)
saveRDS(prince_edward_island,file ="data/old/prince_edward_island.rds")
# QC
forillon <- QCpolygon[QCpolygon$CLAB_ID == "FORI", ]
plot(forillon)
saveRDS(forillon,file ="data/old/forillon.rds")
# SK
prince_albert <- SKpolygon[SKpolygon$CLAB_ID == "PALB", ]
plot(prince_albert)
saveRDS(prince_albert,file ="data/old/prince_albert.rds")
# YT
ivvavik <- YTpolygon[YTpolygon$CLAB_ID == "IVVA", ]
plot(ivvavik)
saveRDS(ivvavik,file ="data/old/ivvavik.rds")
# 5 parks do not have polygons
```
Using the 25 parks, we plotted a map to visualise all the study sites across the country.
```{r study site map, echo = TRUE}
# new map ----
# import coordinates of all national parks, coordinates obtained from Google Maps
park_coordinates <- read.csv("data/park_coordinates.csv")
# remove the dropped parks x5
parks_to_drop <- c("Grasslands National Park of Canada", "Bruce Peninsula National Park of Canada", "Prince of Wales Fort National Historic Site of Canada", "Saoy\\xfa-?ehdacho National Historic Site of Canada", "Sable Island National Park Reserve", "Fort Walsh National Historic Site of Canada")
new_park_coordinates <- subset(park_coordinates, !(park %in% parks_to_drop))
# match coordinates name to park ID
new_park_coordinates$park[new_park_coordinates$park == "Banff National Park of Canada"]<- "BANF"
new_park_coordinates$park[new_park_coordinates$park == "Pacific Rim National Park Reserve of Canada"]<- "PRIM"
new_park_coordinates$park[new_park_coordinates$park == "Waterton Lakes National Park of Canada"]<- "WATE"
new_park_coordinates$park[new_park_coordinates$park == "Kejimkujik National Park and National Historic Site of Canada"]<- "KEJI"
new_park_coordinates$park[new_park_coordinates$park == "Jasper National Park of Canada"]<- "JASP"
new_park_coordinates$park[new_park_coordinates$park == "Forillon National Park of Canada"]<- "FORI"
new_park_coordinates$park[new_park_coordinates$park == "Prince Albert National Park of Canada"]<- "PALB"
new_park_coordinates$park[new_park_coordinates$park == "Kootenay National Park of Canada"]<- "KOOT"
new_park_coordinates$park[new_park_coordinates$park == "Glacier National Park of Canada"]<- "GLAC"
new_park_coordinates$park[new_park_coordinates$park == "Wapusk National Park of Canada"]<- "WAPU"
new_park_coordinates$park[new_park_coordinates$park == "Yoho National Park of Canada"]<- "YOHO"
new_park_coordinates$park[new_park_coordinates$park == "Terra Nova National Park of Canada"]<- "NOVA"
new_park_coordinates$park[new_park_coordinates$park == "Mount Revelstoke National Park of Canada"]<- "REVE"
new_park_coordinates$park[new_park_coordinates$park == "Elk Island National Park of Canada"]<- "ELKI"
new_park_coordinates$park[new_park_coordinates$park == "Georgian Bay Islands National Park of Canada"]<- "GBIS"
new_park_coordinates$park[new_park_coordinates$park == "Point Pelee National Park of Canada"]<- "PELE"
new_park_coordinates$park[new_park_coordinates$park == "Thousand Islands National Park of Canada"]<- "THIS"
new_park_coordinates$park[new_park_coordinates$park == "Wood Buffalo National Park of Canada"]<- "WOOD"
new_park_coordinates$park[new_park_coordinates$park == "Prince Edward Island National Park of Canada"]<- "PEIS"
new_park_coordinates$park[new_park_coordinates$park == "Ivvavik National Park of Canada"]<- "IVVA"
new_park_coordinates$park[new_park_coordinates$park == "Kouchibouguac National Park of Canada"]<- "KOUC"
new_park_coordinates$park[new_park_coordinates$park == "Fundy National Park of Canada"]<- "FUND"
new_park_coordinates$park[new_park_coordinates$park == "Nahanni National Park Reserve of Canada"]<- "NAHA"
new_park_coordinates$park[new_park_coordinates$park == "Aulavik National Park of Canada"]<- "AULA"
new_park_coordinates$park[new_park_coordinates$park == "Fathom Five National Marine Park of Canada"]<- "FIVE"
# convert coordinates into spatial data
new_park_location <- SpatialPoints(new_park_coordinates[, c("longitude", "latitude")])
parks_sf <- st_as_sf(new_park_coordinates, coords = c("longitude", "latitude"), crs = 4326)
#define the crs
esri_102001 <- st_crs("+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
# reproject parks coordinate
parks_esri <- st_transform(parks_sf, crs = esri_102001)
#convert back to dataframe
parks_esri_df <- st_drop_geometry
#convert to coordinates
coordinates <- st_coordinates(parks_esri)
# combine reprojected esri coordinates into original coordinates df
park_coordinates_esri <- cbind(coordinates, new_park_coordinates)
# renaming the columns
names(park_coordinates_esri)[1] <- "esri_long"
names(park_coordinates_esri)[2] <- "esri_lat"
```
```{r downloading Canada province map, eval = FALSE}
# Canada map ----
#level 0 = country; level 1 = province/state; level 2 = counties
provinces <- gadm(country="Canada", level=1, path = tempdir())
#save as an RDS
saveRDS(provinces,file ="data/shapefiles/CAprovinces_map.rds")
provinces <- readRDS("data/shapefiles/CAprovinces_map.rds")
```
```{r echo = TRUE}
#plot both shape files, layered
plot(provinces)
# import ndvi file
ndvi_bg <- "C:/Users/grace/Documents/GitHub/HWI_NDVI_parks/data/ndvi/2021ndvi/2021_jun/VIIRS-Land_v001-preliminary_NPP13C1_S-NPP_20210630_c20220419155820.nc"
ndvi_bg <- terra::rast(ndvi_bg) #bg is 2021 jun 30
plot(ndvi_bg$NDVI)
```
```{r reproject, eval = FALSE}
# reproject NDVI to provinces crs
reprojected_bg <- terra::project(ndvi_bg,
provinces,
method = "near")
#crop reprojected ndvi bg to Can shape
cropped_provinces_ndvi <- crop(reprojected_bg, provinces, mask = TRUE)
provinces_bg <- cropped_provinces_ndvi$NDVI
saveRDS(provinces_bg,file ="figures/old_figures/Canmap.rds")
provinces_bg <- readRDS("figures/old_figures/Canmap.rds")
```
```{r}
#find the extent of the raster
ext(provinces_bg)
#set the bounding box
bbox <- ext(c(-141.006866, -52.6000041603337, 41.6999988824795, 83.0999985311861))
#crop the ndvi
bg_crop <- crop(provinces_bg, bbox)
#write raster
writeRaster(bg_crop, "figures/old_figures/bg_crop.tif", overwrite = TRUE)
```
```{r reproject to ESRI:102001, eval = FALSE}
#REPROJECT BG_CROP
bg_reproject <- terra::project(bg_crop,
"ESRI:102001")
```
```{r preparation for map, echo = TRUE}
plot(bg_crop)
saveRDS(bg_crop,file ="figures/old_figures/bg_crop.rds")
#crop the map
Can_crop <- crop(provinces, bbox)
saveRDS(Can_crop,file ="figures/old_figures/Can_crop.rds")
Can_crop <- readRDS("figures/old_figures/Can_crop.rds")
plot(Can_crop)
# Define manual color scale due to adding national parks ----
manual_colors <- c("WATE"= "#560133", "ELKI" = "#790149", "JASP" = "#9F0162", "WOOD" = "#C7007C",
"BANF" = "#EF0096", "YOHO" = "#FF5AAF", "KOOT" = "#FF9DCB", "REVE" = "#FFCFF2",
"PRIM" = "#450270", "GLAC" = "#65019F", "WAPU" = "#8400CD", "FUND" = "#A700FC",
"KOUC" = "#DA00FD", "NOVA" = "#FF3CFE", "KEJI" = "#FF92FD", "AULA" = "#FFCCFE",
"NAHA" = "#5A000F", "FIVE" = "#7E0018", "PELE" = "#A40122", "GBIS" = "#CD022D",
"THIS" = "#F60239", "PEIS" = "#FF6E3A", "FORI" = "#FFAC3B", "PALB" = "#FFDC3D", "IVVA" = "#FF4C30")