-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_cepedianus_simulation_&_figures.R
2334 lines (1928 loc) · 118 KB
/
N_cepedianus_simulation_&_figures.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
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
# De Wysiecki et al. - Global ENM projection for the broadnose sevengill shark (Notorynchus cepedianus)
# Additional analyses and figures (ordered as they appear in manuscript)
# Each figure is self-sufficient, so expect high repetition of lines in script
library(rgdal)
library(rgeos)
library(raster)
library(spThin)
library(ggplot2)
library(kuenm) # find it on GitHub (marlonecobos/kuenm)
library(ellipsenm) # find it on GitHub (marlonecobos/ellipsenm)
library(rgl)
library(ggspatial)
library(ggsn)
library(viridis)
library(terra)
library(SDMtune) # find it on GitHub (ConsBiol-unibern/SDMtune)
library(bam) # find it on GitHub (luismurao/bam)
library(matrixStats)
library(animation)
library(furrr)
library(magrittr)
setwd('SET YOUR WORKING DIRECTORY')
# Global and regional coastlines from the Global Self-consistent, Hierarchical, High-resolution Geography Database
# Dowloaded from https://www.soest.hawaii.edu/pwessel/gshhg/
coast <- readOGR(dsn = 'DOWNLOAD, READ AND SET PATH', layer = 'GSHHS_f_L1_World')
coast <- gBuffer(coast, byid = T, width = 0)
# Projection
crs <- CRS('+init=epsg:4326')
# Species
Species <- 'Notorynchus cepedianus'
# Function for 10% Minimum Training Presence calculation
# Taken from https://babichmorrowc.github.io/post/2019-04-12-sdm-threshold/
sdm_threshold.5 <- function(sdm, occs, type = 'mtp', binary = FALSE){
occPredVals <- raster::extract(sdm, occs)
if(type == 'mtp'){
thresh <- min(na.omit(occPredVals))
} else if(type == 'p05'){
if(length(occPredVals) < 10){
p05 <- floor(length(occPredVals) * 0.95)
} else {
p05 <- ceiling(length(occPredVals) * 0.95)
}
thresh <- rev(sort(occPredVals))[p05]
}
sdm_thresh <- sdm
sdm_thresh[sdm_thresh < thresh] <- NA
if(binary){
sdm_thresh[sdm_thresh >= thresh] <- 1
}
return(sdm_thresh)
}#MTP5
sdm_threshold.10 <- function(sdm, occs, type = 'mtp', binary = FALSE){
occPredVals <- raster::extract(sdm, occs)
if(type == 'mtp'){
thresh <- min(na.omit(occPredVals))
} else if(type == 'p10'){
if(length(occPredVals) < 10){
p10 <- floor(length(occPredVals) * 0.90)
} else {
p10 <- ceiling(length(occPredVals) * 0.90)
}
thresh <- rev(sort(occPredVals))[p10]
}
sdm_thresh <- sdm
sdm_thresh[sdm_thresh < thresh] <- NA
if(binary){
sdm_thresh[sdm_thresh >= thresh] <- 1
}
return(sdm_thresh)
}#MTP10
# Functions for response plots
.get_presence <- function(swd) {
return(swd@data[swd@pa == 1, , drop = F])
}
.get_absence <- function(swd) {
return(swd@data[swd@pa == 0, , drop = F])
}
#----------------------------------- G space --------------------------------------------
# Read predictors and name them
env <- stack('predictors.tif')
var_names <- c('Temperature', 'Surface_temperature', 'Primary_productivity', 'Kd490', 'Salinity',
'Bathymetry', 'Distance_to_coast', 'Slope')
names(env) <- var_names
# Discard bathymetry and slope because discrepancies at shelf margin (see analysis for Figure S3 and S4)
var_set <- c('Temperature', 'Surface_temperature', 'Primary_productivity', 'Kd490', 'Salinity',
'Distance_to_coast')
# G space
env.G <- env[[var_set]]
#----------------------------------- Figure S1 ---------------------------------------------
# Systematic spatial thinning in SWA
dat <- read.csv('Appendix C.csv') # mind not all data is available
dat <- subset(dat, Region == 'Southwest Atlantic')
swa_train <- subset(dat, Type == 'calibration')
# Visually find the minimum thinning distance that removes spatial clusters in data
thinned_data <- data.frame()
for(i in seq(10, 50, 10)){
swa_train_thin <- thin(swa_train, lat.col = 'Latitude', long.col = 'Longitude', spec.col = 'Species',
thin.par = i, reps = 1000, locs.thinned.list.return = T, write.files = F, write.log.file = F)
thinned_data0 <- data.frame(Lon = swa_train_thin[[1]]$Longitude, Lat = swa_train_thin[[1]]$Latitude,
Dist = as.factor(paste(i, 'km')))
thinned_data <- rbind(thinned_data, thinned_data0)
}
# Calibration area (M space)
coord <- cbind(dat$Longitude, dat$Latitude)
occ.tot <- SpatialPoints(coord, proj4string = crs) # transform to spatial points
occ.buff <- buffer(occ.tot, width = 1000000) # sampling bias: 1000 km buffer around each point
env.M <- crop(env.G, occ.buff) # crop raster stack to buffer polygon
env.M <- mask(env.M, occ.buff) # mask raster stack to buffer polygon
# Erase Pacific areas
lon1 <- c(-84, -69.4, -69.4, -84, -84)
lat1 <- c(-32, -32, -62, -62, -32)
pol1 <- cbind(lon1, lat1)
Pol1 <- Polygon(pol1, hole = F)
list1 <- list(Pol1)
poly1 <- Polygons(list1, ID = 'p1')
spp1 <- SpatialPolygons(list(poly1), proj4string = crs)
env.M <- mask(env.M, spp1, inverse = T)
Ext1 <- extent(-69.4, -39, -61.4, -22.8)
env.M <- crop(env.M, Ext1)
env.M <- stack(env.M)
empty_raster <- env.M[[1]]
empty_raster[!is.na(empty_raster)] = 1
raster.df <- data.frame(coordinates(empty_raster), values = values(empty_raster))
ggplot(data = thinned_data, aes(x = Lon, y = Lat)) +
geom_tile(data = raster.df, aes(x = x, y = y, fill = as.factor(values)), inherit.aes = F) +
geom_point(shape = 21, size = 0.5, color = 'blue') +
scale_fill_manual(values = 'white', na.value = 'grey95') + coord_equal(expand = F) +
theme(legend.position = 'none') + labs(x = 'Longitude', y = 'Latitude') +
facet_wrap(~ Dist, ncol = 5)
ggsave('Figure S1.pdf', width = 30, height = 9, units = 'cm')
#----------------------------------- Figure S2 ---------------------------------------------
# Systematic spatial thinning in AUS
dat <- read.csv('Appendix C.csv')
dat <- subset(dat, Region == 'Australia')
aus_train <- subset(dat, Type == 'calibration')
# Visually find the minimum thinning distance that removes spatial clusters in data
thinned_data <- data.frame()
for(i in seq(10, 50, 10)){
aus_train_thin <- thin(aus_train, lat.col = 'Latitude', long.col = 'Longitude', spec.col = 'Species',
thin.par = i, reps = 1000, locs.thinned.list.return = T, write.files = F, write.log.file = F)
thinned_data0 <- data.frame(Lon = aus_train_thin[[1]]$Longitude, Lat = aus_train_thin[[1]]$Latitude,
Dist = as.factor(paste(i, 'km')))
thinned_data <- rbind(thinned_data, thinned_data0)
}
# Calibration area (M space)
coord <- cbind(dat$Longitude, dat$Latitude)
occ.tot <- SpatialPoints(coord, proj4string = crs) # transform to spatial points
occ.buff <- buffer(occ.tot, width = 1000000) # sampling bias: 1000 km buffer around each point
env.M <- crop(env.G, occ.buff) # crop raster stack to buffer polygon
env.M <- mask(env.M, occ.buff) # mask raster stack to buffer polygon
empty_raster <- env.M[[1]]
empty_raster[!is.na(empty_raster)] = 1
raster.df <- data.frame(coordinates(empty_raster), values = values(empty_raster))
ggplot(data = thinned_data, aes(x = Lon, y = Lat)) +
geom_tile(data = raster.df, aes(x = x, y = y, fill = as.factor(values)), inherit.aes = F) +
geom_point(shape = 21, size = 0.5, color = 'blue') +
scale_fill_manual(values = 'white', na.value = 'grey95') + coord_equal(expand = F) +
theme(legend.position = 'none') + labs(x = 'Longitude', y = 'Latitude') +
facet_wrap(~ Dist, ncol = 3)
ggsave('Figure S2.pdf', width = 30, height = 14, units = 'cm')
#----------------------------------- Figure S3 and S4 ---------------------------------------------
# Depth and slope discrepancies between SWA and AUS
Fit <- 'REGIONAL_INDEPENDENT'
Region1 <- 'SWA'
Region2 <- 'AUS'
# Using three variables to simplify analysis
Vars1 <- c('Bathymetry', 'Surface_temperature', 'Kd490')
Vars2 <- c('Kd490', 'Surface_temperature', 'Slope')
Vars3 <- c('Distance_to_coast', 'Surface_temperature', 'Kd490')
# Occurrences
dat1 <- read.csv(paste(Fit, '/', Region1, '/', Fit, '_', Region1, '_calibration_points.csv', sep = ''))
dat1 <- dat1[, c('Species', 'Longitude', 'Latitude')]
colnames(dat1) <- c('species', 'longitude', 'latitude')
dat2 <- read.csv(paste(Fit, '/', Region2, '/', Fit, '_', Region2, '_calibration_points.csv', sep = ''))
dat2 <- dat2[, c('Species', 'Longitude', 'Latitude')]
colnames(dat2) <- c('species', 'longitude', 'latitude')
# M space SWA
coord1 <- cbind(dat1$longitude, dat1$latitude)
occ.tot1 <- SpatialPoints(coord1, proj4string = crs)
occ.buff1 <- buffer(occ.tot1, width = 1000000)
env.M1 <- crop(env.G, occ.buff1)
env.M1 <- mask(env.M1, occ.buff1)
# Erase Pacific areas
lon1 <- c(-84, -69.4, -69.4, -84, -84)
lat1 <- c(-32, -32, -62, -62, -32)
pol1 <- cbind(lon1, lat1)
Pol1 <- Polygon(pol1, hole = F)
list1 <- list(Pol1)
poly1 <- Polygons(list1, ID = 'p1')
spp1 <- SpatialPolygons(list(poly1), proj4string = crs)
env.M1 <- mask(env.M1, spp1, inverse = T)
Ext1 <- extent(-69.4, -39, -61.4, -22.8)
env.M1 <- crop(env.M1, Ext1)
env.M1 <- stack(env.M1)
# M space AUS
coord2 <- cbind(dat2$longitude, dat2$latitude)
occ.tot2 <- SpatialPoints(coord2, proj4string = crs)
occ.buff2 <- buffer(occ.tot2, width = 1000000)
env.M2 <- crop(env, occ.buff2)
env.M2 <- mask(env.M2, occ.buff2)
env.M2 <- stack(env.M2)
# Preparing overlap objects to perform analyses
niche1_depth <- overlap_object(data = dat1, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M1[[Vars1]])
niche2_depth <- overlap_object(data = dat2, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M2[[Vars1]])
niche1_slope <- overlap_object(data = dat1, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M1[[Vars2]])
niche2_slope <- overlap_object(data = dat2, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M2[[Vars2]])
niche1_dist <- overlap_object(data = dat1, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M1[[Vars3]])
niche2_dist <- overlap_object(data = dat2, species = 'species', longitude = 'longitude', latitude = 'latitude',
method = 'mve1', level = 95, variables = env.M2[[Vars3]])
# Overlap
N.test_depth <- ellipsoid_overlap(niche1_depth, niche2_depth, overlap_type = 'back_union',
significance_test = T, replicates = 1000)
N.test_slope <- ellipsoid_overlap(niche1_slope, niche2_slope, overlap_type = 'back_union',
significance_test = T, replicates = 1000)
N.test_dist <- ellipsoid_overlap(niche1_dist, niche2_dist, overlap_type = 'back_union',
significance_test = T, replicates = 1000)
# Plots: ellipsoid overlap and statistical significance test
# Depth
rgl.open(); rgl.bg(color = 'white')
plot_overlap(N.test_depth, niches = c(1, 2), data = T, background = T, proportion = 1,
background_type = 'back_union', change_labels = T,
data_col = c('darkred', 'darkorange'), niche_col = c('darkred', 'darkorange'),
background_col = viridis::cividis, legend = F)
decorate3d(aspect = c(1, 1, 1), xlab = '', ylab = '', zlab = '')
axes3d(xat = c(-4000, -2000, 0, 2000), yat = c(20, 15, 10), zat = c(0, 0.2, 0.4), box = T)
mtext3d('Bathymetry (m)', edge = 'x-+', line = 1.25)
mtext3d('Sea surface temperature (ºC)', edge = 'y-+', line = 0, at = 12)
mtext3d('Diffuse attenuation Kd490 (1/m)', edge = 'z+-', line = 0, at = 0.32)
snapshot3d('Ellipsoid_depth.png', fmt = 'png', top = T, width = 1000, height = 1000, webshot = F)
ggplot() +
geom_histogram(aes(N.test_depth@significance_results$union_random$Niche_1_vs_2$overlap),
fill = '#00204DFF', bins = 60) +
geom_vline(xintercept = quantile(N.test_depth@significance_results$union_random$Niche_1_vs_2$overlap, 0.05),
colour = '#FFEA46FF', size = 0.75, linetype = 'dashed') +
geom_vline(xintercept = N.test_depth@union_overlap$overlap,
colour = '#7C7B78FF', size = 0.75, linetype = 'dashed') +
theme_bw() + xlab('Overlap') + ylab('Frequency')
ggsave('Test_depth.pdf', dpi = 900, width = 12, height = 10, units = 'cm')
# Slope
rgl.open(); rgl.bg(color = 'white')
plot_overlap(N.test_slope, niches = c(1, 2), data = T, background = T, proportion = 1,
background_type = 'back_union', change_labels = T,
data_col = c('darkred', 'darkorange'), niche_col = c('darkred', 'darkorange'),
background_col = viridis::cividis, legend = F)
decorate3d(aspect = c(1, 1, 1), xlab = '', ylab = '', zlab = '')
axes3d(xat = c(0, 0.2, 0.4), yat = c(20, 15, 10), zat = c(-2, 2, 6), box = T)
mtext3d('Sea surface temperature (ºC)', edge = 'y++', line = 0, at = 12)
mtext3d('Diffuse attenuation Kd490 (1/m)', edge = 'x--', line = 0, at = 0.32)
mtext3d('Slope (º)', edge = 'z+-', line = 1.25)
snapshot3d('Ellipsoid_slope.png', fmt = 'png', top = T, width = 1000, height = 1000, webshot = F)
ggplot() +
geom_histogram(aes(N.test_slope@significance_results$union_random$Niche_1_vs_2$overlap),
fill = '#00204DFF', bins = 60) +
geom_vline(xintercept = quantile(N.test_slope@significance_results$union_random$Niche_1_vs_2$overlap, 0.05),
colour = '#FFEA46FF', size = 0.75, linetype = 'dashed') +
geom_vline(xintercept = N.test_slope@union_overlap$overlap,
colour = '#7C7B78FF', size = 0.75, linetype = 'dashed') +
theme_bw() + xlab('Overlap') + ylab('Frequency')
ggsave('Test_slope.pdf', dpi = 900, width = 12, height = 10, units = 'cm')
# Distance to coast
rgl.open(); rgl.bg(color = 'white')
plot_overlap(N.test_dist, niches = c(1, 2), data = T, background = T, proportion = 1,
background_type = 'back_union', change_labels = T,
data_col = c('darkred', 'darkorange'), niche_col = c('darkred', 'darkorange'),
background_col = viridis::cividis, legend = F)
decorate3d(aspect = c(1, 1, 1), xlab = '', ylab = '', zlab = '')
axes3d(xat = c(-50, 50, 150, 250), yat = c(20, 15, 10), zat = c(0, 0.2, 0.4), box = T)
mtext3d('Sea surface temperature (ºC)', edge = 'y+-', line = 0, at = 18)
mtext3d('Diffuse attenuation Kd490 (1/m)', edge = 'z--', line = 0, at = 0.09)
mtext3d('Distance to coast (km)', edge = 'x--', line = 1.25)
snapshot3d('Ellipsoid_dist.png', fmt = 'png', top = T, width = 1000, height = 1000, webshot = F)
ggplot() +
geom_histogram(aes(N.test_dist@significance_results$union_random$Niche_1_vs_2$overlap),
fill = '#00204DFF', bins = 60) +
geom_vline(xintercept = quantile(N.test_dist@significance_results$union_random$Niche_1_vs_2$overlap, 0.05),
colour = '#FFEA46FF', size = 0.75, linetype = 'dashed') +
geom_vline(xintercept = N.test_dist@union_overlap$overlap,
colour = '#7C7B78FF', size = 0.75, linetype = 'dashed') +
theme_bw() + xlab('Overlap') + ylab('Frequency')
ggsave('Test_dist.pdf', dpi = 900, width = 12, height = 10, units = 'cm')
#----------------------------------- Figure S5 ---------------------------------------------
# Calibration areas of the three fits
# Global fit
Fit <- 'GLOBAL'
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
env.M <- stack(paste(Fit, '/', Fit, '_calibration_areas.tiff', sep = ''))[[1]] # Calibration area
env.M <- extend(env.M, extent(-180, 180, -64, 58.25)) # slighty bigger for plotting
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # Calibration areas
pred <- crop(pred, env.M)
pred_cal <- mask(pred, env.M)
coast.x <- crop(coast, pred_cal) # crop coastline to calibration area to save time
pred_buff <- pred_cal > -Inf
pred_buff <- rasterToPolygons(pred_buff, dissolve = T) # create buffer around predictions
pred_cal[pred_cal >=0] <- 1
df <- data.frame(coordinates(pred_cal), as.data.frame(pred_cal))
ggplot(data = df) +
geom_tile(aes(x = x, y = y, fill = as.factor(GLOBAL_predictions))) +
geom_polygon(data = pred_buff, aes(x = long, y = lat, group = group), color = 'grey5', fill = NA, size = 0.75) +
geom_polygon(data = coast.x, aes(x = long, y = lat, group = group), color = 'grey30', fill = 'grey50', size = 0.1) +
geom_point(data = occ_cal, aes(x = Longitude, y = Latitude), shape = 21, size = 0.35, color = '#FDE725FF', fill = '#21908CFF') +
scale_fill_manual(values = '#440154FF', na.value = 'grey95') + coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave(paste('Figure S5_', Fit, '.tiff', sep = ''), dpi = 900, width = 20, height = 8, units = 'cm')
# Regional merged fit
Fit <- 'REGIONAL_MERGED'
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
env.M <- stack(paste(Fit, '/', Fit, '_calibration_areas.tiff', sep = ''))[[1]] # Calibration area
env.M <- extend(env.M, extent(-71.15, 164.25, -63.05, -21.05)) # slighty bigger for plotting
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # Calibration areas
pred <- crop(pred, env.M)
pred_cal <- mask(pred, env.M)
coast.x <- crop(coast, pred_cal) # crop coastline to calibration area to save time
pred_buff <- pred_cal > -Inf
pred_buff <- rasterToPolygons(pred_buff, dissolve = T) # create buffer around predictions
pred_cal[pred_cal >=0] <- 1
df <- data.frame(coordinates(pred_cal), as.data.frame(pred_cal))
ggplot(data = df) +
geom_tile(aes(x = x, y = y, fill = as.factor(REGIONAL_MERGED_predictions))) +
geom_polygon(data = pred_buff, aes(x = long, y = lat, group = group), color = 'grey5', fill = NA, size = 0.8) +
geom_polygon(data = coast.x, aes(x = long, y = lat, group = group), color = 'grey30', fill = 'grey50', size = 0.1) +
geom_point(data = occ_cal, aes(x = Longitude, y = Latitude), shape = 21, size = 0.35, color = '#FDE725FF', fill = '#21908CFF') +
scale_fill_manual(values = '#440154FF', na.value = 'grey95') + coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-30, -50), labels = c('30ºS', '50ºS')) +
scale_x_continuous(name = NULL, breaks = c(-50, 50, 150), labels = c('50ºW', '50ºE', '150ºE'))
ggsave(paste('Figure S5_', Fit, '.tiff', sep = ''), dpi = 900, width = 20, height = 8, units = 'cm')
# Regional independent fit
Fit <- 'REGIONAL_INDEPENDENT'
# Southwest Atlantic
Region <- 'SWA'
occ_cal <- read.csv(paste(Fit, '/', Region, '/', Fit, '_', Region, '_calibration_points.csv', sep = '')) # Calibration points
env.M <- stack(paste(Fit, '/', Region, '/', Fit, '_', Region, '_calibration_areas.tiff', sep = ''))[[1]] # Calibration area
env.M <- extend(env.M, extent(-70, -38.4, -61.9, -22.4)) # slighty bigger for plotting
pred <- raster(paste(Fit, '/', Region, '/', Fit, '_', Region, '_predictions.tiff', sep = '')) # Calibration areas
pred <- crop(pred, env.M)
pred_cal <- mask(pred, env.M)
coast.x <- crop(coast, pred_cal) # crop coastline to calibration area to save time
pred_buff <- pred_cal > -Inf
pred_buff <- rasterToPolygons(pred_buff, n = 16, dissolve = T) # create buffer around predictions
pred_cal[pred_cal >=0] <- 1
df <- data.frame(coordinates(pred_cal), as.data.frame(pred_cal))
ggplot(data = df) +
geom_tile(aes(x = x, y = y, fill = as.factor(REGIONAL_INDEPENDENT_SWA_predictions))) +
geom_polygon(data = pred_buff, aes(x = long, y = lat, group = group), color = 'grey5', fill = NA, size = 1.5) +
geom_polygon(data = coast.x, aes(x = long, y = lat, group = group), color = 'grey30', fill = 'grey50', size = 0.1) +
geom_point(data = occ_cal, aes(x = Longitude, y = Latitude), shape = 21, size = 1, stroke = 1.25, color = '#FDE725FF', fill = '#21908CFF') +
scale_fill_manual(values = '#440154FF', na.value = 'grey95') + coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-30, -50), labels = c('30ºS', '50ºS')) +
scale_x_continuous(name = NULL, breaks = c(-65, -45), labels = c('65ºW', '45ºW'))
ggsave(paste('Figure S5_', Fit, '_', Region, '.tiff', sep = ''), dpi = 900, width = 12.8, height = 15, units = 'cm')
# Southern Australia
Region <- 'AUS'
occ_cal <- read.csv(paste(Fit, '/', Region, '/', Fit, '_', Region, '_calibration_points.csv', sep = '')) # Calibration points
env.M <- stack(paste(Fit, '/', Region, '/', Fit, '_', Region, '_calibration_areas.tiff', sep = ''))[[1]] # Calibration area
env.M <- extend(env.M, extent(113.35, 163.25, -54, -23.9)) # slighty bigger for plotting
pred <- raster(paste(Fit, '/', Region, '/', Fit, '_', Region, '_predictions.tiff', sep = '')) # Calibration areas
pred <- crop(pred, env.M)
pred_cal <- mask(pred, env.M)
coast.x <- crop(coast, pred_cal) # crop coastline to calibration area to save time
pred_buff <- pred_cal > -Inf
pred_buff <- rasterToPolygons(pred_buff, dissolve = T) # create buffer around predictions
pred_cal[pred_cal >=0] <- 1
df <- data.frame(coordinates(pred_cal), as.data.frame(pred_cal))
ggplot(data = df) +
geom_tile(aes(x = x, y = y, fill = as.factor(REGIONAL_INDEPENDENT_AUS_predictions))) +
geom_polygon(data = pred_buff, aes(x = long, y = lat, group = group), color = 'grey5', fill = NA, size = 1.5) +
geom_polygon(data = coast.x, aes(x = long, y = lat, group = group), color = 'grey30', fill = 'grey50', size = 0.1) +
geom_point(data = occ_cal, aes(x = Longitude, y = Latitude), shape = 21, size = 1, stroke = 1.25, color = '#FDE725FF', fill = '#21908CFF') +
scale_fill_manual(values = '#440154FF', na.value = 'grey95') + coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, -30), labels = c('50ºS', '30ºS')) +
scale_x_continuous(name = NULL, breaks = c(120, 140, 160), labels = c('120ºE', '140ºE', '160ºE'))
ggsave(paste('Figure S5_', Fit, '_', Region, '.tiff', sep = ''), dpi = 900, width = 18.4, height = 11.7, units = 'cm')
# Global map for referencing
ggplot() +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), color = 'grey5', fill = 'grey5', size = 0.1) +
coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'grey95'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave(paste('Figure S5_Global_map.tiff', sep = ''), dpi = 900, width = 20, height = 8, units = 'cm')
#----------------------------------- Figure S6 ---------------------------------------------
# Response plots for the three fits
# Get feature classes and regularization multipliers from results
fc <- c('lq', 'lq', 'lq', 'lqp') # order: global, merged, SWA and AUS
rm <- c(0.1, 0.1, 0.1, 0.3) # order: global, merged, SWA and AUS
# Fits
Fits <- c('GLOBAL', 'REGIONAL_MERGED', 'REGIONAL_INDEPENDENT')
Regions <- c('SWA', 'AUS')
p_df = data.frame()
a_df = data.frame()
plot_data_df = data.frame()
for(k in 1:3){
if(Fits[k] %in% c('GLOBAL', 'REGIONAL_MERGED')) {
occ_cal <- read.csv(paste(Fits[k], '/', Fits[k], '_calibration_points.csv', sep = '')) # Calibration points
occ_cal <- occ_cal[, c('Longitude', 'Latitude')]
env.M <- stack(paste(Fits[k], '/', Fits[k], '_calibration_areas.tiff', sep = '')) # Calibration area
names(env.M) <- var_set
for(j in names(env.M)) {
# prepare data
env = env.M[[j]]
env <- stack(env)
Vars = j
# background points
set.seed(111)
notna <- which(complete.cases(values(env)))
samp <- sample(notna, 10000, replace = F)
samplocs <- as.data.frame(xyFromCell(env, samp))
# SWD object
data <- prepareSWD(species = Species, p = occ_cal, a = samplocs, env = env)
# run maxent replicates
folds = randomFolds(data, k = 10, only_presence = T, seed = 111)
default_model <- train(method = 'Maxent', data = data, fc = fc[k], reg = rm[k], iter = 1000, folds = folds)
# presences and absences with variable data
p <- .get_presence(default_model@data)
p$var <- j
names(p)[names(p) == j] <- 'values'
p$Fit <- Fits[k]
a <- .get_absence(default_model@data)
a$var <- j
names(a)[names(a) == j] <- 'values'
a$Fit <- Fits[k]
pred <- as.data.frame(matrix(data = NA, nrow = dim(data@data)[1], ncol = 10))
for(i in 1:10){
pred[, i] <- predict(default_model@models[[i]], data = data@data, type = 'cloglog')
}
# plot data
plot_data <- as.data.frame(matrix(data = NA, nrow = dim(data@data)[1], ncol = 4))
names(plot_data) <- c('mean', 'sd', 'max', 'min')
plot_data$mean <- rowMeans(pred)
plot_data$sd <- apply(pred, 1, sd)
plot_data$max <- plot_data$mean + plot_data$sd
plot_data$min <- plot_data$mean - plot_data$sd
plot_data$var <- j
plot_data$values <- data@data[, j]
plot_data$Fit <- Fits[k]
# data frames
p_df <- rbind(p_df, p)
a_df <- rbind(a_df, a)
plot_data_df <- rbind(plot_data_df, plot_data)
}}
if(Fits[k] == 'REGIONAL_INDEPENDENT') {
for(l in 1:2) {
occ_cal <- read.csv(paste(Fits[k], '/', Regions[l], '/', Fits[k], '_', Regions[l], '_calibration_points.csv', sep = '')) # Calibration points
occ_cal <- occ_cal[, c('Longitude', 'Latitude')]
env.M <- stack(paste(Fits[k], '/', Regions[l], '/', Fits[k], '_', Regions[l], '_calibration_areas.tiff', sep = '')) # Calibration area
names(env.M) <- var_set
for(j in names(env.M)) {
# prepare data
env = env.M[[j]]
env <- stack(env)
Vars = j
# background points
set.seed(111)
notna <- which(complete.cases(values(env)))
samp <- sample(notna, 10000, replace = F)
samplocs <- as.data.frame(xyFromCell(env, samp))
# SWD object
data <- prepareSWD(species = Species, p = occ_cal, a = samplocs, env = env)
# run maxent replicates
if(Regions[l] == 'SWA') {kx = 3} else {kx = 4}
folds = randomFolds(data, k = 10, only_presence = T, seed = 111)
default_model <- train(method = 'Maxent', data = data, fc = fc[kx], reg = rm[kx], iter = 1000, folds = folds)
# presences and absences with variable data
p <- .get_presence(default_model@data)
p$var <- j
names(p)[names(p) == j] <- 'values'
p$Fit <- paste(Fits[k], '_', Regions[l], sep = '')
a <- .get_absence(default_model@data)
a$var <- j
names(a)[names(a) == j] <- 'values'
a$Fit <- paste(Fits[k], '_', Regions[l], sep = '')
pred <- as.data.frame(matrix(data = NA, nrow = dim(data@data)[1], ncol = 10))
for(i in 1:10){
pred[, i] <- predict(default_model@models[[i]], data = data@data, type = 'cloglog')
}
# plot data
plot_data <- as.data.frame(matrix(data = NA, nrow = dim(data@data)[1], ncol = 4))
names(plot_data) <- c('mean', 'sd', 'max', 'min')
plot_data$mean <- rowMeans(pred)
plot_data$sd <- apply(pred, 1, sd)
plot_data$max <- plot_data$mean + plot_data$sd
plot_data$min <- plot_data$mean - plot_data$sd
plot_data$var <- j
plot_data$values <- data@data[, j]
plot_data$Fit <- paste(Fits[k], '_', Regions[l], sep = '')
# data frames
p_df <- rbind(p_df, p)
a_df <- rbind(a_df, a)
plot_data_df <- rbind(plot_data_df, plot_data)
}}}
}
# Re-order
order_i_want1 <- c('Temperature', 'Distance_to_coast', 'Surface_temperature', 'Kd490', 'Salinity', 'Primary_productivity')
order_i_want2 <- c('GLOBAL', 'REGIONAL_MERGED', 'REGIONAL_INDEPENDENT_SWA', 'REGIONAL_INDEPENDENT_AUS')
p_df <- p_df[!(p_df$var == 'Salinity' & p_df$values < 32.5), ]
p_df <- p_df[!(p_df$var == 'Distance_to_coast' & p_df$values > 500), ]
p_df <- p_df[!(p_df$var == 'Surface_temperature' & p_df$values < 5), ]
p_df <- p_df[!(p_df$var == 'Kd490' & p_df$values > 0.55), ]
p_df <- p_df[!(p_df$var == 'Primary_productivity' & p_df$values > 0.07), ]
p_df <- transform(p_df, var = factor(var, levels = order_i_want1))
p_df <- transform(p_df, Fit = factor(Fit, levels = order_i_want2))
a_df <- a_df[!(a_df$var == 'Salinity' & a_df$values < 32.5), ]
a_df <- a_df[!(a_df$var == 'Distance_to_coast' & a_df$values > 500), ]
a_df <- a_df[!(a_df$var == 'Surface_temperature' & a_df$values < 5), ]
a_df <- a_df[!(a_df$var == 'Kd490' & a_df$values > 0.55), ]
a_df <- a_df[!(a_df$var == 'Primary_productivity' & a_df$values > 0.07), ]
a_df <- transform(a_df, var = factor(var, levels = order_i_want1))
a_df <- transform(a_df, Fit = factor(Fit, levels = order_i_want2))
plot_data_df <- plot_data_df[!(plot_data_df$var == 'Salinity' & plot_data_df$values < 32.5), ]
plot_data_df <- plot_data_df[!(plot_data_df$var == 'Distance_to_coast' & plot_data_df$values > 500), ]
plot_data_df <- plot_data_df[!(plot_data_df$var == 'Surface_temperature' & plot_data_df$values < 5), ]
plot_data_df <- plot_data_df[!(plot_data_df$var == 'Kd490' & plot_data_df$values > 0.55), ]
plot_data_df <- plot_data_df[!(plot_data_df$var == 'Primary_productivity' & plot_data_df$values > 0.07), ]
plot_data_df <- transform(plot_data_df, var = factor(var, levels = order_i_want1))
plot_data_df <- transform(plot_data_df, Fit = factor(Fit, levels = order_i_want2))
# Labeller
VAR_names = as_labeller(c(Temperature = 'Temperature~(ºC)', Distance_to_coast = 'Dist.~to~coast~(km)',
Surface_temperature = 'Surface~temp.~(ºC)', Kd490 = 'Kd490~(m^-1)',
Salinity = 'Salinity~(psu)', Primary_productivity = 'P.~prod.~(g~C~m^-2~day^-1)',
GLOBAL = 'Global', REGIONAL_MERGED = 'Regional~merged', REGIONAL_INDEPENDENT_SWA = 'SW~Atlantic',
REGIONAL_INDEPENDENT_AUS = 'Australia'), default = label_parsed)
# Plot
ggplot(data = plot_data_df, aes(x = values, y = mean, ymin = min, ymax = max)) +
geom_line(colour = '#440154FF') +
geom_ribbon(fill = '#440154FF', alpha = 0.2) +
geom_rug(data = p_df, inherit.aes = F, aes(values), sides = 't', color = '#FDE725FF', size = 0.3) +
geom_rug(data = a_df, inherit.aes = F, aes(values), sides = 'b', color = '#21908CFF', size = 0.3) +
labs(x = NULL, y = 'Cloglog output') + ylim(0, 1) +
theme(panel.background = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_rect(colour = 'black', fill = NA, size = 0.35),
panel.grid.major = element_line(size = 0.2, colour = 'grey90'),
strip.background = element_rect(fill = 'transparent'),
strip.text = element_text(vjust = -0.5, size = 9),
plot.margin = unit(c(0, 0, 0.2, 0.2), 'cm'),
axis.title = element_text(size = 10)) +
facet_grid(c('Fit', 'var'), scales = 'free', labeller = VAR_names)
ggsave('Figure S6.pdf', width = 23, height = 12, units = 'cm')
#----------------------------------- Figure 1 ---------------------------------------------
# Binary global transfer
Fit <- 'GLOBAL'
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # best model predictions
bg_binary <- pred
bg_binary[bg_binary >= 0] <- 1 # background raster
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
# 10% and 5% Minimum training presence thresholds
mtp5 <- sdm_threshold.5(pred, occ_cal[, c('Longitude', 'Latitude')], 'p05', binary = F)
bin_5 <- pred
bin_5[bin_5 < minValue(mtp5)] <- NA
bin_5[bin_5 >= minValue(mtp5)] <- 2
mtp10 <- sdm_threshold.10(pred, occ_cal[, c('Longitude', 'Latitude')], 'p10', binary = F)
bin_10 <- pred
bin_10[bin_10 < minValue(mtp10)] <- NA
bin_10[bin_10 >= minValue(mtp10)] <- 3
# Areas of strict extrapolation
global_mop <- raster(paste(Fit, '/', Fit, '_mop.tiff', sep = ''))
global_mop[global_mop > 0] <- NA
global_mop[global_mop == 0] <- 8
# Final mosaic
mod_mosaic <- mosaic(bg_binary, bin_10, bin_5, global_mop, fun = sum)
mod_mosaic[mod_mosaic == 1] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 3] <- 2 # MTP5
mod_mosaic[mod_mosaic == 6] <- 3 # overlapping MTPs
mod_mosaic[mod_mosaic > 6] <- 4 # strict extrapolation areas
mod_mosaic <- crop(mod_mosaic, coast)
# Define bounding box of regions predicted as suitable
swa_ext <- extent(-69.69, -45.5, -52.25, -24.85) # Southwest Atlantic
aus_ext <- extent(112, 155, -45.5, -24.5) # Australia
nep_ext <- extent(-136, -109, 22.54, 57.22) # Northeast Pacific
nwa_ext <- extent(-82.1, -57.8, 29.65, 45.65) # Northwest Atlantic
azo_ext <- extent(-34, -23.5, 35.8, 42) # Azores Is.
nea_ext <- extent(-19.25, 20.5, 15.54, 71) # Northeast Atlantic
nwp_ext <- extent(117, 144.72, 23.58, 47) # Northwest Pacific
gal_ext <- extent(-93, -88.25, -2.5, 1.25) # Galápagos Is.
sep_ext <- extent(-82.5, -69, -55, -3.17) # Southeast Pacific
tdc_ext <- extent(-15.5, -6.5, -42.5, -35) # Tristan da Cunha Is.
sea_ext <- extent(10, 30.21, -37.5, -13.5) # Southeast Atlantic
ams_ext <- extent(74.5, 80.75, -41.5, -35.25) # Amsterdam I.
nze1_ext <- extent(163, 179.99, -54.5, -27) # New Zealand 1
nze2_ext <- extent(-179.99, -172.5, -54.5, -27) # New Zealand 2
Ids <- c('Southwest Atlantic', 'Australia', 'Northeast Pacific', 'Northwest Atlantic', 'Azores Is.',
'Northeast Atlantic', 'Northwest Pacific', 'Galápagos Is.', 'Southeast Pacific', 'Tristan da Cunha Is.',
'Southeast Atlantic', 'Amsterdam I.', 'New Zealand 1', 'New Zealand 2')
polys <- SpatialPolygons(list(
Polygons(list(Polygon(swa_ext)), Ids[1]), Polygons(list(Polygon(aus_ext)), Ids[2]),
Polygons(list(Polygon(nep_ext)), Ids[3]), Polygons(list(Polygon(nwa_ext)), Ids[4]),
Polygons(list(Polygon(azo_ext)), Ids[5]), Polygons(list(Polygon(nea_ext)), Ids[6]),
Polygons(list(Polygon(nwp_ext)), Ids[7]), Polygons(list(Polygon(gal_ext)), Ids[8]),
Polygons(list(Polygon(sep_ext)), Ids[9]), Polygons(list(Polygon(tdc_ext)), Ids[10]),
Polygons(list(Polygon(sea_ext)), Ids[11]), Polygons(list(Polygon(ams_ext)), Ids[12]),
Polygons(list(Polygon(nze1_ext)), Ids[13]), Polygons(list(Polygon(nze2_ext)), Ids[14])),
proj4string = crs)
polys <- SpatialPolygonsDataFrame(polys, data.frame(ids = Ids, row.names = names(polys)))
Col <- c('grey95', 'grey80', '#FDE725FF', '#440154FF', 'grey90') # set colours for each code
df <- data.frame(coordinates(mod_mosaic), as.data.frame(mod_mosaic))
ggplot() +
geom_tile(data = df, aes(x = x, y = y, fill = as.factor(layer))) + scale_fill_manual(values = Col) +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), col = 'grey30', fill = 'grey95', size = 0.1) +
geom_polygon(data = polys, aes(x = long, y = lat, group = group), col = 'grey40', fill = NA, size = 0.45) +
coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave('Figure 1.tiff', dpi = 900, width = 20, height = 8.5, units = 'cm')
#----------------------------------- Figure S7 ---------------------------------------------
# Continuos global transfer
Fit <- 'GLOBAL'
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # best model predictions
# Areas of strict extrapolation
global_mop <- raster(paste(Fit, '/', Fit, '_mop.tiff', sep = ''))
global_mop[global_mop > 0] <- NA
global_mop[global_mop == 0] <- 2
# Erase areas of strict extrapolation from prediction
pred_mop <- mosaic(pred, global_mop, fun = max)
pred_mop[pred_mop > 1] <- NA # minus areas of strict extrapolation
pred_mop <- crop(pred_mop, coast)
df <- data.frame(coordinates(pred_mop), as.data.frame(pred_mop))
ggplot() +
geom_tile(data = df, aes(x = x, y = y, fill = layer)) +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), col = 'black', fill = 'grey10', size = 0.1) +
scale_fill_viridis(na.value = '#440154FF') + coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave('Figure S7.tiff', dpi = 900, width = 20, height = 8.5, units = 'cm')
#----------------------------------- Figure S8 ---------------------------------------------
# Binary regional transfers
# Regional merged fit
Fit <- 'REGIONAL_MERGED'
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # best model predictions
bg_binary <- pred
bg_binary[bg_binary >= 0] <- 1 # background raster
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
# 10% and 5% Minimum training presence thresholds
mtp5 <- sdm_threshold.5(pred, occ_cal[, c('Longitude', 'Latitude')], 'p05', binary = F)
bin_5 <- pred
bin_5[bin_5 < minValue(mtp5)] <- NA
bin_5[bin_5 >= minValue(mtp5)] <- 2
mtp10 <- sdm_threshold.10(pred, occ_cal[, c('Longitude', 'Latitude')], 'p10', binary = F)
bin_10 <- pred
bin_10[bin_10 < minValue(mtp10)] <- NA
bin_10[bin_10 >= minValue(mtp10)] <- 3
# Areas of strict extrapolation
global_mop <- raster(paste(Fit, '/', Fit, '_mop.tiff', sep = ''))
global_mop[global_mop > 0] <- NA
global_mop[global_mop == 0] <- 8
# Final mosaic
mod_mosaic <- mosaic(bg_binary, bin_10, bin_5, global_mop, fun = sum)
mod_mosaic[mod_mosaic == 1] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 3] <- 2 # MTP5
mod_mosaic[mod_mosaic == 6] <- 3 # overlapping MTPs
mod_mosaic[mod_mosaic > 6] <- 4 # strict extrapolation areas
mod_mosaic <- crop(mod_mosaic, coast)
# Set colours for each code
Col <- c('grey95', 'grey80', '#FDE725FF', '#440154FF', 'grey90')
df <- data.frame(coordinates(mod_mosaic), as.data.frame(mod_mosaic))
ggplot() +
geom_tile(data = df, aes(x = x, y = y, fill = as.factor(layer))) + scale_fill_manual(values = Col) + coord_equal(expand = 0) +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), col = 'grey30', fill = 'grey95', size = 0.1) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave(paste('Figure S8_', Fit,'.tiff', sep = ''), dpi = 900, width = 20, height = 8.5, units = 'cm')
# Regional independent fit
Fit <- 'REGIONAL_INDEPENDENT'
Region1 <- 'SWA'
Region2 <- 'AUS'
swa_raw <- raster(paste(Fit, '/', Region1, '/', Fit, '_', Region1, '_predictions.tiff', sep = '')) # best model predictions
aus_raw <- raster(paste(Fit, '/', Region2, '/', Fit, '_', Region2, '_predictions.tiff', sep = '')) # best model predictions
bg_binary <- swa_raw
bg_binary[bg_binary >= 0] <- 1 # background raster
# All occurrences used for modelling
swa_occ <- read.csv(paste(Fit, '/', Region1, '/', Fit, '_', Region1, '_calibration_points.csv', sep = '')) # Calibration points
aus_occ <- read.csv(paste(Fit, '/', Region2, '/', Fit, '_', Region2, '_calibration_points.csv', sep = '')) # Calibration points
# 10% Minimum training presence thresholds
swa_mtp10 <- sdm_threshold.10(swa_raw, swa_occ[, c('Longitude', 'Latitude')], 'p10', binary = F)
swa_bin_10 <- swa_raw
swa_bin_10[swa_bin_10 < minValue(swa_mtp10)] <- NA
swa_bin_10[swa_bin_10 >= minValue(swa_mtp10)] <- 2
aus_mtp10 <- sdm_threshold.10(aus_raw, aus_occ[, c('Longitude', 'Latitude')], 'p10', binary = F)
aus_bin_10 <- aus_raw
aus_bin_10[aus_bin_10 < minValue(aus_mtp10)] <- NA
aus_bin_10[aus_bin_10 >= minValue(aus_mtp10)] <- 3
# MOP mosaic
swa_mop <- raster(paste(Fit, '/', Region1, '/', Fit, '_', Region1, '_mop.tiff', sep = ''))
swa_mop[swa_mop > 0] <- NA
swa_mop[swa_mop == 0] <- 8
aus_mop <- raster(paste(Fit, '/', Region2, '/', Fit, '_', Region2, '_mop.tiff', sep = ''))
aus_mop[aus_mop > 0] <- NA
aus_mop[aus_mop == 0] <- 8
mop_mosaic <- mosaic(swa_mop, aus_mop, fun = sum)
mop_mosaic[mop_mosaic < 8] <- NA
mop_mosaic[mop_mosaic >= 8] <- 8 # areas of strict extrapolation
# SWA mosaic
swa_mosaic <- mosaic(bg_binary, swa_bin_10, mop_mosaic, fun = sum)
swa_mosaic[swa_mosaic > 3] <- 8
# AUS mosaic
aus_mosaic <- mosaic(bg_binary, aus_bin_10, mop_mosaic, fun = sum)
aus_mosaic[aus_mosaic > 4] <- 8
# Final mosaic
mod_mosaic <- mosaic(swa_mosaic, aus_mosaic, fun = sum)
mod_mosaic[mod_mosaic == 2] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 4] <- 2 # swa MTP
mod_mosaic[mod_mosaic == 5] <- 3 # aus MTP
mod_mosaic[mod_mosaic == 7] <- 4 # overlapping MTPs
mod_mosaic[mod_mosaic == 16] <- 5 # strict extrapolation areas
mod_mosaic[mod_mosaic == 9] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 11] <- 5 # strict extrapolation areas
mod_mosaic[mod_mosaic == 12] <- 5 # strict extrapolation areas
mod_mosaic <- crop(mod_mosaic, coast)
# Set colours for each code
Col <- c('grey95', 'grey80', '#FDE725FF', '#21908CFF', '#440154FF', 'grey90')
df <- data.frame(coordinates(mod_mosaic), as.data.frame(mod_mosaic))
ggplot() +
geom_tile(data = df, aes(x = x, y = y, fill = as.factor(layer))) + scale_fill_manual(values = Col) +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), col = 'grey30', fill = 'grey95', size = 0.1) +
coord_equal(expand = 0) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave(paste('Figure S8_', Fit,'.tiff', sep = ''), dpi = 900, width = 20, height = 8.5, units = 'cm')
# Three-predictor model (simpler model)
Fit <- 'GLOBAL'
pred <- raster(paste(Fit, '/', Fit, '_predictions_3predictors.tiff', sep = '')) # best model predictions
bg_binary <- pred
bg_binary[bg_binary >= 0] <- 1 # background raster
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
# 10% and 5% Minimum training presence thresholds
mtp5 <- sdm_threshold.5(pred, occ_cal[, c('Longitude', 'Latitude')], 'p05', binary = F)
bin_5 <- pred
bin_5[bin_5 < minValue(mtp5)] <- NA
bin_5[bin_5 >= minValue(mtp5)] <- 2
mtp10 <- sdm_threshold.10(pred, occ_cal[, c('Longitude', 'Latitude')], 'p10', binary = F)
bin_10 <- pred
bin_10[bin_10 < minValue(mtp10)] <- NA
bin_10[bin_10 >= minValue(mtp10)] <- 3
# Areas of strict extrapolation
global_mop <- raster(paste(Fit, '/', Fit, '_mop.tiff', sep = ''))
global_mop[global_mop > 0] <- NA
global_mop[global_mop == 0] <- 8
# Final mosaic
mod_mosaic <- mosaic(bg_binary, bin_10, bin_5, global_mop, fun = sum)
mod_mosaic[mod_mosaic == 1] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 3] <- 2 # MTP5
mod_mosaic[mod_mosaic == 6] <- 3 # overlapping MTPs
mod_mosaic[mod_mosaic > 6] <- 4 # strict extrapolation areas
mod_mosaic <- crop(mod_mosaic, coast)
# Set colours for each code
Col <- c('grey95', 'grey80', '#FDE725FF', '#440154FF', 'grey90')
df <- data.frame(coordinates(mod_mosaic), as.data.frame(mod_mosaic))
ggplot() +
geom_tile(data = df, aes(x = x, y = y, fill = as.factor(layer))) + scale_fill_manual(values = Col) + coord_equal(expand = 0) +
geom_polygon(data = coast, aes(x = long, y = lat, group = group), col = 'grey30', fill = 'grey95', size = 0.1) +
theme(panel.background = element_rect(fill = 'transparent'), panel.grid = element_blank(),
legend.position = 'none', axis.text = element_text(size = 8),
panel.border = element_rect(colour = 'black', fill = NA, size = 1)) +
scale_y_continuous(name = NULL, breaks = c(-50, 0, 50), labels = c('50ºS', '0º', '50ºN')) +
scale_x_continuous(name = NULL, breaks = c(-100, 0, 100), labels = c('100ºW', '0º', '100ºE'))
ggsave(paste('Figure S8_', Fit,'_3predictors.tiff', sep = ''), dpi = 900, width = 20, height = 8.5, units = 'cm')
#----------------------------------- Figure 2 ---------------------------------------------
# Continental suitable regions
Fit <- 'GLOBAL'
pred <- raster(paste(Fit, '/', Fit, '_predictions.tiff', sep = '')) # best model predictions
bg_binary <- pred
bg_binary[bg_binary >= 0] <- 1 # background raster
occ_cal <- read.csv(paste(Fit, '/', Fit, '_calibration_points.csv', sep = '')) # Calibration points
# 10% and 5% Minimum training presence thresholds
mtp5 <- sdm_threshold.5(pred, occ_cal[, c('Longitude', 'Latitude')], 'p05', binary = F)
bin_5 <- pred
bin_5[bin_5 < minValue(mtp5)] <- NA
bin_5[bin_5 >= minValue(mtp5)] <- 2
mtp10 <- sdm_threshold.10(pred, occ_cal[, c('Longitude', 'Latitude')], 'p10', binary = F)
bin_10 <- pred
bin_10[bin_10 < minValue(mtp10)] <- NA
bin_10[bin_10 >= minValue(mtp10)] <- 3
# Areas of strict extrapolation
global_mop <- raster(paste(Fit, '/', Fit, '_mop.tiff', sep = ''))
global_mop[global_mop > 0] <- NA
global_mop[global_mop == 0] <- 8
# Final mosaic
mod_mosaic <- mosaic(bg_binary, bin_10, bin_5, global_mop, fun = sum)
mod_mosaic[mod_mosaic == 1] <- 1 # unsuitable areas
mod_mosaic[mod_mosaic == 3] <- 2 # MTP5
mod_mosaic[mod_mosaic == 6] <- 3 # overlapping MTPs
mod_mosaic[mod_mosaic > 6] <- 4 # strict extrapolation areas
mod_mosaic <- crop(mod_mosaic, coast)
# Define bounding box of regions predicted as suitable
swa_ext <- extent(-69.69, -45.5, -52.25, -24.85) # Southwest Atlantic
aus_ext <- extent(112, 155, -45.5, -24.5) # Australia
nep_ext <- extent(-136, -109, 22.54, 57.22) # Northeast Pacific
nwp_ext <- extent(117, 144.72, 23.58, 47) # Northwest Pacific
sep_ext <- extent(-82.5, -69, -55, -3.17) # Southeast Pacific
sea_ext <- extent(10, 30.21, -37.5, -13.5) # Southeast Atlantic
nze1_ext <- extent(163, 179.99, -54.5, -27) # New Zealand 1
nze2_ext <- extent(-179.99, -172.5, -54.5, -27) # New Zealand 2
# Read occurrence data
dat <- read.csv('Appendix C.csv')
## Southwest Atlantic ##
swa_occ <- subset(dat, Region == 'Southwest Atlantic') # subset occurrences
swa_occ1 <- subset(swa_occ, Source1 == 'Published literature')