-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyses.Rmd
1446 lines (1242 loc) · 52.9 KB
/
analyses.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: "Predicting reintroduction sites using habitat selection in an endangered mesopredator"
author: "Kathryn Macpherson, Belinda Wilson, Shoshana Rapley"
date: "January 12, 2024"
output:
html_document:
toc: true
number_sections: true
top_depth: 3
toc_float:
collapsed: true
theme: cerulean
highlight: pygments
editor_options:
chunk_output_type: console
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), 'tutorial.html')) })
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, eval=FALSE)
```
# **Background**
Reintroductions are a key strategy for reversing biodiversity loss (Sheean *et al*. 2012) and restoring ecological function (Lipsey & Child 2007). Their success can often be attributed to habitat suitability at the recipient site (Bennett *et al*. 2012), since it drives resource (i.e., food, shelter, protection) and mate availability (Su *et al*. 2021). It is important to confirm the habitat suitability of a recipient site, often through monitoring the movement and habitat selection of founders (individuals released for a reintroduction, Bennett *et al*. 2012).
We reintroduced eastern quolls (*Dasyurus viverrinus*) in a series of trials over three consecutive years (2016–2018) into a conservation-fenced sanctuary as part of the [Mulligans Flat-Goorooyarroo Woodland Experiment](https://www.coexistenceconservationlab.org/mulligans-flat-goorooyarroo-woodland-experiment). Throughout these trials, we radiotracked founders to reveal >200 diurnal dens.
Here we explore the habitat selection of reintroduced eastern quolls to predict suitable habitat for future translocations, using species distribution models (SDMs).
# **Setup**
First, we manually installed the [pacman Package Management Tool](https://cran.r-project.org/web/packages/pacman/index.html), which enables us to install and load subsequent packages in a condensed and efficient way.
We also manually installed `Rtools` (after having [downloaded the most up-to-date version from CRAN](https://cran.r-project.org/bin/windows/Rtools/)), and the `slga` (soils) package from GitHub.
These manual installations only need to be done once, upon first running the project.
```{r, eval=FALSE}
# Manually install pacman package
install.packages(pacman)
# Manually install Rtools package
installr::install.Rtools(check=TRUE, check_r_update=TRUE, GUI=TRUE)
# Manually install slga package
devtools::install_github("obrl-soil/slga")
```
```{r}
# Install and load required packages
pacman::p_load(beepr, data.table, devtools, dplyr, elevatr, forcats, ggmap, ggplot2, ggpubr, ggsn, ggspatial, freqtables, installr, janitor, lme4, lubridate, MuMIn, ncdf4, plotrix, plotROC, raster, rasterVis, readxl, reshape2, rgdal, rgl, rJava, rstudioapi, scales, SDMtune, sf, slga, st, stringr, terra, tidyr, tidyterra, tidyverse, viridis, zeallot)
```
We also set the working directory to where this R markdown is saved using the `rstudioapi` package.
```{r}
# Set working directory to where your Rmarkdown is saved
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
```
# **Data preparation**
Here we read in our eastern quoll den assessment data, and compare each of our *intrinsic* den covariates with what is available throughout MFWS. These are:
1. **Den type** (open, hole, grass, log, pipe, drain, or culvert)
2. **Den cover** (ground, rocky outcrop, log, stump, fallen tree roots, living tree, dead tree, termite mound, pipe, drain, culvert, or building)
3. **Den substrate** (natural, experiment, or artificial)
4. **Number of entrances** (1, 2, or ≥3)
5. **Plot type** (open, or kangaroo exclusion zone)
6. **Habitat** (open grassland, Eucalypt forest, or Eucalypt woodland)
7. **Activity** (active, inactive, or could not locate)
## Import
```{r}
# Assign raw data filename to an object
raw_data <- "data.xlsx"
# Read in data
data <- read_excel("input/raw data.xlsx", sheet="dens") %>%
clean_names() %>%
# Convert numeric dates to date format
mutate(date = as.Date(date, origin="1899-12-30"),
entrances = as.factor(entrances_holes)) %>%
dplyr::select(den_id, sector, "den_type"=type,
"den_cover"=under, "den_substrate"='in',
entrances_holes, plot_type, habitat, activity,
easting, northing, latitude, longitude) %>%
subset(sector!="Outside")
```
Calculate how many dens were first found in each study year.
```{r}
years <- read_excel("input/raw data.xlsx", sheet="visits") %>%
clean_names() %>%
mutate(year=year(as.Date(date_tracked))) %>%
group_by(eqd_code) %>%
summarise(min(year)) %>%
clean_names() %>%
group_by(min_year) %>%
count()
```
As expected, the number of dens first identified decreased throughout the years (119 in 2016, 79 in 2017, and 33 in 2018).
### Den map
Here we generate a map of the den sites coloured in order of first use: oldest dens are purple, middle dens are green, and newest dens are yellow (ranging February 2016–November 2018).
You will need to [generate your own API key](https://developers.google.com/maps/documentation/javascript/get-api-key) to fetch the basemap from Google.
```{r,echo=FALSE, eval=FALSE}
# Read in MFWS fence shapefile for plotting
mfws <- readOGR(dsn="input/mfws_fence.shp", verbose=FALSE) %>%
spTransform(CRS("+proj=utm +zone=55 +datum=WGS84")) %>%
fortify(verbose=FALSE) %>% #transforms from from sp to df
mutate(lat=as.numeric(lat + 10000000),
long=as.numeric(long))
# Use Google API to fetch a base map
#ggmap::register_google(key="[enter API key here]")
sat <- get_map(location=c(lat=-35.16580, lon=149.16454),
zoom=14, source="google", maptype="satellite", crop=TRUE)
# Display the map
ggmap(sat)
# Read in MFWS fence shapefile for plotting
mf_outline <- readOGR(dsn="shapefiles/mfws_fence.shp", verbose=FALSE) %>%
spTransform("EPSG:4326") %>%
fortify(verbose=FALSE) #transforms from from sp to df
# Read in MFWS fence shapefile for plotting
mf_tracks <- readOGR(dsn="shapefiles/MFWS_tracks.shp", verbose=FALSE) %>%
spTransform("EPSG:4326") %>%
fortify(verbose=FALSE) #transforms from from sp to df
# Create a map
den_map_sat <- ggmap(sat) +
geom_path(mf_outline, mapping=aes(x=long, y=lat, group=group),
col="white", size=1) +
geom_path(mf_tracks, mapping=aes(x=long, y=lat, group=group),
col="white", size=0.5) +
geom_point(data, mapping=aes(x=longitude, y=latitude,
col=den_id), size=2, alpha=0.8) +
ggsn::scalebar(y.min=-35.153, x.min=149.145,
y.max=-35.182, x.max=149.190,
dist=500, height=3, dist_unit="m",
box.color="white", st.color="white",
st.size=3, anchor=c(y=-35.175, x=149.182),
st.dist=0.04, transform=TRUE) +
theme(panel.grid.major=element_blank(),
panel.background=element_rect(fill="white"),
plot.margin=unit(c(0, 0, 0, 0), "cm"),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
legend.position = "none",
legend.key=element_blank()) +
scale_colour_viridis(discrete=T, option="D", begin=0, end=1) +
xlab("") + ylab("")
# Display the plot
print(den_map_sat)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(den_map_sat, filename="output/den satellite map.jpeg",
width=200, height=125, units="mm")
```
# **Intrinsic den covariates**
## Den type
a) Plots
```{r}
# Transfer from wide to long format, and calculate percent for each level
long <- data %>%
group_by(den_type) %>%
summarise(percent = 100/nrow(data)*length(den_type),
n = length(den_type)) %>%
na.omit() %>% mutate(den_type = factor(den_type,
levels=c("Natural",
"Experiment",
"Artificial")))
# Create histogram plot
den_type_hist <- ggplot(long, aes(x=den_type, y=percent, fill=den_type)) +
geom_col() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Den type") + ylab("Percentage (%)") +
# Viridis scale options are A–E
scale_fill_viridis(discrete=T, option="D", begin=0.9, end=0.2) +
scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Histogram for publication
den_type_hist_pub <- ggplot(long, aes(x=den_type, y=percent, fill=den_type)) +
geom_col() +
annotate("text", x=3.5, y=90, label = parse(text = "bold('a')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Den type") + ylab("Percentage (%)") +
# Viridis scale options are A–E
scale_fill_viridis(discrete=T, option="D", begin=0.1, end=0.9) +
scale_y_continuous(limits=c(0, 90), breaks=seq(0, 90, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Display the plot
print(den_type_hist_pub)
# Subset to those records with valid den substrates
plot_data <- subset(data, den_type!="NA")
View(data)
# Create map plot
den_type_map <- ggplot() +
geom_path(mfws, mapping=aes(x=long, y=lat, group=group),
col="grey15") +
geom_point(plot_data, mapping=aes(x=easting, y=northing,
col=den_type), alpha=0.8) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
legend.position = c(0.70, 0.25),
legend.title = element_text(size=9),
legend.text = element_text(size=9),
legend.key.size = unit(0.5, 'cm'),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(-0.5, -1, -1, -1), "cm"),
legend.key = element_blank()) +
scale_colour_manual(values=viridis(length(unique(data$den_type)),
begin=0.9, end=0.2), name="Den type",
breaks=c('Natural', 'Experiment', 'Artificial')) +
xlab("") + ylab("")
# Combine histogram and map
den_type_plots <- ggarrange(den_type_hist, den_type_map,
ncol=2, nrow=1, widths=c(3,3))
# Display the plot
print(den_type_plots)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(den_type_plots,
filename="output/den type histogram and map.jpeg",
width=200, height=75, units="mm")
# Export the plot to a jpeg file
ggsave(den_type_hist_pub,
filename="output/den type histogram publication.jpeg",
width=150, height=75, units="mm")
```
b) Models
```{r}
# Chi-square test
den_type_mod <- data %>%
subset(den_type!="N/A") %>%
freq_table(den_type) %>%
freq_test() %>%
select(c(1:3, 5, 7, 12:14))
```
## Den cover
a) Plots
```{r}
# Transfer from wide to long format, and calculate percent for each level
long <- data %>%
group_by(den_cover) %>%
summarise(percent = 100/nrow(data)*length(den_cover),
n = length(den_cover)) %>%
na.omit() %>%
mutate(den_cover=factor(den_cover,
levels=c("Ground", "Log",
"Fallen tree roots", "Living tree",
"Pipe, drain, or culvert", "Stump",
"Dead tree", "Rocky outcrop", "Building",
"Termite mound")))
# Create histogram plot
den_cover_hist <- ggplot(long, aes(x=den_cover, y=percent, fill=den_cover)) +
geom_col() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.text.x = element_text(angle=-45, vjust=0.5, hjust=0.2),
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey"),
plot.margin = unit(c(0.5, 0.75, 0, 0.2), "cm")) +
xlab("Den cover") + ylab("Percentage (%)") +
scale_fill_viridis(discrete=T, option="D", begin=1, end=0) +
scale_y_continuous(limits=c(0, 50), breaks=seq(0, 100, 20)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Histogram for publication
den_cover_hist_pub <- ggplot(long, aes(x=den_cover, y=percent, fill=den_cover)) +
geom_col() +
annotate("text", x=10.5, y=50, label = parse(text = "bold('c')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "none",
axis.text.x = element_text(angle=-45, vjust=0.5, hjust=0.2),
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Den cover") + ylab("Percentage (%)") +
scale_fill_viridis(discrete=T, option="D", begin=0, end=1) +
scale_y_continuous(limits=c(0, 50), breaks=seq(0, 50, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Display the plot
print(den_cover_hist_pub)
# Subset to those records with valid den covers
plot_data <- subset(data, den_cover!="NA")
# Create map plot
den_cover_map <- ggplot() +
geom_path(mfws, mapping=aes(x=long, y=lat, group=group), col="grey15") +
geom_point(plot_data, mapping=aes(x=easting, y=northing,
col=den_cover), alpha=0.8) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
legend.position = c(0.85, 0.25),
legend.title = element_text(size=9),
legend.text = element_text(size=9),
legend.key.size = unit(0.5, 'cm'),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(-0.5, -1, -1, -1), "cm"),
legend.key = element_blank()) +
scale_colour_manual(values=viridis(length(unique(data$den_cover)),
begin=1, end=0), name="Den cover") +
guides(colour=guide_legend(ncol=2)) +
xlab("") + ylab("")
# Combine histogram and map into single plot
den_cover_plots <- ggarrange(den_cover_hist, den_cover_map,
ncol=2, nrow=1, widths=c(3,3))
# Display the plot
print(den_cover_plots)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(den_cover_plots,
filename="output/den cover histogram and map.jpeg",
width=275, height=125, units="mm")
# Export the plot to a jpeg file
ggsave(den_cover_hist_pub,
filename="output/den cover histogram publication.jpeg",
width=150, height=75, units="mm")
```
b) Models
```{r}
# Chi-square test
den_cover_mod <- data %>%
subset(den_cover!="N/A") %>%
freq_table(den_cover) %>%
freq_test() %>%
select(c(1:3, 5, 7, 12:14))
```
## Den substrate
a) Plots
```{r}
# Transfer from wide to long format, and calculate percent for each level
long <- data %>%
group_by(den_substrate) %>%
summarise(percent = 100/nrow(data)*length(den_substrate),
n = length(den_substrate)) %>%
na.omit() %>%
mutate(den_substrate=factor(den_substrate,
levels=c("Rabbit warren", "Log",
"Grass", "Pipe, drain, or culvert",
"Open")))
# Use ifelse to define 'hole' as 'rabbit warren' instead
data$den_substrate <- ifelse(data$den_substrate == "Hole", "Rabbit warren", as.character(data$den_substrate))
# Create histogram plot
den_substrate_hist <- ggplot(long, aes(x=den_substrate, y=percent,
fill=den_substrate)) +
geom_col() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Den substrate") + ylab("Percentage (%)") +
# Options include magma, inferno, plasma, viridis, turbo
scale_fill_viridis(discrete=T, option="D", begin=1, end=0) +
scale_y_continuous(limits=c(0, 90), breaks=seq(0, 100, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Histogram for publication
den_substrate_hist_pub <- ggplot(long, aes(x=den_substrate, y=percent,
fill=den_substrate)) +
geom_col() +
annotate("text", x=5.5, y=90, label = parse(text = "bold('b')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Den substrate") + ylab("Percentage (%)") +
# Options include magma, inferno, plasma, viridis, turbo
scale_fill_viridis(discrete=T, option="D", begin=0, end=1) +
scale_y_continuous(limits=c(0, 90), breaks=seq(0, 90, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Display the plot
print(den_substrate_hist_pub)
# Subset to those records with valid den substrates
plot_data <- subset(data, den_substrate!="NA")
# Create map plot
den_substrate_map <- ggplot() +
geom_path(mfws, mapping=aes(x=long, y=lat, group=group),
col="grey15") +
geom_point(plot_data, mapping=aes(x=easting, y=northing,
col=den_substrate), alpha=0.8) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
legend.position = c(0.70, 0.25),
legend.title = element_text(size=9),
legend.text = element_text(size=9),
legend.key.size = unit(0.5, 'cm'),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(-0.5, -1, -1, -1), "cm"),
legend.key = element_blank()) +
scale_colour_manual(values=viridis(length(unique(data$den_substrate)),
begin=1, end=0), name="Den substrate") +
xlab("") + ylab("")
# Combine histogram and map into single plot
den_substrate_plots <- ggarrange(den_substrate_hist,
den_substrate_map,
ncol=2, nrow=1, widths=c(3,2))
# Display the plot
print(den_substrate_plots)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(den_substrate_plots,
filename="output/den substrate histogram and map.jpeg",
width=250, height=125, units="mm")
# Export the plot to a jpeg file
ggsave(den_substrate_hist_pub,
filename="output/den substrate histogram publication.jpeg",
width=150, height=75, units="mm")
```
b) Models
```{r}
# Chi-square test
den_substrate_mod <- data %>%
subset(den_substrate!="N/A") %>%
freq_table(den_substrate) %>%
freq_test() %>%
select(c(1:3, 5, 7, 12:14))
```
## Number of entrances
```{r}
# Use ifelse to define entrance hole values
data_2 <- data %>%
mutate(entrances = ifelse(entrances_holes==1, 1,
ifelse(entrances_holes==2, 2,
ifelse(entrances_holes=="≥3", "≥3", NA)))) %>%
drop_na(entrances) %>%
mutate(entrances = factor(entrances, levels=c("1", "2", "≥3")))
```
a) Plots
```{r}
# Transfer from wide to long format, and calculate percent for each level
long <- data_2 %>%
group_by(entrances) %>%
summarise(percent = 100/nrow(data)*length(entrances),
n = length(entrances)) %>%
na.omit()
# Create histogram plot
entrances_hist <- ggplot(long, aes(x=entrances, y=percent,
fill=entrances)) +
geom_col() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Number of entrances") + ylab("Percentage (%)") +
scale_fill_viridis(discrete=T, option="D", begin=0.9, end=0.2) +
scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Histogram for publication
entrances_hist_pub <- ggplot(long, aes(x=entrances, y=percent,
fill=entrances)) +
geom_col() +
annotate("text", x=3.5, y=80, label = parse(text = "bold('d')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Number of entrances") + ylab("Percentage (%)") +
scale_fill_viridis(discrete=T, option="D", begin=0.1, end=0.9) +
scale_y_continuous(limits=c(0, 80), breaks=seq(0, 80, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Display the plot
print(entrances_hist_pub)
# Create map plot
entrances_map <- ggplot() +
geom_path(mfws, mapping=aes(x=long, y=lat, group=group),
col="grey15") +
geom_point(data_2, mapping=aes(x=easting, y=northing,
col=entrances)) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
legend.position = c(0.75, 0.25),
legend.title = element_text(size=9),
legend.text = element_text(size=9),
legend.key.size = unit(0.5, 'cm'),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(-0.5, -1, -1, -1), "cm"),
legend.key = element_blank()) +
scale_colour_manual(values=viridis(length(unique(data_2$entrances)),
begin=0.9, end=0.2),
name="Number of entrances") +
xlab("") + ylab("")
# Combine histogram and map into single plot
entrances_plots <- ggarrange(entrances_hist,
entrances_map,
ncol=2, nrow=1, widths=c(3,2))
# Display the plot
print(entrances_plots)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(entrances_plots,
filename="output/entrances histogram and map.jpeg",
width=250, height=125, units="mm")
# Export the plot to a jpeg file
ggsave(entrances_hist_pub,
filename="output/entrances histogram publication.jpeg",
width=150, height=75, units="mm")
```
b) Models
```{r}
# Chi-square test
entrances_mod <- data %>%
subset(entrances_holes!="N/A") %>%
freq_table(entrances_holes) %>%
freq_test() %>%
select(c(1:3, 5, 7, 12:14))
```
## Plot type
### Plots
```{r}
# Transfer from wide to long format, and calculate percent for each level
long <- data %>%
group_by(plot_type) %>%
summarise(percent = 100/nrow(data)*length(plot_type),
n = length(plot_type)) %>%
na.omit() %>% mutate(plot_type = factor(plot_type,
levels=c("Open",
"Kangaroo exclosure",
"Bettong exclosure")))
# Create histogram plot
plot_type_hist <- ggplot(long, aes(x=plot_type, y=percent,
fill=plot_type)) +
geom_col() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Plot type") + ylab("Percentage (%)") +
# Options include magma, inferno, plasma, viridis, turbo
scale_fill_viridis(discrete=T, option="D", begin=0.9, end=0.1) +
scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Histogram for publication
plot_type_hist_pub <- ggplot(long, aes(x=grazing_pressure, y=percent,
fill=grazing_pressure)) +
geom_col() +
annotate("text", x=3.5, y=90, label = parse(text = "bold('b')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("Grazing pressure") + ylab("Percentage (%)") +
# Options include magma, inferno, plasma, viridis, turbo
scale_fill_viridis(discrete=T, option="D", begin=0.1, end=0.9) +
scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, 10)) +
geom_text(aes(label=paste("n = ", n), vjust=-0.5))
# Display the plot
print(plot_type_hist_pub)
# Subset to those records with valid plot types
plot_data <- subset(data, plot_type!="NA")
# Create map plot
plot_type_map <- ggplot() +
geom_path(mfws, mapping=aes(x=long, y=lat, group=group),
col="grey15") +
geom_point(plot_data, mapping=aes(x=easting, y=northing,
col=plot_type)) +
coord_sf(xlim=c(695750, 699250), ylim=c(6104250, 6107750)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill="white"),
legend.position = c(0.75, 0.25),
legend.title = element_text(size=9),
legend.text = element_text(size=9),
legend.key.size = unit(0.5, 'cm'),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(-0.5, -1, -1, -1), "cm"),
legend.key = element_blank()) +
scale_colour_manual(values=viridis(length(unique(data$plot_type)),
begin=0.9, end=0.1), name="Plot type",
breaks=c('Open', 'Kangaroo exclosure',
'Bettong exclosure')) + xlab("") + ylab("")
# Combine histogram and map into single plot
plot_type_plots <- ggarrange(plot_type_hist,
plot_type_map,
ncol=2, nrow=1, widths=c(3,2))
# Display the plot
print(plot_type_plots)
```
```{r, echo=FALSE, eval=FALSE}
# Export the plot to a jpeg file
ggsave(plot_type_plots,
filename="output/plot type histogram and map.jpeg",
width=250, height=125, units="mm")
# Export the plot to a jpeg file
ggsave(plot_type_hist_pub,
filename="output/plot type histogram publication.jpeg",
width=150, height=75, units="mm")
```
### Models
```{r}
# Chi-square test
plot_type_mod <- data %>%
subset(plot_type!="N/A") %>%
freq_table(plot_type) %>%
freq_test() %>%
select(c(1:3, 5, 7, 12:14))
```
### Adjust for availability within MFWS
```{r}
# Read in MFWS fence shapefile
mfws <- st_read("input/mfws_fence.shp")
# Reproject and prepare shapefile
mfws <- mfws %>%
st_set_crs(st_crs(mfws)) %>%
st_transform("EPSG:32755") %>%
clean_names() %>%
mutate(plot="open") %>%
# Since the roo layer only includes three column, we select them here
select(id, geometry, plot)
# Read in MFWS kangaroo exclusion zone shapefile using sf
kan <- st_read("input/MFGO_kangaroo_exclosures_polygons.shp")
# Reproject and prepare shapefile
kan <- kan %>%
st_set_crs(st_crs(kan)) %>%
st_transform("EPSG:32755") %>%
clean_names() %>%
mutate(plot="kangaroo",
id=row_number()) %>%
# Remove exclosures in the adjacent reserve
filter(id <6) %>%
# Mutate id so it has prefix
mutate(id=paste0("K", row_number()))
# Read in MFWS kangaroo exclusion zone shapefile using sf
bet <- st_read("input/MFWS_bettong_exclosures_polygons.shp")
# Reproject and prepare shapefile
bet <- bet %>%
st_set_crs(st_crs(bet)) %>%
st_transform("EPSG:32755") %>%
clean_names() %>%
# Since the roo layer only includes three column, we select them here
select(id, geometry) %>%
# Mutate id so it has prefix
mutate(id=paste0("B", row_number())) %>%
# Assign bettong plots inside kangaroo plots as another category
mutate(id = ifelse(id == "B1", "KB1",
ifelse(id == "B6", "KB6",
ifelse(id == "B8", "KB8",
ifelse(id == "B9", "KB9",
ifelse(id == "B10", "KB10",
ifelse(id == "B12", "KB12", id))))))) %>%
# Assign zone type to these plots
mutate(plot = ifelse(str_starts(id, "^B"), "bettong", "both"))
# Crop MFWS polygon by the shape of kan and bet layers
mfws_clip <- st_difference(mfws, st_union(kan, bet)) %>%
select(id=id.1, plot=plot.1, geometry)
# Plot the plot types
ggplot() +
geom_sf(data=mfws_clip, aes(fill=plot)) +
geom_sf_text(data=mfws_clip, aes(label=plot)) +
geom_sf(data=kan, aes(fill=id)) +
geom_sf_text(data=kan, aes(label=id)) +
geom_sf(data=bet, aes(fill=id)) +
geom_sf_text(data=bet, aes(label=id)) +
#coord_sf(expand=FALSE, xlim=c(extent(mfws)[1]+100,
# extent(mfws)[2]+100),
# ylim=c(extent(mfws)[3]+100,
# extent(mfws)[4]+100)) +
theme_minimal() +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank())
# Combine kangaroo and bettong layers and calculate area
plots <- rbind(kan, bet) %>%
# Calculate area of each geometry (or polygon)
mutate(area_km2=as.numeric(st_area(geometry))/1e6)
plots_sum <- plots %>%
as.data.frame() %>%
group_by(plot) %>%
summarise(area_km2=sum(area_km2)) %>%
select(plot, area_km2)
# Calculate the open plot area
plots_sum <- plots_sum %>%
add_row(plot="open", area_km2=mfws_area_minus_aquatic-
sum(plots$area_km2))
# Read in den coordinates data and prepare for plotting
den <- read_excel("input/raw data.xlsx", sheet="dens") %>%
clean_names() %>%
mutate(easting=as.numeric(easting),
northing=as.numeric(northing))
# Convert to spatial points dataframe
den_sp <- SpatialPointsDataFrame(
data.frame(den$easting, den$northing), den,
proj4string=CRS("EPSG:32755")) %>%
st_as_sf()
# Join den coordinates and plots layer
den_zone <- st_join(den_sp, plots, join=st_intersects) %>%
st_drop_geometry() %>%
as.data.frame() %>%
mutate(plot = ifelse(is.na(plot), "open", plot)) %>%
# Remove dens outside MFWS
filter(sector != "Outside",
!is.na(sector))
# Extract exclosure zone for each den coordinate and calculate frequencies
den_zone_adj <- den_zone %>%
group_by(plot) %>%
count() %>%
# Since only one den occurs in both, but is included in the kangaroo plot
# count, we exclude it
filter(plot != "both") %>%
# Adjusted by frequency of dens
mutate(den_freq=n) %>%
select(-n) %>%
mutate(den_perc=(den_freq/sum(den_zone_adj$den_freq))*100) %>%
# Remove dens outside MFWS
filter(!is.na(plot))
# Assign MFWS minus aquatic area value
mfws_area_minus_aquatic <- sum(mfws_group$area_km2)-mfws_group$area_km2[1]
# Assign correct number of dens (223)
dens_n <- length(unique(den_zone$den))
# Adjust frequencies and percentages to account for exclosure zone availability
den_zone_plot <- den_zone_adj %>%
# Join with df to get area_km2 column
left_join(plots, by="plot") %>%
group_by(plot) %>%
summarize(plot = max(plot), den_freq = max(den_freq),
den_perc = max(den_perc), area_km2 = sum(area_km2)) %>%
mutate(area_km2 = ifelse(plot == "open",
# Calculate open area by subtracting other plot types
mfws_area_minus_aquatic-
(sum(area_km2, na.rm=TRUE)), area_km2)) %>%
# Calculate frequency of dens adjusted by available area
mutate(den_freq_adj=(den_freq/area_km2)*
mfws_area_minus_aquatic/
nrow(den_zone_adj)) %>%
# Redistributing so the number of dens is 223
mutate(den_freq_adj=den_freq_adj/sum(den_freq_adj)*dens_n) %>%
# Calculate percentage of dens adjusted by available area
mutate(den_perc_adj=(den_freq_adj/sum(den_zone_plot$den_freq_adj))*100,
plot=factor(plot, levels = c("open", "kangaroo", "bettong"),
labels = c("Open", "Kangaroo", "Bettong")))
# Display chi-square test for unequal distribution of dens
# after having been adjusted for available area
den_zone %>%
# Remove any dens that were in zone NA (i.e., outside MFWS)
filter(!is.na(plot),
plot != "both") %>%
freq_table(plot) %>%
mutate(n=den_zone_plot$den_freq_adj,
percent=den_zone_plot$den_perc_adj) %>%
freq_test() %>%
# Select the statistics we're interested in
select(plot=cat, n, percent, t_crit,
chi2_pearson, df, p=p_chi2_pearson)
```
Bar plot
```{r}
# Use ifelse to define plot type as grazing pressure instead
den_zone_plot <- den_zone_plot %>%
mutate(grazing_pressure = ifelse(plot == "Open", "High",
ifelse(plot == "Kangaroo", "Medium",
ifelse(plot == "Bettong", "Low", NA))))
# Ensure columns are ordered correctly for plotting
den_zone_plot$grazing_pressure <- factor(den_zone_plot$grazing_pressure,
levels = c("High", "Medium", "Low"))
```
```{r}
# Plot raw den percentages per habitat type
zone_raw_barplot <- ggplot(data=den_zone_plot,
aes(x= grazing_pressure, y=den_perc, fill=grazing_pressure)) +
geom_col() +
annotate("text", x=3.5, y=90, label = parse(text = "bold('c')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("") + ylab("Percentage (%)") +
scale_fill_viridis(discrete=T, option="D", begin=0.1, end=0.9) +
scale_y_continuous(limits=c(0, 90), breaks=seq(0, 90, 10))
# Display the plot
print(zone_raw_barplot)
# Plot adjusted den percentages per habitat type
zone_adj_barplot <- ggplot(data=den_zone_plot,
aes(x= grazing_pressure, y=den_perc_adj, fill=grazing_pressure)) +
geom_col() +
annotate("text", x=3.5, y=60, label = parse(text = "bold('d')"), size=5) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), "cm"),
legend.position = "none",
axis.line.x = element_line(colour="grey"),
axis.line.y = element_line(colour="grey")) +
xlab("") + ylab("Percentage adjusted for availability (%)") +
scale_fill_viridis(discrete=T, option="D", begin=0.1, end=0.9) +
scale_y_continuous(limits=c(0, 60), breaks=seq(0, 60, 10))
# Display the plot
print(zone_adj_barplot)
# Combine the plots
zone_barplots <- ggarrange(zone_raw_barplot, zone_adj_barplot, ncol= 2, nrow= 1,
widths= c(1,1), heights = c(1,1)) %>%
annotate_figure(bottom=text_grob("Grazing zone", hjust=0.5, vjust=0))
# Display the combined plot
print(zone_barplots)
```
```{r, echo=FALSE, eval=FALSE}
ggsave(zone_barplots,
filename="output/grazing pressure barplots.jpeg",
width=225, height=100, units="mm")
```
## Habitat