-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script#19.8_OVERSEE_figures_prepare.R
1661 lines (1466 loc) · 87.7 KB
/
Script#19.8_OVERSEE_figures_prepare.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
##### 19/08/19 - ETHZ - Fabio Benedetti © UP Group+ IBP+ ETH Zürich
##### Script to prepare the figures for the mansucript, as follows:
# - Figure 1: panel of contemporary annual diversity estimates (total plankton, phyto-, zooplankton)
# - Figure 2: anomalies of log(S) to eV for phyto- and zooplankton
# - Figure 3: heatmap of rank correlations (or slopes of lm) between diversity estimates (total, phyto-, zooplankton + anoms to eV)
# and selected predictors
# - Figure 4: panel of future diversity changes
### Last update: 30/10/19
# --------------------------------------------------------------------------------------------------------------------------------
library("tidyverse")
library("raster")
library("rgdal")
library("sp")
library("reshape2")
library("viridis")
library("scales")
library("maps")
library("vegan")
library("FactoMineR")
library("cmocean")
# Coastline
world2 <- map_data("world2")
WD <- getwd()
# --------------------------------------------------------------------------------------------------------------------------------
### 1°) Figure 1: panel of contemporary annual diversity estimates (total plankton, phyto-, zooplankton)
setwd(paste(WD,"/tables_composition","/", sep=""))
phyto.base <- read.table("table_phyto_annual_composition_baseline_14_08.txt", h = T, sep = "\t")
zoo.base <- read.table("table_zoo_annual_composition_baseline_14_08.txt", h = T, sep = "\t")
base <- cbind(phyto.base,zoo.base[,c(4:length(zoo.base))])
dim(base)
colnames(base) # phyto = 4:341 ; zoo = 342:length(base)
base$x2 <- base$x
base[base$x < 0 ,"x2"] <- (base[base$x < 0 ,"x"]) + 360
### Compute species richness for total plankton, phyto- and zooplankton
base$rich_plankton <- rowSums(as.matrix(base[,c(4:865)]))
base$rich_phyto <- rowSums(as.matrix(base[,c(4:341)]))
base$rich_zoo <- rowSums(as.matrix(base[,c(342:865)]))
summary(base)
setwd(WD)
### Bin all diversity indices
# 1) HSI_plankton
base$rich_plankton_bin <- factor(cut_interval(base$rich_plankton,10))
levels(base$rich_plankton_bin)
levels <- str_replace_all(levels(base$rich_plankton_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(base$rich_plankton_bin) <- levels
# 2) HSI_phyto
base$rich_phyto_bin <- factor(cut_interval(base$rich_phyto,10))
levels(base$rich_phyto_bin)
levels <- str_replace_all(levels(base$rich_phyto_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(base$rich_phyto_bin) <- levels
# 3) HSI_zoo
base$rich_zoo_bin <- factor(cut_interval(base$rich_zoo,10))
levels(base$rich_zoo_bin)
levels <- str_replace_all(levels(base$rich_zoo_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(base$rich_zoo_bin) <- levels
map1 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = factor(rich_plankton_bin)), data = base) +
scale_fill_viridis(name = "", na.value = "white", discrete = T) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
map2 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = factor(rich_phyto_bin)), data = base) +
scale_fill_viridis(name = "", na.value = "white", discrete = T) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
map3 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = factor(rich_zoo_bin)), data = base) +
scale_fill_viridis(name = "", na.value = "white", discrete = T) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
ggsave(plot = map1, filename = "map_Fig1A_plankton_rich_baseline.pdf", dpi = 300, width = 7, height = 4)
ggsave(plot = map2, filename = "map_Fig1C_phyto_rich_baseline.pdf", dpi = 300, width = 7, height = 4)
ggsave(plot = map3, filename = "map_Fig1E_zoo_rich_baseline.pdf", dpi = 300, width = 7, height = 4)
### Make zonal plots too
library("dplyr")
zonal <- data.frame(base %>%
group_by(y) %>%
summarise(mean_plankton = mean(rich_plankton,na.rm=T), sd_plankton = sd(rich_plankton,na.rm=T),
mean_phyto = mean(rich_phyto,na.rm=T), sd_phyto = sd(rich_phyto,na.rm=T),
mean_zoo = mean(rich_zoo,na.rm=T), sd_zoo = sd(rich_zoo,na.rm=T) )
) # eo ddf
summary(zonal)
plot1 <- ggplot() + geom_ribbon(aes(x = y, ymin = mean_plankton - sd_plankton, ymax = mean_plankton + sd_plankton),
fill = "grey70", data = zonal) +
geom_line(aes(x = y, y = mean_plankton), data = zonal, colour = "black" ) + scale_y_continuous(limits = c(0,300)) +
ylab("Mean annual species richness\n(Plankton)") + xlab("Latitude (°)") +
theme_classic() + coord_flip()
#
plot2 <- ggplot() + geom_ribbon(aes(x = y, ymin = mean_phyto - sd_phyto, ymax = mean_phyto + sd_phyto),
fill = "grey70", data = zonal) +
geom_line(aes(x = y, y = mean_phyto), data = zonal, colour = "black" ) + scale_y_continuous(limits = c(0,200)) +
ylab("Mean annual species richness\n(Phytoplankton)") + xlab("Latitude (°)") +
theme_classic() + coord_flip()
#
plot3 <- ggplot() + geom_ribbon(aes(x = y, ymin = mean_zoo - sd_zoo, ymax = mean_zoo + sd_zoo),
fill = "grey70", data = zonal) +
geom_line(aes(x = y, y = mean_zoo), data = zonal, colour = "black" ) + scale_y_continuous(limits = c(0,200)) +
ylab("Mean annual species richness\n(Zooplankton)") + xlab("Latitude (°)") +
theme_classic() + coord_flip()
#
ggsave(plot = plot1, filename = "plot_Fig1B_plankton_rich_baseline.pdf", dpi = 300, width = 3, height = 4)
ggsave(plot = plot2, filename = "plot_Fig1D_phyto_rich_baseline.pdf", dpi = 300, width = 3, height = 4)
ggsave(plot = plot3, filename = "plot_Fig1F_zoo_rich_baseline.pdf", dpi = 300, width = 3, height = 4)
# -----------------------------------------------------------
### 2°) Figure 2: panel of anomalies of log(S) to eV, for phyto- and zooplankton, with:
# - biplot with ln(S) ~ eV x2
# - map of anomaly of ln(S) to eV (lm)
# First, get annual clims of env predictors
setwd("/net/kryo/work/fabioben/OVERSEE/data/env_predictors/global_monthly_clims_1d/")
files <- dir()[grep("21_02_19",dir())]
clims <- lapply(files, function(f) {
d <- read.table(f, h = T, sep = ";")
return(d)
} # eo FUN
) # eo lapply
clims <- dplyr::bind_rows(clims)
# And use dplyr to compute annual climatologies
clims$x2 <- clims$x
clims[clims$x < 0 ,"x2"] <- (clims[clims$x < 0 ,"x"]) + 360
clims$id <- factor(paste(clims$x2, clims$y, sep = "_"))
aclim <- data.frame(clims %>% group_by(id) %>%
summarise(x = unique(x2), y = unique(y), Bathy = mean(Bathy,na.rm=T),
SST = mean(SST,na.rm=T), SSS = mean(SSS,na.rm=T), dSST = mean(deltaT,na.rm=T), Wind = mean(Wind,na.rm=T),
logEKE = mean(logEKE,na.rm=T), MLD = mean(MLD1,na.rm=T), PAR = mean(PAR,na.rm=T), O2 = mean(dO2,na.rm=T),
NO3 = mean(logNO3,na.rm=T), SiO2 = mean(logSiO2,na.rm=T), Nstar = mean(Nstar,na.rm=T), Sistar = mean(Sistar,na.rm=T),
Chla = mean(logChl,na.rm=T)
) # eo summarise
) # eo ddf
# Exclude non opean ocean cells: SSS > 30 and Bathy < -175
aclim <- aclim[aclim$SSS >= 20,]
aclim <- aclim[aclim$Bathy <= -175,]
# Check
aclim <- aclim[!is.na(aclim$id),]
aclim <- aclim[order(aclim$id),]
setwd(WD)
### Combine with 'base'
base$cell_id <- factor(paste(base$x2, base$y, sep = "_"))
base <- base[order(base$cell_id),]
head(base[,c(1:3)]);head(aclim[,c(1:3)])
# dim(base); dim(aclim)
# colnames(base)
ddf <- cbind(aclim, base[which(base$cell_id %in% unique(aclim$id)),c(867:869)])
# colnames(ddf)
# summary(ddf)
### Add K and compute eV
ddf$absT <- ddf$SST + 273.15
ddf$kT <- 1 / (ddf$absT*-8.6173303*10^(-5) )
### Compute div anomalies to SST based on a linear regression
lm <- lm(log(rich_phyto) ~ kT, data = ddf, na.action = na.exclude)
summary(lm) # Adjusted R-squared = 0.7738; p-value: < 2.2e-16
ddf$phyto_anom <- residuals(lm)
# And anoms between rich and SST
lm2 <- lm(rich_phyto ~ SST, data = ddf, na.action = na.exclude)
summary(lm2) # Adjusted R-squared = 0.664; p-value: < 2.2e-16
ddf$phyto_anom2 <- residuals(lm2)
lm <- lm(log(rich_zoo) ~ kT, data = ddf, na.action = na.exclude)
summary(lm) # Adjusted R-squared = 0.849; p-value: < 2.2e-16
ddf$zoo_anom <- residuals(lm)
# And anoms between rich and SST
lm2 <- lm(rich_zoo ~ SST, data = ddf, na.action = na.exclude)
summary(lm2) # Adjusted R-squared = 0.831; p-value: < 2.2e-16
ddf$zoo_anom2 <- residuals(lm2)
summary(ddf)
#
map4 <- ggplot() + geom_raster(aes(x = x, y = y, fill = phyto_anom), data = ddf[!is.na(ddf$phyto_anom),] ) +
scale_fill_gradient2(name = "Anomalies to\nlinear model",
low = "#3288bd", high = "#d53e4f", mid = "white", limits = c(-0.54,1.35)) +
geom_contour(colour = "grey60", binwidth = 0.25, size = 0.25, aes(x = x, y = y, z = phyto_anom),
data = ddf[!is.na(ddf$phyto_anom),] ) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
map5 <- ggplot() + geom_raster(aes(x = x, y = y, fill = zoo_anom), data = ddf[!is.na(ddf$zoo_anom),]) +
scale_fill_gradient2(name = "Anomalies to\nlinear model",
low = "#3288bd", high = "#d53e4f", mid = "white", limits = c(-0.54,1.35)) +
geom_contour(colour = "grey60", binwidth = 0.25, size = 0.25, aes(x = x, y = y, z = zoo_anom),
data = ddf[!is.na(ddf$zoo_anom),] ) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
setwd(WD)
ggsave(plot = map4, filename = "map_Fig2B_phyto_anoms.pdf", dpi = 300, width = 7, height = 4)
ggsave(plot = map5, filename = "map_Fig2D_zoo_anoms.pdf", dpi = 300, width = 7, height = 4)
### And biplots
summary(log(ddf$rich_phyto)) # 3-5.5
summary(log(ddf$rich_zoo)) # 3.5-5.5
plot4 <- ggplot() + geom_point(aes(x = kT, y = log(rich_phyto)), data = ddf, colour = "grey70") +
geom_smooth(aes(x = kT, y = log(rich_phyto)), data = ddf, colour = "black", method = "lm") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness\n(ln)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot5 <- ggplot() + geom_point(aes(x = kT, y = log(rich_zoo)), data = ddf, colour = "grey70") +
geom_smooth(aes(x = kT, y = log(rich_zoo)), data = ddf, colour = "black", method = "lm") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness\n(ln)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
ggsave(plot = plot4, filename = "plot_Fig2A_phyto_anoms.pdf", dpi = 300, width = 4.5, height = 4)
ggsave(plot = plot5, filename = "plot_Fig2C_zoo_anoms.pdf", dpi = 300, width = 4.5, height = 4)
### 27/08/2019: Also map and plot anomalies to SST
mp1 <- ggplot() + geom_raster(aes(x = x, y = y, fill = phyto_anom2), data = ddf[!is.na(ddf$phyto_anom2),] ) +
scale_fill_gradient2(name = "Anomalies to\nSST",
low = "#3288bd", high = "#d53e4f", mid = "white", limits = c(-56,162)) +
geom_contour(colour = "grey60", binwidth = 25, size = 0.25, aes(x = x, y = y, z = phyto_anom2),
data = ddf[!is.na(ddf$phyto_anom2),] ) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
mp2 <- ggplot() + geom_raster(aes(x = x, y = y, fill = zoo_anom2), data = ddf[!is.na(ddf$zoo_anom2),] ) +
scale_fill_gradient2(name = "Anomalies to\nSST",
low = "#3288bd", high = "#d53e4f", mid = "white", limits = c(-56,162)) +
geom_contour(colour = "grey60", binwidth = 25, size = 0.25, aes(x = x, y = y, z = zoo_anom2),
data = ddf[!is.na(ddf$zoo_anom2),] ) +
geom_polygon(aes(x = long, y = lat, group = group), data = world2, fill = "grey70", colour = "black", size = 0.3) +
coord_quickmap() + scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
#
setwd(WD)
ggsave(plot = mp1, filename = "map_Fig2B_phyto_anoms2.pdf", dpi = 300, width = 7, height = 4)
ggsave(plot = mp2, filename = "map_Fig2D_zoo_anoms2.pdf", dpi = 300, width = 7, height = 4)
### 05/09/19: Graphs of ln(S)~eV not convincing for phytoplankton (too different looking from Righetti et al. 2019):
### fit 2nd and 3r order polynomials and plot
### For phytoplankton
lm.2 <- lm(log(rich_phyto) ~ kT + I(kT^2), data = ddf, na.action = na.exclude)
lm.3 <- lm(log(rich_phyto) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
summary(lm.2) # Adjusted R-squared = 0.825; p-value: < 2.2e-16
summary(lm.3) # Adjusted R-squared = 0.8371; p-value: < 2.2e-16
ddf$phyto_anom_2d <- residuals(lm.2)
ddf$phyto_anom_3d <- residuals(lm.3)
ddf$phyto_2d <- predict(lm.2)
ddf$phyto_3d <- predict(lm.3)
### For zoo
lm.2 <- lm(log(rich_zoo) ~ kT + I(kT^2), data = ddf, na.action = na.exclude)
lm.3 <- lm(log(rich_zoo) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
summary(lm.2) # Adjusted R-squared = 0.849; p-value: < 2.2e-16
summary(lm.3) # Adjusted R-squared = 0.9705; p-value: < 2.2e-16
ddf$zoo_anom_2d <- residuals(lm.2)
ddf$zoo_anom_3d <- residuals(lm.3)
ddf$zoo_2d <- predict(lm.2)
ddf$zoo_3d <- predict(lm.3)
### Make plots
plot1 <- ggplot() + geom_point(aes(x = kT, y = log(rich_phyto)), data = ddf, colour = "grey70") +
geom_smooth(aes(x = kT, y = log(rich_phyto)), data = ddf, colour = "black", method = "loess") +
geom_line(aes(x = kT, y = phyto_2d), data = ddf, colour = "#b2df8a") +
geom_line(aes(x = kT, y = phyto_3d), data = ddf, colour = "#33a02c") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness\n(log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot2 <- ggplot() + geom_point(aes(x = kT, y = log(rich_zoo)), data = ddf, colour = "grey70") +
geom_smooth(aes(x = kT, y = log(rich_zoo)), data = ddf, colour = "black", method = "loess") +
geom_line(aes(x = kT, y = zoo_2d), data = ddf, colour = "#a6cee3") +
geom_line(aes(x = kT, y = zoo_3d), data = ddf, colour = "#1f78b4") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness\n(log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
ggsave(plot = plot1, filename = "plot_phyto_anoms_withloess.pdf", dpi = 300, width = 6, height = 7)
ggsave(plot = plot2, filename = "plot_zoo_anoms_withloess.pdf", dpi = 300, width = 6, height = 7)
### Summarize slopes across the regimes defined by Righetti et al. 2019 :
# Above 23°C: 0.2939 (0.26 above 24°C)
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST >= 23,]))
# Above 19°C: 0.37812
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST >= 19,]))
# 19°C-11°C: 0.3886
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST < 19 & ddf$SST > 11,]))
# Below 11°C: 0.0054
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= 11,]))
# Below 9°C: -0.027
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= 9,]))
# Below 5°C: -0.127
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= 5,]))
# Below 7°C: -0.0599
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= 7,]))
### Same with zoo
# Above 23°C: -0.2654 ! (-0.3412 above 24°C)
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST >= 23,]))
# Above 19°C: -0.0810
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST >= 19,]))
# 19°C-11°C: 0.7569
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 19 & ddf$SST > 11,]))
# 17°C-11°C: 0.6877
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 17 & ddf$SST > 11,]))
# Below 11°C: 0.0222
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 11,]))
# Below 9°C: -0.0785
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 9,]))
# Below 7°C: -0.1775
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 7,]))
# Below 5°C: -0.3172
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 5,]))
### For phytoplankton, display the slopes from lm objects of varying SST thresholds so you identify the point where it goes below 0.32
for(t in seq(from = -1, to = 30, by = 1)) {
lm <- lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= t,])
message(paste("Below t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Below t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
# Same with zoo
for(t in seq(from = -1, to = 30, by = 1)) {
lm <- lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= t,])
message(paste("Below t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Below t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
summary(lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST <= 9,]))
summary(lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST <= 9,]))
### And when above the threshold t
for(t in seq(from = -1, to = 30, by = 1)) {
lm <- lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST >= t,])
message(paste("Above t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Above t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
# Same with zoo
for(t in seq(from = -1, to = 30, by = 1)) {
lm <- lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST >= t,])
message(paste("Above t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Above t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
### And from 11 to an upper threshold t
for(t in seq(from = 12, to = 30, by = 1)) {
lm <- lm(log(rich_phyto) ~ kT, data = ddf[ddf$SST < t & ddf$SST > 11,] )
message(paste("Above t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Above t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
# Same with zoo
for(t in seq(from = 12, to = 30, by = 0.5)) {
lm <- lm(log(rich_zoo) ~ kT, data = ddf[ddf$SST < t & ddf$SST > 11,] )
message(paste("Above t = ",t,"°C || Slope to kT = ", round(lm$coefficients[2],4), sep = ""))
message(paste("Above t = ",t,"°C || r2 to kT = ", round(summary(lm)$adj.r.squared,4), sep = ""))
message(paste("", sep = ""))
}
### Niki & Meike want to see the heatmap of points density per bins of both kT and log(S) illustrated with a heatmap
lm3.phyto <- lm(log(rich_phyto) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
lm3.zoo <- lm(log(rich_zoo) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
ddf$phyto_3d <- predict(lm3.phyto)
ddf$zoo_3d <- predict(lm3.zoo)
ddf$kT_bin <- factor(cut_interval(ddf$kT,15))
levels(ddf$kT_bin)
levels <- str_replace_all(levels(ddf$kT_bin), ",", ":")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$kT_bin) <- levels
ddf$logphyto_bin <- factor(cut_interval(log(ddf$rich_phyto),10))
levels(ddf$logphyto_bin)
levels <- str_replace_all(levels(ddf$logphyto_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$logphyto_bin) <- levels
ddf$logzoo_bin <- factor(cut_interval(log(ddf$rich_zoo),15))
levels(ddf$logzoo_bin)
levels <- str_replace_all(levels(ddf$logzoo_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$logzoo_bin) <- levels
# Use dplyr to count n obs per binxbin
require("viridis")
dens <- ddf %>% group_by(kT_bin,logphyto_bin) %>% summarise(n = n())
summary(data.frame(dens))
# Plot heatmap with geom_raster
heat1 <- ggplot() + geom_tile(aes(x = factor(kT_bin), y = factor(logphyto_bin), fill = log(n)), data = na.omit(dens)) +
scale_fill_viridis(name = "Density\n(log)", option = "A") + xlab("Thermal energy (1/kT)") +
ylab("Phytoplankton richness\n(log)") + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
#
ggsave(plot = heat1, filename = "heatmap_dens_richxkT_phyto.pdf", width = 9, height = 4, dpi = 300)
###
dens <- ddf %>% group_by(kT_bin,logzoo_bin) %>% summarise(n = n())
summary(data.frame(dens))
# Plot heatmap with geom_raster
heat2 <- ggplot() + geom_tile(aes(x = factor(kT_bin), y = factor(logzoo_bin), fill = log(n)), data = na.omit(dens)) +
scale_fill_viridis(name = "Density\n(log)", option = "A") + xlab("Thermal energy (1/kT)") +
ylab("Zooplankton richness\n(log)") + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
#
ggsave(plot = heat2, filename = "heatmap_dens_richxkT_zoo.pdf", width = 9, height = 4, dpi = 300)
### UPDATE 24/09/19: need finer bins and overlay fit
lm3.phyto <- lm(log(rich_phyto) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
lm3.zoo <- lm(log(rich_zoo) ~ kT + I(kT^2) + I(kT^3), data = ddf, na.action = na.exclude)
ddf$phyto_3d <- predict(lm3.phyto)
ddf$zoo_3d <- predict(lm3.zoo)
ddf$kT_bin <- factor(cut_interval(ddf$kT,40))
levels(ddf$kT_bin)
levels <- str_replace_all(levels(ddf$kT_bin), ",", ":")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$kT_bin) <- levels
ddf$logphyto_bin <- factor(cut_interval(log(ddf$rich_phyto),20))
levels(ddf$logphyto_bin)
levels <- str_replace_all(levels(ddf$logphyto_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$logphyto_bin) <- levels
ddf$logzoo_bin <- factor(cut_interval(log(ddf$rich_zoo),20))
levels(ddf$logzoo_bin)
levels <- str_replace_all(levels(ddf$logzoo_bin), ",", "-")
levels <- gsub("\\[|\\]", "", levels)
levels <- gsub("\\(|\\)", "", levels)
levels(ddf$logzoo_bin) <- levels
### Try adding a stat_density_2d(aes(fill = stat(level)), geom = "polygon")
# plot1 <- <- ggplot() + stat_density_2d(aes(x = kT, y = phyto_3d), data = ddf, colour = "grey65") +
# geom_line(aes(x = kT, y = phyto_3d), data = ddf, colour = "black") +
# scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness\n(log)") +
# xlab("Thermal energy (1/kT)") + theme_classic()
# #
# plot2 <- ggplot() + stat_density_2d(aes(x = kT, y = zoo_3d), data = ddf, colour = "grey65") +
# geom_line(aes(x = kT, y = zoo_3d), data = ddf, colour = "black") +
# scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness\n(log)") +
# xlab("Thermal energy (1/kT)") + theme_classic()
#
ggsave(plot = plot1, filename = "plot_phyto_fit.pdf", dpi = 300, width = 4, height = 4)
ggsave(plot = plot2, filename = "plot_zoo_fit.pdf", dpi = 300, width = 4, height = 4)
# stat_density_2d(aes(x=eruptions, y=waiting, color=geyser_types, fill=geyser_types), geom="polygon", bins=4, alpha=.1) +
### With polygon for density
plot1 <- ggplot() + stat_density_2d(data = na.omit(ddf), aes(x = kT, y = log(rich_phyto) ),
fill = "#66c2a5", fill = "#66c2a5", geom = "polygon", bins = 6, alpha = .3) +
geom_line(aes(x = kT, y = phyto_3d), data = na.omit(ddf), colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness\n(log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot2 <- ggplot() + stat_density_2d(data = na.omit(ddf), aes(x = kT, y = log(rich_zoo) ),
fill = "#d53e4f", fill = "#d53e4f", geom = "polygon", bins = 6, alpha = .3) +
geom_line(aes(x = kT, y = zoo_3d), data = na.omit(ddf), colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness\n(log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
ggsave(plot = plot1, filename = "plot_phyto_dens_polygon_fit.pdf", dpi = 300, width = 5, height = 4)
ggsave(plot = plot2, filename = "plot_zoo_dens_polygon_fit.pdf", dpi = 300, width = 5, height = 4)
### And by adding geom_point below stat_density_2d ?
plot1 <- ggplot() + geom_point(data = na.omit(ddf), aes(x = kT, y = log(rich_phyto) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = na.omit(ddf), aes(x = kT, y = log(rich_phyto) ),
colour = "#3288bd", fill = "#3288bd", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = phyto_3d), data = na.omit(ddf), colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot2 <- ggplot() + geom_point(data = na.omit(ddf), aes(x = kT, y = log(rich_zoo) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = na.omit(ddf), aes(x = kT, y = log(rich_zoo) ),
colour = "#d53e4f", fill = "#d53e4f", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = zoo_3d), data = na.omit(ddf), colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
ggsave(plot = plot1, filename = "plot_phyto_dens_points_polygon_fit.pdf", dpi = 300, width = 5, height = 5)
ggsave(plot = plot2, filename = "plot_zoo_dens_points_polygon_fit.pdf", dpi = 300, width = 5, height = 5)
### Values returned by stat_density_2d ?? # https://stackoverflow.com/questions/36157437/r-extract-coordinates-plotted-by-ggplot2stat-density-2d
# head( ggplot_build(plot1)$data[[2]] )
summary( ggplot_build(plot1)$data[[2]] ) # phyto
dim( ggplot_build(plot1)$data[[2]] )
summary( ggplot_build(plot2)$data[[2]] ) # zoo
str(ggplot_build(plot1)$data)
summary( ggplot_build(plot1)$data[[3]] )
head(ggplot_build(plot1)$data[[3]])
### NOTE: levels are computed using MASS::kde2d()
# ?kde2d
# https://en.wikipedia.org/wiki/Multivariate_kernel_density_estimation
# https://stackoverflow.com/questions/32206623/what-does-level-mean-in-ggplotstat-density2d
### And add a dahsed dline representing the expected slopes and intercepts
# To find the intercept = https://stackoverflow.com/questions/33292969/linear-regression-with-specified-slope
ddf2 <- na.omit(ddf)
slopeP <- 0.32
interP <- mean(log(ddf2$rich_phyto) - slopeP * ddf2$kT )
slopeZ <- 0.65
interZ <- mean(log(ddf2$rich_zoo) - slopeZ * ddf2$kT )
# Derive expected model
ddf2$phyto.mte <- slopeP*(ddf2$kT) + interP
ddf2$zoo.mte <- slopeZ*(ddf2$kT) + interZ
summary(ddf2)
### Compare R2 of MTE models to 3rd polynomial fit's
summary(lm(log(ddf2$rich_phyto) ~ ddf2$phyto.mte, data = ddf2)) # R-squared: 0.7843
summary(lm(log(ddf2$rich_zoo) ~ ddf2$zoo.mte, data = ddf2)) # R-squared: 0.853
summary(lm3.phyto) # 0.8371
summary(lm3.zoo) # 0.9705
### And by adding geom_point below stat_density_2d ?
plot1 <- ggplot() + geom_point(data = ddf2, aes(x = kT, y = log(rich_phyto) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = ddf2, aes(x = kT, y = log(rich_phyto) ),
colour = "#3288bd", fill = "#3288bd", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = phyto.mte), data = ddf2, colour = "black", linetype = "dashed") +
geom_line(aes(x = kT, y = phyto_3d), data = ddf2, colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot2 <- ggplot() + geom_point(data = ddf2, aes(x = kT, y = log(rich_zoo) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = ddf2, aes(x = kT, y = log(rich_zoo) ),
colour = "#d53e4f", fill = "#d53e4f", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = zoo.mte), data = ddf2, colour = "black", linetype = "dashed") +
geom_line(aes(x = kT, y = zoo_3d), data = ddf2, colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
ggsave(plot = plot1, filename = "plot_phyto_dens_points_polygon_fit+expectedMTE.pdf", dpi = 300, width = 5, height = 5)
ggsave(plot = plot2, filename = "plot_zoo_dens_points_polygon_fit+expectedMTE.pdf", dpi = 300, width = 5, height = 5)
### ANd yet another possibility is to collide histograms next to the dotplot (using ggExtra)
require("ggExtra")
### add histgrams
plot1 <- ggplot() + geom_point(data = ddf2, aes(x = kT, y = log(rich_phyto) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = ddf2, aes(x = kT, y = log(rich_phyto) ),
colour = "#3288bd", fill = "#3288bd", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = phyto.mte), data = ddf2, colour = "black", linetype = "dashed") +
geom_line(aes(x = kT, y = phyto_3d), data = ddf2, colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Phytoplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
#
plot2 <- ggplot() + geom_point(data = ddf2, aes(x = kT, y = log(rich_zoo) ), colour = "grey75", size = 1, alpha = .2) +
stat_density_2d(data = ddf2, aes(x = kT, y = log(rich_zoo) ),
colour = "#d53e4f", fill = "#d53e4f", geom = "polygon", bins = 6, alpha = .2) +
geom_line(aes(x = kT, y = zoo.mte), data = ddf2, colour = "black", linetype = "dashed") +
geom_line(aes(x = kT, y = zoo_3d), data = ddf2, colour = "black") +
scale_y_continuous(limits = c(3,5.5)) + ylab("Zooplankton species richness (log)") +
xlab("Thermal energy (1/kT)") + theme_classic()
p1 <- ggExtra::ggMarginal(plot1, type = "histogram", data = ddf2, x = kT, y = log(rich_phyto), colour = "black", fill = "white")
p2 <- ggExtra::ggMarginal(plot2, type = "histogram",data = ddf2, x = kT, y = log(rich_zoo), colour = "black", fill = "white")
setwd(WD)
ggsave(plot = p1, filename = "plot_phyto_dens_points_polygon_fit+expectedMTE+hist.pdf", dpi = 300, width = 7, height = 7)
ggsave(plot = p2, filename = "plot_zoo_dens_points_polygon_fit+expectedMTE+hist.pdf", dpi = 300, width = 7, height = 7)
### But missing the scales on the density histograms...add them manually
p1 <- ggplot(ddf2, aes(x=kT)) + geom_histogram(colour = "black", fill = "white") +
xlab("Thermal energy (1/kT)") + ylab("Density") + scale_x_continuous(limits = c(-42.77,-38.33)) +
theme_classic()
p2 <- ggplot(ddf2, aes(x=log(rich_phyto))) + geom_histogram(colour = "black", fill = "white") +
xlab("Phytoplankton species richness (log)") + ylab("Density") + scale_x_continuous(limits = c(3,5.5)) +
theme_classic() + coord_flip()
p3 <- ggplot(ddf2, aes(x=log(rich_zoo))) + geom_histogram(colour = "black", fill = "white") +
xlab("Zooplankton species richness (log)") + ylab("Density") + scale_x_continuous(limits = c(3,5.5)) +
theme_classic() + coord_flip()
ggsave(plot = p1, filename = "plot_dens_points_kT.pdf", dpi = 300, width = 7, height = 3)
ggsave(plot = p2, filename = "plot_dens_points_logphyto.pdf", dpi = 300, width = 3, height = 7)
ggsave(plot = p3, filename = "plot_dens_points_logzoo.pdf", dpi = 300, width = 3, height = 7)
# -----------------------------------------------------------
### 3°) Figure 3: Heatmap of rank correlation and/or linear slopes between the 5 diversity metrics and the env predictors
### A°) Spearman's rank correlations
library("corrplot")
colnames(ddf)
# Compute p-val matrix
p.mat <- cor.mtest(na.omit(ddf[,c(5:17,22,18,19,20,23:26)]))$p
# Correlation matrix
M <- cor(na.omit(ddf[,c(5:17,22,18,19,20,23:26)]), method = "spearman")
head(M)
# Provide names to p.mat's dimensions
rownames(p.mat) <- rownames(M)
colnames(p.mat) <- colnames(M)
head(p.mat)
# Need to melt M so you have 3 columns: env variables, diversity indices and corr values
m <- reshape2::melt(data = M, value.name = "cor", id.vars = colnames(ddf)[c(5:17,22)] )
# remove the line where Var2 %in% colnames(aclim)[6:17]
m <- m[!(m$Var2 %in% colnames(ddf)[c(5:17,22)]),]
m <- m[(m$Var1 %in% colnames(ddf)[c(5:17,22)]),]
colnames(m) <- c("env","div","corr")
# create a new variable from incidence
m$corrfactor <- cut(m$corr,
breaks = c(-1,-0.75,-0.5,-0.25,-0.10,-0.01,0.01,0.1,0.25,0.5,0.75,1),
labels = c("-1.0","-0.75","-0.50","-0.25","-0.1","0","0.1","0.25","0.50","0.75","1.0")
)
m$corrfactor <- factor(as.character(m$corrfactor),levels=rev(levels(m$corrfactor)))
# Change the levels of the factors according to what you want to display on the plot
unique(m$env)
unique(m$div)
# For re-naming
levels(m$env)[levels(m$env) == "logEKE"] <- "EKE"
levels(m$env)[levels(m$env) == "NO3"] <- "Nitrates"
levels(m$env)[levels(m$env) == "SiO2"] <- "Silicates"
levels(m$env)[levels(m$env) == "Nstar"] <- "N*"
levels(m$env)[levels(m$env) == "Sistar"] <- "Si*"
levels(m$env)[levels(m$env) == "Chla"] <- "Chlorophyll"
levels(m$env)[levels(m$env) == "kT"] <- "kT"
levels(m$div)[levels(m$div) == "rich_plankton"] <- "Plankton richness"
levels(m$div)[levels(m$div) == "rich_phyto"] <- "Phytoplankton richness"
levels(m$div)[levels(m$div) == "rich_zoo"] <- "Zooplankton richness"
levels(m$div)[levels(m$div) == "phyto_anom"] <- "Phytoplankton anomalies (log)"
levels(m$div)[levels(m$div) == "zoo_anom"] <- "Zooplankton anomalies (log)"
levels(m$div)[levels(m$div) == "phyto_anom2"] <- "Phytoplankton anomalies"
levels(m$div)[levels(m$div) == "zoo_anom2"] <- "Zooplankton anomalies"
### Do the same for p.mat
p <- melt(data = p.mat, value.name = "pval", id.vars = colnames(ddf)[c(5:17,22)] )
head(p)
# remove the line where Var2 %in% colnames(aclim)[6:17]
p <- p[!(p$Var2 %in% colnames(ddf)[c(5:17,22)]),]
p <- p[(p$Var1 %in% colnames(ddf)[c(5:17,22)]),]
colnames(p) <- c("env","div","pval")
# Change the levels of the factors according to what you want to display on the plot
unique(p$env)
unique(p$div)
### Change p$pval levels to "*" etc.
summary(p$pval)
p[p$pval > 0.01,]
# 318 dSST rich_phyto 0.0106
# 428 O2 zoo_anom2 0.1251
#define a colour for fonts
textcol <- "grey30"
# Diverging color palette
#levels(m$corrfactor) ; #unique(m$corrfactor) 11 levels but only 10 are realized (no 0)
#values <- c("#d73027","#f46d43","#fdae61","#fee090","#ffffbf","#e0f3f8","#abd9e9","#74add1","#4575b4","#313695")
#quartz()
h1 <- ggplot(m,aes(x = env, y = div, fill = corr)) + geom_tile() +
geom_tile(colour = "white", size = 0.25, show_guide = F) +
scale_fill_gradient2("Rank correlation", low = "#3288bd", high = "#d53e4f", mid = "white", limits = c(-1,1)) +
labs(x = "",y = "",title = "Rank correlations") +
scale_y_discrete(expand=c(0,0)) + scale_x_discrete(expand = c(0,0)) +
geom_vline(aes(xintercept = 36), size = 3.4, alpha = 0.24) + coord_fixed() +
theme_grey(base_size = 10) + geom_text(label = round(m$corr,3) ) +
theme(
legend.title=element_blank(),
#remove legend margin
legend.margin = grid::unit(0,"cm"),
#change legend text properties
legend.text=element_text(colour=textcol,size=7,face="bold"),
#change legend key height
legend.key.height=grid::unit(0.8,"cm"),
#set a slim legend
legend.key.width=grid::unit(0.2,"cm"),
#set x axis text size and colour
axis.text.x=element_text(size=10,colour=textcol),
#set y axis text colour and adjust vertical justification
axis.text.y=element_text(vjust = 0.2,colour=textcol),
#change axis ticks thickness
axis.ticks=element_line(size=0.4),
#change title font, size, colour and justification
plot.title=element_text(colour=textcol,hjust=0,size=14,face="bold"),
#remove plot background
plot.background=element_blank(),
#remove plot border
panel.border=element_blank()
)
setwd(WD)
ggsave(plot = h1, filename = "heat_Fig.3_rank_corrv2.pdf", dpi = 300, width = 12, height = 6)
### B°) Slopes
# https://stackoverflow.com/questions/51953709/fast-pairwise-simple-linear-regression-between-variables-in-a-data-frame
# pairwise_simple_lm <- function(dat) {
#
# # matrix and its dimension (n: numbeta.ser of data; p: numbeta.ser of variables)
# dat <- as.matrix(dat)
# n <- nrow(dat)
# p <- ncol(dat)
# # variable summary: mean, (unscaled) covariance and (unscaled) variance
# m <- colMeans(dat)
# V <- crossprod(dat) - tcrossprod(m * sqrt(n))
# d <- diag(V)
# # R-squared (explained variance) and its complement
# R2 <- (V ^ 2) * tcrossprod(1 / d)
# R2_complement <- 1 - R2
# R2_complement[seq.int(from = 1, by = p + 1, length = p)] <- 0
# # slope and intercept
# beta <- V * rep(1 / d, each = p) # slope
# alpha <- m - beta * rep(m, each = p) # intercept
# # residual sum of squares and standard error
# RSS <- R2_complement * d
# sig <- sqrt(RSS * (1 / (n - 2)))
# # statistics for slope
# beta.se <- sig * rep(1 / sqrt(d), each = p)
# beta.tv <- beta / beta.se
# beta.pv <- 2 * pt(abs(beta.tv), n - 2, lower.tail = FALSE)
# # F-statistic and p-value
# Ff.fv <- (n - 2) * R2 / R2_complement
# Ff.pv <- pf(Ff.fv, 1, n - 2, lower.tail = FALSE)
# # export
# data.frame(LHS = rep(colnames(dat), times = p),
# RHS = rep(colnames(dat), each = p),
# alpha = c(alpha),
# beta = c(beta),
# beta.se = c(beta.se),
# beta.tv = c(beta.tv),
# beta.pv = c(beta.pv),
# sig = c(sig),
# R2 = c(R2),
# Ff.fv = c(Ff.fv),
# Ff.pv = c(Ff.pv),
# stringsAsFactors = FALSE
# ) # eo ddf
#
# } # eo FUN
#
# lm.res <- pairwise_simple_lm(dat = na.omit(ddf[,c(5:17,22,18,19,20,23,24)]))
# head(lm.res)
# summary(lm.res)
# str(lm.res)
# #
# lm.res <- lm.res[which(lm.res$LHS %in% colnames(ddf)[c(5:17,22)]),]
# lm.res <- lm.res[which(lm.res$RHS %in% c("rich_plankton","rich_phyto","rich_zoo","phyto_anom","zoo_anom")),]
# # OK
# summary(lm.res)
#
# ### Colour should be beta (slope, both positive and negative) and the label should be the R2
# # lm.res$beta
# # round(lm.res$R2,2)
# # For re-naming
# lm.res$RHS <- factor(lm.res$RHS)
# lm.res$LHS <- factor(lm.res$LHS)
# levels(lm.res$LHS)
# levels(lm.res$RHS)
# # Change levels
# levels(lm.res$LHS)[levels(lm.res$LHS) == "logEKE"] <- "EKE"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "NO3"] <- "Nitrates"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "SiO2"] <- "Silicates"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "Nstar"] <- "N*"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "Sistar"] <- "Si*"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "Chla"] <- "Chlorophyll"
# levels(lm.res$LHS)[levels(lm.res$LHS) == "kT"] <- "kT"
# levels(lm.res$RHS)[levels(lm.res$RHS) == "rich_plankton"] <- "Plankton richness"
# levels(lm.res$RHS)[levels(lm.res$RHS) == "rich_phyto"] <- "Phytoplankton richness"
# levels(lm.res$RHS)[levels(lm.res$RHS) == "rich_zoo"] <- "Zooplankton richness"
# levels(lm.res$RHS)[levels(lm.res$RHS) == "phyto_anom"] <- "Phytoplankton anomalies"
# levels(lm.res$RHS)[levels(lm.res$RHS) == "zoo_anom"] <- "Zooplankton anomalies"
#
#
# h2 <- ggplot(lm.res, aes(x=LHS,y=RHS,fill=beta) ) + geom_tile() + geom_tile(colour="white",size=0.25, show_guide=FALSE) +
# labs(x="",y="",title="Adjusted R2 and slope of linear models") +
# scale_y_discrete(expand=c(0,0)) + scale_x_discrete(expand=c(0,0)) +
# scale_fill_gradient2(name = "Slope", low = "#3288bd", high = "#d53e4f", mid = "white") +
# geom_vline(aes(xintercept = 36),size=3.4,alpha=0.24) + coord_fixed() +
# theme_grey(base_size=10) + geom_text(label = round(lm.res$R2,3) ) +
# theme(
# legend.title=element_blank(),
# legend.margin = grid::unit(0,"cm"),
# legend.text=element_text(colour=textcol,size=7,face="bold"),
# legend.key.height=grid::unit(0.8,"cm"),
# legend.key.width=grid::unit(0.2,"cm"),
# axis.text.x=element_text(size=10,colour=textcol),
# axis.text.y=element_text(vjust = 0.2,colour=textcol),
# axis.ticks=element_line(size=0.4),
# plot.title=element_text(colour=textcol,hjust=0,size=14,face="bold"),
# plot.background=element_blank(),
# panel.border=element_blank()
# )
#
# setwd(WD)
# ggsave(plot = h2, filename = "heat_Fig.3_lm.pdf", dpi = 300, width = 12, height = 6)
### 27/08/19: Make bivariate plots to assess global PDR
plot <- ggplot() + geom_point(aes(x = Chla, y = rich_phyto, colour = abs(y)), data = ddf) +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Phytoplankton species richness") +
scale_colour_viridis(name = "Latitude", option = "A") +
theme_classic()
ggsave(plot = plot, filename = "plot_richxchla_phyto_HSI.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = Chla, y = rich_zoo, colour = abs(y)), data = ddf) +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Zooplankton species richness") +
scale_colour_viridis(name = "Latitude", option = "A") +
theme_classic()
ggsave(plot = plot, filename = "plot_richxchla_zoo_HSI.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = rich_phyto, y = rich_zoo, colour = Chla, size = abs(y)), data = ddf, alpha = 0.5) +
scale_colour_viridis(name = "Phytoplankton biomass\nlog(mgC.m3)") +
xlab("Phytoplankton species richness") + ylab("Zooplankton species richness") +
theme_classic()
ggsave(plot = plot, filename = "plot_phytoxzooxchla_rich_HSI.pdf", dpi = 300, width = 8, height = 6)
### 27/08/19: And with a fit
#summary(lm(rich_phyto ~ Chla, data = ddf)) # -15.60; 0.036
#summary(lm(rich_zoo ~ Chla, data = ddf)) # -90.38; 0.383
#summary(lm(phyto_anom ~ Chla, data = ddf)) # 0.287; 0.209
#summary(lm(zoo_anom ~ Chla, data = ddf)) # -0.079; 0.017
plot <- ggplot() + geom_point(aes(x = Chla, y = rich_phyto, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = Chla, y = rich_phyto, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Phytoplankton species richness") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(20,235)) + theme_classic()
ggsave(plot = plot, filename = "plot_richxchla_phyto_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = Chla, y = rich_zoo, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = Chla, y = rich_zoo, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Zooplankton species richness") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(20,235)) + theme_classic()
ggsave(plot = plot, filename = "plot_richxchla_zoo_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
plot <- ggplot() + geom_point(aes(x = Chla, y = phyto_anom, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = Chla, y = phyto_anom, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Phytoplankton richness anomalies") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(-0.54,1.35)) + geom_hline(yintercept = 0, linetype = "dashed") +
theme_classic()
ggsave(plot = plot, filename = "plot_anomsxchla_phyto_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = Chla, y = zoo_anom, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = Chla, y = zoo_anom, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Zooplankton richness anomalies") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(-0.54,1.35)) + geom_hline(yintercept = 0, linetype = "dashed") +
theme_classic()
ggsave(plot = plot, filename = "plot_anomsxchla_zoo_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
### 27/08/19: And with Nutrients availability?
summary(lm(rich_phyto ~ NO3, data = ddf)) # -15.263; 0.382
summary(lm(rich_zoo ~ NO3, data = ddf)) # -38.137; 0.769
summary(lm(phyto_anom ~ NO3, data = ddf)) # 0.0137; 0.0052
summary(lm(zoo_anom ~ NO3, data = ddf)) # -0.0477; 0.0718
summary(lm(rich_phyto ~ SiO2, data = ddf)) # -12.9874; 0.198
summary(lm(rich_zoo ~ SiO2, data = ddf)) # -37.266; 0.526
summary(lm(phyto_anom ~ SiO2, data = ddf)) # 0.080227; 0.1296
summary(lm(zoo_anom ~ SiO2, data = ddf)) # -0.009277; 0.00191
plot <- ggplot() + geom_point(aes(x = NO3, y = rich_phyto, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = NO3, y = rich_phyto, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Nitrates concentration - log(µg/L)") + ylab("Phytoplankton species richness") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(20,235)) + theme_classic()
ggsave(plot = plot, filename = "plot_richxno3_phyto_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = NO3, y = rich_zoo, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = NO3, y = rich_zoo, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Nitrates concentration - log(µg/L)") + ylab("Zooplankton species richness") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(20,235)) + theme_classic()
ggsave(plot = plot, filename = "plot_richxno3_zoo_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
plot <- ggplot() + geom_point(aes(x = NO3, y = phyto_anom, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = NO3, y = phyto_anom, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Nitrates concentration - log(µg/L)") + ylab("Phytoplankton richness anomalies") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(-0.54,1.35)) + geom_hline(yintercept = 0, linetype = "dashed") +
theme_classic()
ggsave(plot = plot, filename = "plot_anomsxno3_phyto_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = NO3, y = zoo_anom, colour = abs(y)), data = ddf, alpha = 0.6) +
geom_smooth(aes(x = NO3, y = zoo_anom, colour = abs(y)), data = ddf, method = "lm", colour = "black") +
xlab("Nitrates concentration - log(µg/L)") + ylab("Zooplankton richness anomalies") +
scale_colour_distiller(name = "Latitude", palette = "RdYlBu", direction = 1) +
scale_y_continuous(limits = c(-0.54,1.35)) + geom_hline(yintercept = 0, linetype = "dashed") +
theme_classic()
ggsave(plot = plot, filename = "plot_anomsxno3_zoo_HSI_fit.pdf", dpi = 300, width = 6, height = 4)
# Anomalies between S and SST
plot <- ggplot() + geom_point(aes(x = Chla, y = phyto_anom2, colour = abs(y)), data = ddf) +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Phytoplankton richness anomalies") +
scale_colour_viridis(name = "Latitude", option = "A") +
theme_classic()
ggsave(plot = plot, filename = "plot_anoms2xchla_phyto.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = Chla, y = zoo_anom2, colour = abs(y)), data = ddf) +
xlab("Phytoplankton biomass - log(mgC.m3)") + ylab("Zooplankton richness anomalies") +
scale_colour_viridis(name = "Latitude", option = "A") +
theme_classic()
ggsave(plot = plot, filename = "plot_anoms2xchla_zoo.pdf", dpi = 300, width = 6, height = 4)
#
plot <- ggplot() + geom_point(aes(x = phyto_anom2, y = zoo_anom2, colour = Chla, size = abs(y)), data = ddf, alpha = 0.5) +
scale_colour_viridis(name = "Phytoplankton biomass\nlog(mgC.m3)") +
xlab("Phytoplankton richness anomalies") + ylab("Zooplankton richness anomalies") +
theme_classic()
ggsave(plot = plot, filename = "plot_phytoxzooxchla_anoms2.pdf", dpi = 300, width = 8, height = 6)
### 20/08/19: Find the best looking bivariate plots to hilight PDRs
# After exploring various possibilities, I think we need to do so per bioregions
# Perform PCA or CA
library("FactoMineR")
colnames(base)
pca <- PCA(X = base[,c(4:865)], graph = F, ncp = 5, scale.unit = F)
# Use cell's coords along CA1:CA4 to compute euclidean distances and cluster
summary(pca)
# Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
# Variance 16.711 3.254 1.973 1.404 1.064