-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.R
More file actions
1867 lines (1442 loc) · 96.4 KB
/
Copy pathcode.R
File metadata and controls
1867 lines (1442 loc) · 96.4 KB
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
# Code to accompany paper: Temperate functional niche availability not resident-invader competition shapes tropicalisation in reef fishes
# Paper authors: Mark Miller, James Reimer, Katie Cook, John M. Pandolfi, Brigitte Sommer, Masami Obuchi, Maria Beger
# Code author: Mark Miller mark.gr.miller@gmail.com
# For additional code supporting paper data preparation and pre-analyses see: https://github.com/lark-gorilla/coral_fish
#
# data (description) included to run code:
# table1_analysis.csv (Summary results from paper Table 1)
# thermal_midpoint_means.csv (average thermal midpoint of tropical species per FG, derived from Stuart Smith et al. 2018)
# JPN_AUS_RMI_CHK_MLD_TMR_trait_master_opt2_clusters.csv (species trait data clustered into functional groups)
# sp_list_summer_only.csv (Summer only Australian species to filter out from trait data)
# Jpn_transects_biomass.csv (transect-level fish biomass data from Japan)
# Aus_transects_biomass.csv (transect-level fish biomass data from Australia)
# Aus_site_species_biomass.csv (site-level fish biomass data from Japan)
# Jpn_site_species_biomass.csv (site-level fish biomass data from Australia)
#rm(list=ls())
#Packages and versions
library(dplyr) #(1.0.6)
library(ggplot2) #(3.3.3 )
library(gridExtra) #(2.3)
library(mice) #(3.13.0)
library(scales) #(1.1.1)
library(ggwordcloud) #(0.5.0) # make sure package dependency "Rccp" is installed and updated
library(vegan) #(2.5-7)
library(ade4) #(1.7-17)
library(emmeans) #(1.6.2-1)
library(ggResidpanel) #(0.3.0)
library(adehabitatHR) #(0.4.19)
library(sf) #(0.9-8)
library(rgeos) #(0.5-5)
library(cluster) #(2.1.2)
library(patchwork) #(1.1.1)
# set working directory
setwd('C:/tropicalisation_paper/NCOMMS-21-26138-T_code_data')
################ read and prepare data ###################
##########################################################
# read in species trait data, with species assigned to 1 of 19 clusters (functional groups;FGs)
dat<-read.csv('JPN_AUS_master_trait.csv', h=T)
dat$Diet<-factor(dat$Diet)
dat$Aggregation<-factor(dat$Aggregation)
dat$Position<-factor(dat$Position)
#Remove Aus summer only species
aus_summer<-read.csv('sp_list_summer_only.csv')
dat[dat$Species %in% aus_summer$Fish,]$AUS_sp<-0
# Read in biomass data
bio_jpn<-read.csv('Jpn_transects_biomass.csv')
bio_aus<-read.csv('Aus_transects_biomass.csv')
# read in species per site biomass data for PCoA
aus_sp_site<-read.csv('Aus_site_species_biomass.csv')
jpn_sp_site<-read.csv('Jpn_site_species_biomass.csv')
# FG to factor
bio_jpn$FG<-factor(bio_jpn$FG)
bio_aus$FG<-factor(bio_aus$FG)
# Remove 2 poorly sampled sites from Jpn and Aus
bio_jpn<-filter(bio_jpn, !SiteID %in% c('JP27', 'JP32'))
bio_aus<-filter(bio_aus, !Site %in% c('Flat Rock', 'Wolf Rock'))
jpn_sp_site<-filter(jpn_sp_site, !SiteID %in% c('JP27', 'JP32'))
aus_sp_site<-filter(aus_sp_site, !Site %in% c('Flat Rock', 'Wolf Rock'))
# correct biomass
bio_aus$cor_biom<-bio_aus$tot_biom/bio_aus$totMsurv
bio_jpn$cor_biom<-bio_jpn$tot_biom/bio_jpn$totMsurv
aus_sp_site$cor_biom<-aus_sp_site$tot_biom/aus_sp_site$totMsurv
jpn_sp_site$cor_biom<-jpn_sp_site$tot_biom/jpn_sp_site$totMsurv
# and summarise by site for sp site data
aus_sp_site<-aus_sp_site%>%group_by(Site, Lat, FG, ThermalAffinity2, Fish)%>%
summarise(cor_biom=sum(cor_biom))
jpn_sp_site<-jpn_sp_site%>%group_by(SiteID, lat, FG, ThermalAffinity2, SpeciesFish)%>%
summarise(cor_biom=sum(cor_biom))
########################################
#################### Fig 2 Word Cloud creation ##########################
###################################################################
dat_mice<-mice(dat[c("BodySize","DepthRange", "Diet", "Position", "Aggregation")], m=5, method=c(rep('norm.predict', 2), rep('polyreg', 3)))
dat_imp<-mice::complete(dat_mice)
dat_imp<-cbind(Species=dat[,1], ThermalAffinity=dat["ThermalAffinity2"], dat_imp, dat[,8:length(dat)])
#manual edit
dat_imp[dat_imp$Species=='Cantheschenia grandisquamis',]$Diet<-'Omnivore'
# Bodysize
# 6 classes on cuts from Mouillot et al (2014)
dat_imp$BodySize <- cut(dat_imp$BodySize ,breaks = c(-Inf, 7,15,30,50, 80, Inf),
labels = c("Tiny","VSmall", "Small", "Medium", "Large", "VLarge"))
# DepthRange
ggplot(data=dat_imp, aes(x=DepthRange))+geom_histogram(binwidth=10, col='black')+
scale_x_continuous(breaks=seq(0, 420, 10))
# split into those that can only dive to coral reef (~27m) depths,
# the photic zone (~100m), and deeper > 100m. loosely similar to Mouillot et al (2014) 'Water column' trait
dat_imp$DepthRange <- cut(dat_imp$DepthRange ,breaks = c(-Inf, 30,100, Inf),
labels = c("shallow", "mid-depth", "deep"))
dat_imp$FE<-paste(dat_imp$BodySize, dat_imp$DepthRange,
dat_imp$Diet, dat_imp$Position, dat_imp$Aggregation)
# How to represent what each FG is using wordclouds of FEs
# reformat data
# split dataframe into list based on rows
FEdat<-dat_imp %>% group_by(FE) %>% summarise(FG=unique(groupk19),num=n())
FEdatlist<-split(FEdat, 1:nrow(FEdat))
FEwordlist<-lapply(FEdatlist, function(x){data.frame(FEcomp=unlist(strsplit(x$FE, ' ')),
n=x$num, FG=x$FG, FE=x$FE)})
FEword.df<-do.call('rbind', FEwordlist)
FEword.agg<-FEword.df %>% group_by(FG, FEcomp) %>% summarize(sum_word=sum(n))
# arranges FEcomp in descending order of n, by FG
FEword.agg<-FEword.agg %>% group_by(FG) %>% arrange(desc(sum_word), .by_group=TRUE)
# help with colouring https://stackoverflow.com/questions/18902485/colored-categories-in-r-wordclouds
FEword.agg$colorlist='blue'
FEword.agg$colorlist<-ifelse(FEword.agg$FEcomp%in%unique(dat_imp$Diet),
'orange', FEword.agg$colorlist)
FEword.agg$colorlist<-ifelse(FEword.agg$FEcomp%in%unique(dat_imp$Aggregation),
'green', FEword.agg$colorlist)
FEword.agg$colorlist<-ifelse(FEword.agg$FEcomp%in%unique(dat_imp$DepthRange),
'purple', FEword.agg$colorlist)
FEword.agg$colorlist<-ifelse(FEword.agg$FEcomp%in%unique(dat_imp$BodySize),
'red', FEword.agg$colorlist)
# preserves scaling i.e. sizes of FGs
ggplot(FEword.agg, aes(label=FEcomp, size=sum_word, colour=colorlist))+
geom_text_wordcloud()+scale_size_area()+facet_wrap(~FG)
FEword.agg.cl<-split(FEword.agg, FEword.agg$FG)
# include seed
out<-lapply(FEword.agg.cl[as.numeric(names(sort(table(dat$groupk19), decreasing = T)))], function(x){
ggplot(x, aes(label=FEcomp, size=sum_word, colour=colorlist))+
geom_text_wordcloud(seed=300)+scale_size_area()+theme_minimal()+
labs(title=paste('FG', unique(x$FG), 'n=', sum(x$sum_word)/5))})
do.call('grid.arrange', out)
ggsave('C:/coral_fish/outputs/wordclouds.eps',
plot=do.call('grid.arrange', out),width = 20, height = 20, units = "cm")
# get names for future plots
FEword.agg%>%group_by(FG)%>%
mutate(totword=sum(sum_word), cumword=cumsum(sum_word), wordperc=cumword/totword*100)%>%
filter(wordperc<59) ->FGnames
FGnames<-as.data.frame(aggregate(FEcomp~FG, FGnames,
function(x){paste(x, collapse='-')}))
######################################################################
#### Fig 2 Tropical-subtropical Biomass comparisons (4rt) ####
##############################################################
jpn_cor<-bio_jpn%>%filter(FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))%>%
dplyr::select(FG, Site.trans.ID,ThermalAffinity2, cor_biom)%>%
group_by(FG,Site.trans.ID)%>%tidyr::spread(ThermalAffinity2, cor_biom)%>%
ungroup()%>%group_by(FG)%>%
summarise(cor_p=cor.test((tropical^0.25), (subtropical^0.25), method = 'kendall')$p.value,
cop_est=cor.test((tropical^0.25), (subtropical^0.25), method = 'kendall')$estimate)
jpn_cor$txt=ifelse(jpn_cor$cor_p<0.001, paste0('tau=',round(jpn_cor$cop_est, 2), '***'),
ifelse(jpn_cor$cor_p<0.01, paste0('tau=',round(jpn_cor$cop_est, 2), '**'),
ifelse(jpn_cor$cor_p<0.05, paste0('tau=',round(jpn_cor$cop_est, 2), '*'),
NA)))
aus_cor<-bio_aus%>%filter(FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))%>%
dplyr::select(FG, Site.trans.ID,ThermalAffinity2, cor_biom)%>%
group_by(FG,Site.trans.ID)%>%tidyr::spread(ThermalAffinity2, cor_biom)%>%
ungroup()%>%group_by(FG)%>%
summarise(cor_p=cor.test((tropical^0.25), (subtropical^0.25), method = 'kendall')$p.value,
cop_est=cor.test((tropical^0.25), (subtropical^0.25), method = 'kendall')$estimate)
aus_cor$txt=ifelse(aus_cor$cor_p<0.001, paste0('tau=',round(aus_cor$cop_est, 2), '***'),
ifelse(aus_cor$cor_p<0.01, paste0('tau=',round(aus_cor$cop_est, 2), '**'),
ifelse(aus_cor$cor_p<0.05, paste0('tau=',round(aus_cor$cop_est, 2), '*'),
NA)))
sg_lat_spans<-data.frame(xmin=c(24.2, 26.2, 28.5, 31, 32.7, 33.38, 34.6),
xmax=c(24.5, 28.4, 30.5, 31.6, 32.82, 33.5, 35 ))
bio_jpn$ThermalAffinity2<-factor(bio_jpn$ThermalAffinity2, levels=c('tropical', 'subtropical'))
jpn_plot_dat<-filter(bio_jpn, FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))
jpn_nsp<-dat%>%filter(JPN_sp==1 & groupk19 %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))%>%
group_by(groupk19,ThermalAffinity2)%>%summarise(n())%>%
dplyr::rename(FG=groupk19, count=`n()`)
jpn_nsp$y=3.5
jpn_nsp$x=34
jpn_nsp[jpn_nsp$ThermalAffinity2=='subtropical',]$x=35
jpn_plot_dat$FG<-factor(jpn_plot_dat$FG, levels=c(15,10,8,2,6,12,4,1,16))
jpn_cor$FG<-factor(jpn_cor$FG, levels=c(15,10,8,2,6,12,4,1,16))
jpn_nsp$FG<-factor(jpn_nsp$FG, levels=c(15,10,8,2,6,12,4,1,16))
p1<-ggplot(data=jpn_plot_dat) +
geom_rect(data=sg_lat_spans, aes(ymin=0, ymax=4, xmin=xmin, xmax=xmax), fill='gainsboro')+
geom_point(data=filter(jpn_plot_dat, ((tot_biom/totMsurv)^0.25)<4),
aes(x = lat, y = (tot_biom/totMsurv)^0.25, colour=ThermalAffinity2), shape=1)+
geom_smooth(aes(x = lat, y = (tot_biom/totMsurv)^0.25, colour=ThermalAffinity2),se=F)+
geom_smooth(data=jpn_plot_dat%>%group_by(Site.trans.ID, FG)%>%summarise(cor_biom=sum(cor_biom), lat=first(lat)),
aes(x = lat, y = cor_biom^0.25),colour='black', linetype='dotted',se=F)+
geom_label(data=jpn_cor, aes(x=28.5, y=3.5, label=txt))+
geom_label(data=jpn_nsp, aes(x=x, y=y, colour=ThermalAffinity2, label=count))+
facet_grid(FG~., scales='free_y')+
theme_bw()+theme(legend.position = "none")+
xlab('Latitude')+ylab('4rt-trans standardised biomass')+
ggtitle('Japan')+
theme(strip.background = element_blank(),strip.text.y = element_blank(),
plot.title = element_text(hjust = 0.5))
sg_lat_spans<-data.frame(xmin=c(-23.4, -24.8, -26.61, -28.19, -29.9, -29.97),
xmax=c(-24.1, -25.3, -26.98, -28.616, -30.96, -30.3))
bio_aus$ThermalAffinity2<-factor(bio_aus$ThermalAffinity2, levels=c('tropical', 'subtropical'))
aus_plot_dat<-filter(bio_aus, FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))
aus_nsp<-dat%>%filter(AUS_sp==1 & groupk19 %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))%>%
group_by(groupk19,ThermalAffinity2)%>%summarise(n())%>%
dplyr::rename(FG=groupk19, count=`n()`)
aus_nsp$y=3.5
aus_nsp$x=-30
aus_nsp[aus_nsp$ThermalAffinity2=='subtropical',]$x=-31
aus_plot_dat$FG<-factor(aus_plot_dat$FG, levels=c(15,10,8,2,6,12,4,1,16))
aus_cor$FG<-factor(aus_cor$FG, levels=c(15,10,8,2,6,12,4,1,16))
aus_nsp$FG<-factor(aus_nsp$FG, levels=c(15,10,8,2,6,12,4,1,16))
p2<-ggplot(data=aus_plot_dat) +
geom_rect(data=sg_lat_spans, aes(ymin=0, ymax=4, xmin=xmin, xmax=xmax), fill='gainsboro')+
geom_point(data=filter(aus_plot_dat, ((tot_biom/totMsurv)^0.25)<4),
aes(x = Lat, y = (tot_biom/totMsurv)^0.25, colour=ThermalAffinity2), shape=1)+
geom_smooth(aes(x = Lat, y = (tot_biom/totMsurv)^0.25, colour=ThermalAffinity2),se=F)+
geom_smooth(data=aus_plot_dat%>%group_by(Site.trans.ID, FG)%>%summarise(cor_biom=sum(cor_biom), Lat=first(Lat)),
aes(x = Lat, y = cor_biom^0.25),colour='black', linetype='dotted',se=F)+
geom_label(data=aus_cor, aes(x=-26, y=3.5, label=txt))+
geom_label(data=aus_nsp, aes(x=x, y=y, colour=ThermalAffinity2, label=count))+
facet_grid(FG~., scales='free_y')+scale_x_reverse()+ylab(NULL)+
ggtitle('Australia')+
theme_bw()+theme(legend.position = "none")+
xlab('Latitude')+theme(plot.title = element_text(hjust = 0.5))
#png('C:/coral_fish/outputs/biomass_thermal_plot_sums.png',width = 8, height =12 , units ="in", res =300)
grid.arrange(p2, p1, ncol=2)
dev.off()
#ggsave('C:/coral_fish/outputs/fig2_biomass_labels.eps',
# plot=grid.arrange(p2, p1, ncol=2),width = 20, height = 29, units = "cm")
################################################################
#### Prepare tropical-only data for tropicalization footprint analyses ####
#########################################################################
# Calc standardisation
# filter out unwanted FGs for per FG objects but not for community total
jpn_trop_prop<-filter(bio_jpn, ThermalAffinity2=='tropical' & lat<25.5)%>%
group_by(FG)%>%summarise(mean_biom=(mean(cor_biom^0.25, na.rm=T))^4)
aus_trop_prop<-filter(bio_aus, ThermalAffinity2=='tropical' & Lat> -24.5)%>%
group_by(FG)%>%summarise(mean_biom=(mean(cor_biom^0.25, na.rm=T))^4)
jpn_trop_prop<-left_join(filter(bio_jpn, ThermalAffinity2=='tropical' &
FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16)),
jpn_trop_prop, by='FG')
aus_trop_prop<-left_join(filter(bio_aus, ThermalAffinity2=='tropical' &
FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16)),
aus_trop_prop, by='FG')
jpn_trop_comm<-filter(bio_jpn, ThermalAffinity2=='tropical')%>%
group_by(lat, SiteID, Site.trans.ID)%>%summarise(cor_biom=sum(cor_biom))
jpn_trop_comm$mean_biom<-as.numeric(filter(jpn_trop_comm, lat<25.5)%>%ungroup()%>%
summarise(mean_biom=(mean(cor_biom^0.25, na.rm=T))^4))
aus_trop_comm<-filter(bio_aus, ThermalAffinity2=='tropical')%>%
group_by(Lat, Site, Site.trans.ID)%>%summarise(cor_biom=sum(cor_biom))
aus_trop_comm$mean_biom<-as.numeric(filter(aus_trop_comm, Lat> -24.5)%>%ungroup()%>%
summarise(mean_biom=(mean(cor_biom^0.25, na.rm=T))^4))
jpn_trop_prop$FG<-factor(jpn_trop_prop$FG, levels=c(15, 10, 8, 2,6,12,4,1,16))
aus_trop_prop$FG<-factor(aus_trop_prop$FG, levels=c(15, 10, 8, 2,6,12,4,1,16))
# add tropicalization_metric: >1 = more biomass at site compared to tropical site-group
# Using 4th root transformation to make data approx normality (can still handle 0's though)
jpn_trop_comm$trop_met<-(jpn_trop_comm$cor_biom/jpn_trop_comm$mean_biom)^0.25
aus_trop_comm$trop_met<-(aus_trop_comm$cor_biom/aus_trop_comm$mean_biom)^0.25
jpn_trop_prop$trop_met<-(jpn_trop_prop$cor_biom/jpn_trop_prop$mean_biom)^0.25
aus_trop_prop$trop_met<-(aus_trop_prop$cor_biom/aus_trop_prop$mean_biom)^0.25
# create site-groups ("Zones") based on dendrogram clustering
# Note Zone naming differs slightly from those in paper text
jpn_trop_comm$FG<-'comm'
jpn_trop_prop$FG<-as.character(jpn_trop_prop$FG)
jpn_trop_tests<-rbind(data.frame(jpn_trop_comm), jpn_trop_prop[names(jpn_trop_comm)])
jpn_trop_tests$site.group<-'trop.base'
jpn_trop_tests[jpn_trop_tests$lat > 25 & jpn_trop_tests$lat < 28.5,]$site.group<-'trop.island'
jpn_trop_tests[jpn_trop_tests$lat > 28.5 & jpn_trop_tests$lat < 31,]$site.group<-'trans.island'
jpn_trop_tests[jpn_trop_tests$SiteID %in% c('JP28', 'JP29', 'JP30', 'JP31'),]$site.group<-'trans.inland'
jpn_trop_tests[jpn_trop_tests$SiteID %in% c('JP8', 'JP9', 'JP10', 'JP11', 'JP12'),]$site.group<-'trans.headld'
jpn_trop_tests[jpn_trop_tests$lat > 34,]$site.group<-'temp.headld'
table(jpn_trop_tests$SiteID, jpn_trop_tests$site.group)
aus_trop_comm$FG<-'comm'
aus_trop_prop$FG<-as.character(aus_trop_prop$FG)
aus_trop_tests<-rbind(data.frame(aus_trop_comm), aus_trop_prop[names(aus_trop_comm)])
aus_trop_tests$site.group<-'trop.base'
aus_trop_tests[aus_trop_tests$Lat > -25.6 & aus_trop_tests$Lat < -24.5,]$site.group<-'trans.bay'
aus_trop_tests[aus_trop_tests$Lat > -28 & aus_trop_tests$Lat < -25.6,]$site.group<-'trans.offshore'
aus_trop_tests[aus_trop_tests$Lat < -28,]$site.group<-'temp.offshore'
aus_trop_tests[aus_trop_tests$Site %in% c('Julian Rock False Trench', 'Julian Rock Nursery', 'Cook Island'),]$site.group<-'trans.temp'
aus_trop_tests[aus_trop_tests$Site %in% c('Muttonbird Island', 'Woolgoolga Reef', 'Woolgoolga Headland', 'North Rock'),]$site.group<-'temp.inshore'
table(aus_trop_tests$Site,aus_trop_tests$site.group)
###################################################################
#### Make FG tropicalization + community trends over latitude (Fig 3 A & C) ####
################################################################################
sg_lat_spans<-data.frame(xmin=c(24.2, 26.2, 28.5, 31, 32.7, 33.38, 34.6),
xmax=c(24.5, 28.4, 30.5, 31.6, 32.82, 33.5, 35 ))
jpn_trop_prop$FG<-factor(jpn_trop_prop$FG, levels=c(15, 10, 8, 2,6,12,4,1,16))
jpn_trop_prop$FG_name<-recode(jpn_trop_prop$FG,
'15'='Benthic Predators', '10'='Upper-benthic Predators',
'8'='Benthic Herbivore/Omnivores', '2'='Upper-benthic Planktivores',
'6'='Upper-benthic Herbivores','12'='Demersal Predators',
'4'='Benthic Planktivores','1'='Upper-benthic Omnivores',
'16'='Corallibovores')
jpn_trend<-ggplot(jpn_trop_prop) +
geom_rect(data=sg_lat_spans, aes(ymin=0, ymax=2, xmin=xmin, xmax=xmax), fill='grey', alpha=0.5)+
geom_smooth(aes(x = lat, y = (cor_biom/mean_biom)^0.25, colour=FG_name), se=F)+
geom_smooth(data=jpn_trop_comm, aes(x = lat, y = (cor_biom/mean_biom)^0.25),
se=F, colour='black', linetype='dashed')+
theme_bw()+
geom_hline(yintercept=0.05^0.25)+scale_x_continuous(breaks=24:35)+
xlab('Latitude')+ylab('Proportion of tropical biomass')+
scale_y_continuous(breaks=c(0, 0.05, 0.25, 0.5, 1, 2, 4, 20)^0.25,
labels=c(0,0.05, 0.25, 0.5, 1, 2, 4, 20), minor_breaks = NULL)
sg_lat_spans<-data.frame(xmin=c(-23.4, -24.8, -26.61, -28.19, -29.9, -29.97),
xmax=c(-24.1, -25.3, -26.98, -28.616, -30.96, -30.3))
aus_trop_prop$FG<-factor(aus_trop_prop$FG, levels=c(15, 10, 8, 2,6,12,4,1,16))
aus_trop_prop$FG_name<-recode(aus_trop_prop$FG,
'15'='Benthic Predators', '10'='Upper-benthic Predators',
'8'='Benthic Herbivore/Omnivores', '2'='Upper-benthic Planktivores',
'6'='Upper-benthic Herbivores','12'='Demersal Predators',
'4'='Benthic Planktivores','1'='Upper-benthic Omnivores',
'16'='Corallibovores')
aus_trend<-ggplot(aus_trop_prop) +
geom_rect(data=sg_lat_spans, aes(ymin=0, ymax=4^0.25, xmin=xmin, xmax=xmax), fill='grey', alpha=0.6)+
geom_smooth(aes(x = Lat, y = (cor_biom/mean_biom)^0.25, colour=factor(FG)), se=F)+
geom_smooth(data=aus_trop_comm, aes(x = Lat, y = (cor_biom/mean_biom)^0.25),
se=F, colour='black', linetype='dashed')+
theme_bw()+theme(legend.position = "none")+
geom_hline(yintercept=0.05^0.25)+scale_x_reverse(breaks=-23:-31)+
xlab('Latitude')+ylab('Proportion of tropical biomass')+
scale_y_continuous(breaks=c(0, 0.05, 0.25, 0.5, 1, 2, 4)^0.25,
labels=c(0,0.05, 0.25, 0.5, 1, 2, 4), minor_breaks = NULL)
#################################################################################
#### Japan FG tropicalization footprint comparisons (results for Fig 3.B) ####
###############################################################################
# set comm as intercept
jpn_trop_tests$FG<-factor(jpn_trop_tests$FG, levels=c('comm', 15, 10, 8, 2,6,12,4,1,16))
# remove comm for diff between FG stat reporting
#jpn_trop_tests<-filter(jpn_trop_tests, FG!='comm')
#jpn_trop_tests$FG<-factor(jpn_trop_tests$FG, levels=c( 15, 10, 8, 2,6,12,4,1,16))
## trop.island
ggplot(data=filter(jpn_trop_tests, site.group=='trop.island'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|SiteID, data=filter(jpn_trop_tests, site.group=='trop.island'),
weights=varIdent(form=~1|FG))
boxplot(residuals(m1, type='pearson')~filter(jpn_trop_tests, site.group=='trop.island')$FG) # ok good
# pearson and normalized residuals incorporate the effect of the gls variance structure
em1<-emmeans(m1, specs='FG')
pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out<-data.frame(site.group='trop.island',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:9, c(1,6)]))
ggplot()+
geom_jitter(data=filter(jpn_trop_tests, site.group=='trop.island'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept = trop_comps_out$emmean[1], linetype='dotted')+
geom_errorbar(data=trop_comps_out, aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=trop_comps_out, aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## trans.island
ggplot(data=filter(jpn_trop_tests, site.group=='trans.island'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|SiteID, weights=varIdent(form=~1|FG), data=filter(jpn_trop_tests, site.group=='trans.island'))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out<-rbind(trop_comps_out,
data.frame(site.group='trans.island',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(jpn_trop_tests, site.group=='trans.island'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out[trop_comps_out$site.group=='trans.island',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out, site.group=='trans.island'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out, site.group=='trans.island'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## trans.inland
ggplot(data=filter(jpn_trop_tests, site.group=='trans.inland'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|SiteID, weights=varIdent(form=~1|FG), data=filter(jpn_trop_tests, site.group=='trans.inland'))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out<-rbind(trop_comps_out,
data.frame(site.group='trans.inland',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(jpn_trop_tests, site.group=='trans.inland'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out[trop_comps_out$site.group=='trans.inland',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out, site.group=='trans.inland'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out, site.group=='trans.inland'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## trans.headld
ggplot(data=filter(jpn_trop_tests, site.group=='trans.headld'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|SiteID, weights=varIdent(form=~1|FG),
data=filter(jpn_trop_tests, site.group=='trans.headld'),
control=lmeControl(opt = 'optim')) # different optimizer used to make run
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out<-rbind(trop_comps_out,
data.frame(site.group='trans.headld',data.frame(em1), rbind(c(NA, NA),
data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(jpn_trop_tests, site.group=='trans.headld'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out[trop_comps_out$site.group=='trans.headld',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out, site.group=='trans.headld'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out, site.group=='trans.headld'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## temp.headld
ggplot(data=filter(jpn_trop_tests, site.group=='temp.headld'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
# remove FGs with only 0s - these will be sig diff
zer_fgs<-filter(jpn_trop_tests, site.group=='temp.headld')%>%group_by(FG)%>%summarise(st=sum(trop_met))%>%filter(., st==0)%>%.$FG
m1<-lme(trop_met~FG, random=~1|SiteID, weights=varIdent(form=~1|FG), data=filter(jpn_trop_tests, site.group=='temp.headld' & !FG%in% zer_fgs))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out<-rbind(trop_comps_out,
data.frame(site.group='temp.headld',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:(9-length(zer_fgs)), c(1,6)])))
ggplot()+
geom_jitter(data=filter(jpn_trop_tests, site.group=='temp.headld'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out[trop_comps_out$site.group=='temp.headld',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out, site.group=='temp.headld'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out, site.group=='temp.headld'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
#### make Fig 3.B to combine with others ####
trop_comps_out<-rbind(trop_comps_out,
data.frame(site.group='temp.headld', FG=c(6,12,1,16), emmean=0, SE=0,
df=0, lower.CL=0, upper.CL=0, contrast=NA, p.value=0))
trop_comps_out$site.group<-factor(trop_comps_out$site.group,
levels=c('trop.base', 'trop.island', 'trans.island', 'trans.inland',
'trans.headld', 'temp.headld'))
trop_comps_out$site.group2<-recode(trop_comps_out$site.group,
trop.base = 'Tropical Coral Reef',
trop.island = 'Cold-tropical Reef',
trans.island = 'Transitional Reef',
trans.inland = 'Subtropical Coral Community',
trans.headld = 'Warm-temperate Transitional Reef',
temp.headld = 'Temperate Kelp Reef')
jpn_mods<-ggplot()+
geom_hline(yintercept=0.05^0.25, linetype='dotted')+
geom_errorbar(data=trop_comps_out, aes(x=FG, ymin=emmean-SE, ymax=emmean+SE))+
geom_point(data=trop_comps_out, aes(x=FG, y=emmean, size=ifelse(is.na(p.value), 1, ifelse(p.value>0.05, 1, 2))), shape=1)+
geom_point(data=trop_comps_out, aes(x=FG, y=emmean, colour=factor(FG)), size=2)+
theme_bw()+theme(legend.position = "none")+facet_wrap(~site.group2, nrow=1)+
scale_colour_manual(values = c("black", "#F8766D", "#D39200" ,"#93AA00", "#00BA38",
"#00C19F", "#00B9E3", "#619CFF", "#DB72FB", "#FF61C3"))+
ylab('Proportion of tropical biomass')+
scale_y_continuous(breaks=c(0, 0.05, 0.25, 0.5, 1, 2, 4, 20)^0.25,
labels=c(0,0.05, 0.25, 0.5, 1, 2, 4, 20), minor_breaks = NULL)
#### Australia FG tropicalization footprint comparisons (results for Fig 3.D) ####
##################################################################################
# drop some FGs and set comm as intercept
aus_trop_tests$FG<-factor(aus_trop_tests$FG, levels=c('comm', 15, 10, 8, 2,6,12,4,1,16))
# remove comm for diff between FG stat reporting
#aus_trop_tests<-filter(aus_trop_tests, FG!='comm')
#aus_trop_tests$FG<-factor(aus_trop_tests$FG, levels=c( 15, 10, 8, 2,6,12,4,1,16))
## trans.bay
ggplot(data=filter(aus_trop_tests, site.group=='trans.bay'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
# drop 2 big outliers, actually don't
m1<-lme(trop_met~FG, random=~1|Site, weights=varIdent(form=~1|FG), data=filter(aus_trop_tests,
site.group=='trans.bay'))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
pairs(em1)
trop_comps_out_aus<-data.frame(site.group='trans.bay',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:9, c(1,6)]))
ggplot()+
geom_jitter(data=filter(aus_trop_tests, site.group=='trans.bay'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out_aus[trop_comps_out_aus$site.group=='trans.bay',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out_aus, site.group=='trans.bay'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out_aus, site.group=='trans.bay'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none") # stupid massive outlier drags up 12, makes it look ns but it is sig diff
## trans.offshore
ggplot(data=filter(aus_trop_tests, site.group=='trans.offshore'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|Site, weights=varIdent(form=~1|FG), data=filter(aus_trop_tests, site.group=='trans.offshore'))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
#pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out_aus<-rbind(trop_comps_out_aus,
data.frame(site.group='trans.offshore',data.frame(em1), rbind(c(NA, NA),
data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(aus_trop_tests, site.group=='trans.offshore'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out_aus[trop_comps_out_aus$site.group=='trans.offshore',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out_aus, site.group=='trans.offshore'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out_aus, site.group=='trans.offshore'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## trans.temp
ggplot(data=filter(aus_trop_tests, site.group=='trans.temp'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|Site, weights=varIdent(form=~1|FG), data=filter(aus_trop_tests, site.group=='trans.temp'))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
#pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out_aus<-rbind(trop_comps_out_aus,
data.frame(site.group='trans.temp',data.frame(em1), rbind(c(NA, NA),
data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(aus_trop_tests, site.group=='trans.temp'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out_aus[trop_comps_out_aus$site.group=='trans.temp',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out_aus, site.group=='trans.temp'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out_aus, site.group=='trans.temp'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## temp.offshore
ggplot(data=filter(aus_trop_tests, site.group=='temp.offshore'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~FG, random=~1|Site, weights=varIdent(form=~1|FG), data=filter(aus_trop_tests,
site.group=='temp.offshore' & trop_met<4)) # remove massive outlier
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
#pairs(em1)
plot(em1, comparisons = TRUE)
trop_comps_out_aus<-rbind(trop_comps_out_aus,
data.frame(site.group='temp.offshore',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:9, c(1,6)])))
ggplot()+
geom_jitter(data=filter(aus_trop_tests, site.group=='temp.offshore'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out_aus[trop_comps_out_aus$site.group=='temp.offshore',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out_aus, site.group=='temp.offshore'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out_aus, site.group=='temp.offshore'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
## temp.inshore
ggplot(data=filter(aus_trop_tests, site.group=='temp.inshore'), aes(x=FG, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
# remove FGs with only 0s - these will be sig diff
zer_fgs<-filter(aus_trop_tests, site.group=='temp.inshore')%>%group_by(FG)%>%summarise(st=sum(trop_met))%>%filter(., st==0)%>%.$FG
m1<-lme(trop_met~FG, random=~1|Site, weights=varIdent(form=~1|FG), data=filter(aus_trop_tests, site.group=='temp.inshore' & !FG%in% zer_fgs))
#resid_panel(m1)
summary(m1)
em1<-emmeans(m1, specs='FG')
#pairs(em1)
#plot(em1, comparisons = TRUE)
trop_comps_out_aus<-rbind(trop_comps_out_aus,
data.frame(site.group='temp.inshore',data.frame(em1), rbind(c(NA, NA), data.frame(pairs(em1))[1:(9-length(zer_fgs)), c(1,6)])))
ggplot()+
geom_jitter(data=filter(aus_trop_tests, site.group=='temp.inshore'), aes(x=FG, y=trop_met), shape=1, alpha=0.5, width=0.2, colour='grey')+
geom_hline(yintercept =trop_comps_out_aus[trop_comps_out_aus$site.group=='temp.inshore',]$response[1], linetype='dotted')+
geom_errorbar(data=filter(trop_comps_out_aus, site.group=='temp.inshore'), aes(x=FG, ymin=lower.CL, ymax=upper.CL))+
geom_point(data=filter(trop_comps_out_aus, site.group=='temp.inshore'), aes(x=FG, y=emmean, colour=ifelse(p.value>0.05| is.na(p.value), 'blue', 'red')), size=2)+
theme_bw()+theme(legend.position = "none")
### make Fig 3.D to combine with others ####
trop_comps_out_aus<-rbind(trop_comps_out_aus,
data.frame(site.group='temp.inshore', FG=c(6,12,16), emmean=0, SE=0,
df=0, lower.CL=0, upper.CL=0, contrast=NA, p.value=0))
trop_comps_out_aus$site.group<-factor(trop_comps_out_aus$site.group, levels=c('trop.base', 'trans.bay', 'trans.offshore',
'trans.temp', 'temp.offshore', 'temp.inshore'))
trop_comps_out_aus$site.group2<-recode(trop_comps_out_aus$site.group,
trop.base = 'Tropical Coral Reef',
trans.bay = 'Cold-tropical Bay',
trans.offshore = 'Warm-transitional Reef',
trans.temp = 'Transitional Reef',
temp.offshore = 'Subtropical Coral Community',
temp.inshore = 'Temperate Kelp Reef')
aus_mods<-ggplot()+
geom_hline(yintercept=0.05^0.25, linetype='dotted')+
geom_errorbar(data=trop_comps_out_aus, aes(x=FG, ymin=emmean-SE, ymax=emmean+SE))+
geom_point(data=trop_comps_out_aus, aes(x=FG, y=emmean, size=ifelse(is.na(p.value), 1, ifelse(p.value>0.05, 1, 2))), shape=1)+
geom_point(data=trop_comps_out_aus, aes(x=FG, y=emmean, colour=factor(FG)), size=2)+
theme_bw()+theme(legend.position = "none")+facet_wrap(~site.group2, nrow=1)+
scale_colour_manual(values = c("black", "#F8766D", "#D39200" ,"#93AA00", "#00BA38",
"#00C19F", "#00B9E3", "#619CFF", "#DB72FB", "#FF61C3"))+
ylab('Proportion of tropical biomass')+
scale_y_continuous(breaks=c(0, 0.05, 0.25, 0.5, 1, 2, 4, 20)^0.25,
labels=c(0,0.05, 0.25, 0.5, 1, 2, 4, 20), minor_breaks = NULL)
# write out results
#write.csv(rbind(trop_comps_out, trop_comps_out_aus), 'C:/coral_fish/outputs/sitegroup_FG_tropicalization.csv', quote=F, row.names=F)
############################################################
#### Fig 3 tropicalization footprint plot and write out ####
############################################################
ggsave('C:/coral_fish/outputs/fig3_tropicalization_footprints_legend.eps',
plot=jpn_trend+jpn_mods+aus_trend+aus_mods+
plot_layout(ncol=1, nrow=4, guides = 'collect')&theme(legend.position = 'bottom')
,width = 21, height = 30, units = "cm")
ggsave('C:/coral_fish/outputs/fig3_tropicalization_footprints_NOlegend.eps',
plot=jpn_trend+jpn_mods+aus_trend+aus_mods+
plot_layout(ncol=1, nrow=4, guides = 'collect')&theme(legend.position = 'none')
,width = 21, height = 30, units = "cm")
############################################################
#### Look for sig diff between FGs and community-level tropicalization footprint per zone ####
##############################################################################################
jpn_trop_tests$site.group<-factor(jpn_trop_tests$site.group,
levels=c('trop.base', 'trop.island', 'trans.island', 'trans.inland',
'trans.headld', 'temp.headld'))
ggplot(data=filter(jpn_trop_tests, FG=='comm'), aes(x=site.group, y=trop_met))+
geom_point(aes(colour=SiteID))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~site.group, random=~1|SiteID, weights=varIdent(form=~1|site.group),
data=filter(jpn_trop_tests, FG=='comm'))
resid_panel(m1)
summary(m1)
anova(m1)
em1<-emmeans(m1, specs='site.group')
pairs(em1)
plot(em1, comparisons = TRUE)
aus_trop_tests$site.group<-factor(aus_trop_tests$site.group, levels=c('trop.base', 'trans.bay', 'trans.offshore',
'trans.temp', 'temp.offshore', 'temp.inshore'))
ggplot(data=filter(aus_trop_tests, FG=='comm'), aes(x=site.group, y=trop_met))+
geom_point(aes(colour=Site))+geom_boxplot(alpha=0.5) # remember boxplot = medians
m1<-lme(trop_met~site.group, random=~1|Site, weights=varIdent(form=~1|site.group),
data=filter(aus_trop_tests, FG=='comm' & !Site.trans.ID%in%
c('Julian Rock Nursery_T5','Julian Rock False Trench_T4'))) # removal of 2 big outliers
resid_panel(m1)
summary(m1)
anova(m1)
em1<-emmeans(m1, specs='site.group')
pairs(em1)
plot(em1, comparisons = TRUE)
##############################################################################
#### Test functional groups are independent of thermal preference with #####
################# Stuart-smith (2018) thermal midpoint data ################
# We do not have permission to distribute the Stuart-Smith et al (2018) thermal midpoint data
# however the estimated thermal midpoint per FG, created later in this section 'thermal_midpoint_means.csv',
# is included to allow interrogation of results.
therm_mid<-readxl::read_xlsx('C:/coral_fish/sourced_data/stuart_smith_thermal_midpoints/Thermal niche midpoints.xlsx')
tanika_match<-read.csv('C:/coral_fish/sourced_data/stuart_smith_thermal_midpoints/species_match_tanika.csv')
# bodge loop to fix 13 species names
for(i in tanika_match[tanika_match$Name.in.st.data!='',]$Name.in.st.data){
therm_mid[therm_mid$SPECIES_NAME==i,]$SPECIES_NAME<-as.character(tanika_match[tanika_match$Name.in.st.data==i,]$Not.matched)}
dat_ss<-left_join(dat, therm_mid, by=c('Species'='SPECIES_NAME'))
which(is.na(dat_ss$`95th SSTmax`)) # some naming mis-matches/ missing sp ~80
table(dat_ss$ThermalAffinity2, dat_ss$`Temp-Trop (23cutoff)`) # some differences
# check for coverage of FGs and compare within each region
# filter to FGs we're interested in
dat_ss<-filter(dat_ss, groupk19 %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16))
dat_ss$groupk19<-factor(dat_ss$groupk19)
dat_ss$sst95<-dat_ss$`95th SSTmax`
#JPN
dat_ss%>%filter(JPN_sp>0 & ThermalAffinity2=='tropical')%>%group_by(groupk19)%>%
summarise(n_sp=n(), n_sp_conf=length(confidence[which(confidence>1)])) # pretty good
conf_tm_jpn<-dat_ss%>%filter(JPN_sp>0 & ThermalAffinity2=='tropical' & confidence>1)
qplot(data=conf_tm_jpn, x=groupk19, y=sst95, geom='boxplot') # looks good but need closer
m1<-lm(sst95~groupk19, data=conf_tm_jpn[-80,]) #remove outlier
#par(mfrow=c(2,2));plot(m1)
#resid_panel(m1)
#m1.1<-lm(sst95~groupk19, data=conf_tm_jpn[-c(80, which(resid(m1)>1|resid(m1)< -1)),])
resid_panel(m1.1)
summary(m1)
anova(m1) # ns
pairs(emmeans(m1, 'groupk19'))
jpn_mean<-emmeans(m1, 'groupk19')
boxplot(resid(m1, type='pearson')~factor(conf_tm_jpn$groupk19))
ggplot(data=data.frame(emmeans(m1, 'groupk19')), aes(x=factor(groupk19), y=emmean))+geom_pointrange(aes(ymin=lower.CL, ymax=upper.CL))+xlab('Functional Group')+ylab('Thermal Midpoint')
#try with GLS to be sure
m2<-gls(sst95~factor(groupk19), data=conf_tm_jpn,
weights=varIdent(form=~1|groupk19))
boxplot(resid(m2, type='pearson')~factor(conf_tm_jpn$groupk19))
# no better, remember these groups have different n() so standard
# error different anyway
p1<-ggplot(data=data.frame(emmeans(m1, 'groupk19')), aes(x=factor(groupk19), y=emmean))+
geom_pointrange(aes(ymin=lower.CL, ymax=upper.CL))+xlab('Functional Group')+
ylab('Thermal Midpoint (°C)')+scale_y_continuous(limits=c(30.5, 31.6),
breaks=c(30.5,30.75, 31, 31.25, 31.5, 31.5))+labs(title ='Japan')
#AUS
dat_ss%>%filter(AUS_sp>0 & ThermalAffinity2=='tropical')%>%group_by(groupk19)%>%
summarise(n_sp=n(), n_sp_conf=length(confidence[which(confidence>1)])) # pretty good
conf_tm_aus<-dat_ss%>%filter(AUS_sp>0 & ThermalAffinity2=='tropical' & confidence>1)
qplot(data=conf_tm_aus, x=groupk19, y=sst95, geom='boxplot') # looks good but need closer
m1<-lm(sst95~groupk19, data=conf_tm_aus[-c(18, 186, 89),]) #remove outliers
par(mfrow=c(2,2));plot(m1)
resid_panel(m1)
summary(m1)
anova(m1) # ns
pairs(emmeans(m1, 'groupk19'))
aus_mean<-emmeans(m1, 'groupk19')
boxplot(resid(m1, type='pearson')~factor(conf_tm_aus$groupk19))
p2<-ggplot(data=data.frame(emmeans(m1, 'groupk19')), aes(x=factor(groupk19), y=emmean))+
geom_pointrange(aes(ymin=lower.CL, ymax=upper.CL))+xlab('Functional Group')+
ylab('Thermal Midpoint (°C)')+scale_y_continuous(limits=c(30.5, 31.6),
breaks=c(30.5,30.75, 31, 31.25, 31.5, 31.5))+labs(title ='Australia')
#png('C:/coral_fish/outputs/themal_midpoint_suppl.png',width =8, height =4 , units ="in", res =600)
grid.arrange(p1, p2, ncol=2)
dev.off()
write.csv(rbind(data.frame(region='Japan', jpn_mean),data.frame(region='Australia', aus_mean)),
'C:/coral_fish/outputs/thermal_midpoint_means.csv', quote=F, row.names=F)
## Check for site.group differences between FG that could be explained by sst differences
jpn_sp_site$site.group<-'trop.base'
jpn_sp_site[jpn_sp_site$lat > 25 & jpn_sp_site$lat < 28.5,]$site.group<-'trop.island'
jpn_sp_site[jpn_sp_site$lat > 28.5 & jpn_sp_site$lat < 31,]$site.group<-'trans.island'
jpn_sp_site[jpn_sp_site$SiteID %in% c('JP28', 'JP29', 'JP30', 'JP31'),]$site.group<-'trans.inland'
jpn_sp_site[jpn_sp_site$SiteID %in% c('JP8', 'JP9', 'JP10', 'JP11', 'JP12'),]$site.group<-'trans.headld'
jpn_sp_site[jpn_sp_site$lat > 34,]$site.group<-'temp.headld'
aus_sp_site$site.group<-'trop.base'
aus_sp_site[aus_sp_site$Lat > -25.6 & aus_sp_site$Lat < -24.5,]$site.group<-'trans.bay'
aus_sp_site[aus_sp_site$Lat > -28 & aus_sp_site$Lat < -25.6,]$site.group<-'trans.offshore'
aus_sp_site[aus_sp_site$Lat < -28,]$site.group<-'temp.offshore'
aus_sp_site[aus_sp_site$Site %in% c('Julian Rock False Trench', 'Julian Rock Nursery', 'Cook Island'),]$site.group<-'trans.temp'
aus_sp_site[aus_sp_site$Site %in% c('Muttonbird Island', 'Woolgoolga Reef', 'Woolgoolga Headland', 'North Rock'),]$site.group<-'temp.inshore'
aus_sp_site$site.group<-factor(aus_sp_site$site.group,
levels=c('trop.base', 'trans.bay', 'trans.offshore','trans.temp', 'temp.offshore', 'temp.inshore'))
jpn_sp_site$site.group<-factor(jpn_sp_site$site.group,
levels=c('trop.base', 'trop.island', 'trans.island', 'trans.inland','trans.headld', 'temp.headld'))
jpn_sp_site<-jpn_sp_site%>%filter(FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16)
& ThermalAffinity2=='tropical')
aus_sp_site<-aus_sp_site%>%filter(FG %in% c(15, 10, 8, 2, 6, 12, 4, 1, 16)
& ThermalAffinity2=='tropical')
jpn_sp_site<-left_join(jpn_sp_site, dat_ss%>%dplyr::select(Species, confidence, sst95),
by=c('SpeciesFish'='Species'))
aus_sp_site<-left_join(aus_sp_site, dat_ss%>%dplyr::select(Species, confidence, sst95),
by=c('Fish'='Species'))
ggplot(data=filter(jpn_sp_site, confidence>1))+
geom_boxplot(aes(y=sst95, x=factor(FG)))+facet_wrap(~site.group, scales='free')
ggplot(data=filter(jpn_sp_site, confidence>1))+
geom_boxplot(aes(y=sst95, x=factor(site.group)))+facet_wrap(~FG, scales='free_y')
em1<-filter(jpn_sp_site, confidence>1) %>% group_by(site.group) %>% do(tidy(emmeans(lm(sst95 ~ factor(FG), .), 'FG')))
filter(jpn_sp_site, confidence>1) %>% group_by(site.group) %>% do(tidy(anova(lm(sst95 ~ factor(FG), .) )))
#trop.base = 0.018, trans.island=0.013
p1<-ggplot()+geom_jitter(data=filter(jpn_sp_site, confidence>1),
aes(x=factor(FG),y=sst95), shape=1, alpha=0.5, colour='red', width=0.1)+
geom_pointrange(data=em1, aes(x=factor(FG),y=estimate, ymin=conf.low, ymax=conf.high))+
facet_wrap(~site.group, scales='free', nrow=1)+theme_bw()+
xlab('Functional Group')+ylab('Thermal Midpoint (°C)')+ labs(title ='Japan')
ggplot(data=filter(aus_sp_site, confidence>1))+
geom_boxplot(aes(y=sst95, x=factor(FG)))+facet_wrap(~site.group, scales='free')
ggplot(data=filter(aus_sp_site, confidence>1))+
geom_boxplot(aes(y=sst95, x=factor(site.group)))+facet_wrap(~FG, scales='free_y')
em1<-filter(aus_sp_site, confidence>1) %>% group_by(site.group) %>% do(tidy(emmeans(lm(sst95 ~ factor(FG), .), 'FG')))
filter(aus_sp_site, confidence>1) %>% group_by(site.group) %>% do(tidy(anova(lm(sst95 ~ factor(FG), .) )))
# trop.base=0.005, trans.bay <0.001, temp.inshore <0.001
p2<-ggplot()+geom_jitter(data=filter(aus_sp_site, confidence>1),
aes(x=factor(FG),y=sst95), shape=1, alpha=0.5, colour='red', width=0.1)+
geom_pointrange(data=em1, aes(x=factor(FG),y=estimate, ymin=conf.low, ymax=conf.high))+
facet_wrap(~site.group, scales='free', nrow=1)+theme_bw()+
xlab('Functional Group')+ylab('Thermal Midpoint (°C)')+ labs(title ='Australia')
#png('C:/coral_fish/outputs/themal_midpoint_zone_suppl.png',width =8, height =8 , units ="in", res =600)
grid.arrange(p1, p2, nrow=2)
dev.off()
###########################################################################
#### Trait space setup and data preparation ######
##################################################
# recreate distance matrix from clustering
#order aggregation
dat$Aggregation<-factor(dat$Aggregation, levels=c("solitary", "pairs","groups","schools"), ordered = T)
distlog<-daisy(dat[,c("BodySize","Diet", "Position", "Aggregation", 'DepthRange')],
metric = "gower",stand = FALSE, type = list(logratio = c(1,5)))
func_dudi<-dudi.pco(d = cailliez(distlog, print=TRUE, cor.zero = F), scannf = FALSE, nf = 4)
#func_dudi<-dudi.pco(d = (distlog+3.04421), scannf = FALSE, nf =4)
screeplot(func_dudi)
hist(func_dudi$eig)
func_pco<-data.frame(func_dudi$li,dat)
#### Quality of dendrogram ######
#################################
# Variance explained contribution of PCoA first 40 axes
100* func_dudi$eig[1:40]/sum(func_dudi$eig[func_dudi$eig>0.007])
library(mFD)
d1<-dat
d1$BodySize<-log(dat$BodySize)
d1$DepthRange<-log(dat$DepthRange)
distlog1<-daisy(d1[,c("BodySize","Diet", "Position", "Aggregation", 'DepthRange')], metric = "gower",stand = FALSE)
d1_cat<-data.frame(trait_name =c("BodySize","Diet", "Position", "Aggregation", 'DepthRange'),
trait_type=c('Q','N', 'N', 'O', 'Q' ))
row.names(d1)<-d1$Species
sp_dist_fish <- mFD::funct.dist(
sp_tr = d1[,c("BodySize","Diet", "Position", "Aggregation", 'DepthRange')],
tr_cat = d1_cat,
metric = "gower", stop_if_NA=F)
fish_quality <- mFD::quality.fspaces(
sp_dist = sp_dist_fish,
maxdim_pcoa = 2,
deviation_weighting = "absolute",
fdist_scaling = F,
fdendro = "average")
round(fish_quality$"quality_fspaces", 3)
# plot it
btree<-hclust(distlog, method='average')
copo<-cophenetic(btree)
dl1 <- lower.tri(distlog, diag=TRUE)
cp1 <- lower.tri(copo, diag=TRUE)
df1<-data.frame(gower_d=distlog[dl1],
cophenetic_d=copo[cp1])
ggplot(data=df1)+geom_point(aes(x=gower_d, y=cophenetic_d))
df2<-data.frame(m1=rep(1:nrow(dat), nrow(dat)), m2=sort(rep(1:nrow(dat), nrow(dat))),
gower_d=as.vector(as.matrix(distlog)), copo_d=as.vector(as.matrix(copo)))