-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigure_analysis.R
3196 lines (2921 loc) · 164 KB
/
figure_analysis.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
##################
### LIBRARIRES ### ----
##################
library(rstudioapi) # to set the working directory
library(reshape2) # to format data frames
library(ggpubr)
library(ggrepel) # to use geom_repel (labels)
library(cowplot) # to arrange graphs
library(tidyr) # for the function separate
library(grid) # for rectGrob
#library(gridExtra) # for grid.arrange
library(car) # for Anova
source("figure_settings.R")
setwd(dirname(getActiveDocumentContext()$path)) # Set working directory to source file location
path_potapov<-"Data/Potapov et al 2021 Ecology/DataS1/"
path_hedenec="Data/Hedenec 2022/"
path_figures<-"Figures/"
path_data<-"Data/"
############# ----
# MAIN TEXT # ----
############# ----
# load data # ----
path_results<-"results_final_main/"#"results_23_02_22/"#"results_23_02_03/"#"results_23_01_06/"
data_multi_channel<-Rload_data(path_results,1)
palette_multi_channel<-Rmake_palettes(data_multi_channel$species_traits,
data_multi_channel$n_tot_species,
data_multi_channel$n_faeces)
data_size_structured<-Rload_data(path_results,3)
palette_size_structured<-Rmake_palettes(data_size_structured$species_traits,
data_size_structured$n_tot_species,
data_size_structured$n_faeces)
data_detritivore_spectrum<-Rload_data(path_results,2)
palette_detritivore_spectrum<-Rmake_palettes(data_detritivore_spectrum$species_traits,
data_detritivore_spectrum$n_tot_species,
data_detritivore_spectrum$n_faeces)
# species data
data_species<-rbind(data_multi_channel$data_species,
data_size_structured$data_species)
data_species$trophic_group_file<-as.factor(data_species$trophic_group_file)
levels(data_species$trophic_group_file)=c(label_multichannel,label_size_structured)
# detritus data
data_detritus<-rbind(data_multi_channel$data_detritus,
data_size_structured$data_detritus)
data_detritus$trophic_group_file<-as.factor(data_detritus$trophic_group_file)
levels(data_detritus$trophic_group_file)=c(label_multichannel,label_size_structured)
# detritus production data
production<-rbind(data_multi_channel$data_detritus_production,
data_size_structured$data_detritus_production)
production$trophic_group_file<-as.factor(production$trophic_group_file)
levels(production$trophic_group_file)=c(label_multichannel,label_size_structured)
# temp<-Rload_data(path_results,trophic_group_file)
# list2env(temp,.GlobalEnv)
# temp<-Rmake_palettes(species_traits,n_faeces)
# list2env(temp,.GlobalEnv)
# FOOD WEB STRUCTURE # ----
# Trophic levels and diet # ----
# trophic levels
data<-data_species
data$TL[data$TL==0]=NA
data_potapov<-read.table(paste(path_potapov,"RawData_MassDensityTrophicLevel.csv",sep=""),sep=",",header=T)
data_potapov<-data_potapov[,c("SizeClass","TL")]
data_potapov$TL<-data_potapov$TL-1
data_potapov<-data_potapov %>% separate(SizeClass, c("bodymass",NA), sep = "-")
data_potapov$bodymass[data_potapov$bodymass==">1000"]=1000
data_potapov$bodymass<-as.numeric(data_potapov$bodymass)
levels(data$trophic_group)<-c("microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales")
p1<-ggplot(data=data)+
geom_point(aes(bodymass,TL,colour=trophic_group),size=4)+
palette_multi_channel$scale_colour_line_trophic_groups+
theme+theme(legend.position="bottom",
legend.key.size=unit(30,'pt'))+
guides(colour = guide_legend(reverse=FALSE))
legend<-get_legend(p1)
p1<-ggplot(data=data)+
geom_smooth(aes(bodymass,TL), colour="black",fill="lightgrey", linetype="blank")+
geom_point(aes(bodymass,TL,colour=trophic_group),size=4)+
geom_smooth(data=data_potapov, aes(bodymass,TL), se=FALSE, colour="black", linetype="dashed")+
geom_smooth(aes(bodymass,TL), colour="black", se=FALSE, linetype="solid")+
facet_wrap(~trophic_group_file,nrow=2)+
theme+theme(legend.position="none")+
palette_multi_channel$scale_colour_line_trophic_groups+
scale_x_log10(minor_breaks = 10^seq(-10,5,1),
breaks = 10^(seq(-8,2,2)),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
xlab(label_bodymass)+
ylab(label_TL)
# diet
data<-data_species[data_species$trophic_group_file==label_multichannel,]
diet<-read.table(paste(path_results,"diet_",data$simu_ID[1],".txt",sep=""),sep=",",header=T)
diet$pred<-data_multi_channel$names_species # add the names of the predators
diet$pred<-factor(diet$pred,levels=data_multi_channel$names_species)
data<-data[order(data$names),]
diet[data$TL==0,c(1:(ncol(diet)-1))]=NA # removes the diet of extinct carnivores
diet<-diet[which(data$trophic_type%in%c("microbivores","carnivores")),c(1:data_multi_channel$n_tot_species,ncol(diet))] # keeps only consumers
diet<-Rmake_table_for_plot(diet,"pred","prey","flow") # makes diet a long table
levels(diet$prey)<-data_multi_channel$names_species
survivor<-data$names[data$TL>0 & data$trophic_type%in%c("microbivores","carnivores")]
diet<-diet[diet$pred%in%survivor,]
# sum of prey biomass by trophic group
temp<-data_species[,c("names","trophic_group")]
names(temp)[1]="prey"
diet<-merge(diet,temp,by="prey")
diet$prey<-NULL
names(diet)[3]="prey"
names(temp)[1]="pred"
diet<-merge(diet,temp,by="pred")
diet<-aggregate(flow~pred+prey+trophic_group, data=diet, sum)
n_micro<-length(which(data$names[data$trophic_group%in%c("micro-food web microbivores","micro-food web carnivores")]%in%survivor))
n_macro<-length(which(data$names[data$trophic_group%in%c("macro-food web carnivores")]%in%survivor))
p2<-ggplot(data=diet)+
geom_bar(aes(pred,flow,fill=prey),stat="identity",position="fill")+
geom_point(aes(pred,-0.05,colour=trophic_group),size=4)+
annotate("errorbarh",y=1.03,xmin=0.7,xmax=n_micro+0.3,height = 0.05)+
annotate("errorbarh",y=1.03,xmin=n_micro+0.7,xmax=n_micro+n_macro+0.45,height = 0.05)+
annotate("text",x=n_micro/2,y=1.08,label="micro-food web",family="serif",size=6,angle=-90)+
annotate("text",x=n_micro+n_macro/2,y=1.08,label="macro-food web",family="serif",size=6,angle=-90)+
theme+theme(legend.position="none",
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.y = element_line(arrow = arrow(angle = 15, length = unit(.3,"inches"),type = "closed")),
#axis.line.y=element_blank(),
panel.grid=element_blank())+
palette_multi_channel$scale_colour_line_trophic_groups_consumers+
palette_multi_channel$scale_colour_bar_trophic_groups+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
coord_flip()+
xlab("Increasing body\nsize of predators")+
ylab(label_diet)
legend_height=0.15
graph<-ggdraw(xlim = c(0, 2), ylim = c(0, 1+legend_height)) +
draw_plot(p1, 0, legend_height, 1, 1)+
draw_plot(p2, 1, legend_height, 1, 1)+
draw_plot(legend, 0, 0, 2, legend_height)+
draw_plot_label(c("A","B"), c(0,1), c(1,1)+legend_height, size = 30)
ggsave(paste(path_figures,"figure_TL_diet.pdf",sep=""), graph, width = 12, height = 8, device=cairo_pdf)
# ECOSYSTEM FUNCTIONING # ----
# Decomposition and respiration # ----
# detritus stocks
data<-aggregate(biomass~type+input_FOM+input_DOC+trophic_group_file, data=data_detritus, sum)
p1<-ggplot(data)+
#geom_col(aes(type,biomass,fill=type))+
geom_rect(aes(xmin=as.numeric(type)-0.45,xmax=as.numeric(type)+0.45,ymin=min(biomass)*0.5,ymax=biomass,fill=type))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
palette_multi_channel$scale_colour_bar_detritus_classes+
scale_x_continuous(breaks=1:length(levels(data$type)),
labels=levels(data$type))+
y_axis_log10+
ylab(label_stock)
data<-rbind(data_multi_channel$data_detritus,
data_size_structured$data_detritus)
data$trophic_group_file<-as.factor(data$trophic_group_file)
levels(data$trophic_group_file)=c(label_multichannel,label_size_structured)
data<-aggregate(biomass~type+trophic_group_file, data=data, sum)
data<-data[data$type=="FOM",]
data$biomass[data$trophic_group_file==label_multichannel]/data$biomass[data$trophic_group_file==label_size_structured]
# decomposition by each decomposer trophic group
consumption=NULL
# multichannel
temp<-read.table(paste(path_results,"consumption_",1,".txt",sep=""),sep=",",header=T)
names(temp)<-data_multi_channel$names_tot
temp<-temp[,data_multi_channel$names_detritus] # selects abiotic resources only
temp$consumer<-data_multi_channel$names_species
temp$trophic_group_file<-label_multichannel
temp<-Rmake_table_for_plot(temp,c("consumer","trophic_group_file"),"resource","biomass") # makes diet a long table
consumption<-rbind(consumption,temp)
# size-structured
temp<-read.table(paste(path_results,"consumption_",3,".txt",sep=""),sep=",",header=T)
names(temp)<-data_size_structured$names_tot
temp<-temp[,data_size_structured$names_detritus] # selects abiotic resources only
temp$consumer<-data_size_structured$names_species
temp$trophic_group_file<-label_size_structured
temp<-Rmake_table_for_plot(temp,c("consumer","trophic_group_file"),"resource","biomass") # makes diet a long table
consumption<-rbind(consumption,temp)
# resource type
temp<-data_multi_channel$detritus_traits[,c("type","names")]
names(temp)<-c("type","resource")
consumption<-merge(consumption,temp,by="resource")
# consumer trophic group
temp<-data_multi_channel$species_traits[,c("trophic_group","names")]
names(temp)<-c("trophic_group","consumer")
consumption<-merge(consumption,temp,by="consumer")
rm(temp)
# only consider decomposers
consumption<-consumption[consumption$trophic_group%in%c("microbes","macro-food web detritivores","trophic whales"),]
consumption<-aggregate(biomass~type+trophic_group+trophic_group_file, data=consumption, sum)
consumption$trophic_group<-factor(consumption$trophic_group,levels=c("microbes","macro-food web detritivores","trophic whales"))
levels(consumption$trophic_group)<-c("microbes","macro-detritivores","trophic whales")
consumption$type<-factor(consumption$type,levels=c("faeces","FOM","SOM","DOC"))
p2<-ggplot(data=consumption)+
geom_bar(aes(type,biomass,fill=trophic_group),stat="identity",position=position_stack(reverse = TRUE))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
#legend.position=c(1, 1),
#legend.justification=c(1, 1),
axis.title.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
palette_multi_channel$scale_colour_bar_trophic_groups_detritivores+
ylab(label_decomposition)
# detritus production
data<-aggregate(production~type+trophic_group_file, data=production, sum)
data$type[data$type%in%c("FOM","DOC")]<-NA
data<-data[data$type%in%c("faeces","SOM"),]
data$type<-droplevels(data$type)
#production$production[production$production==0]=NA
p3<-ggplot(data)+
geom_rect(aes(xmin=as.numeric(type)-0.45,xmax=as.numeric(type)+0.45,ymin=min(production)*0.5,ymax=production,fill=type))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
palette_multi_channel$scale_colour_bar_detritus_production+
scale_x_continuous(breaks=1:length(levels(data$type)),
labels=levels(data$type))+
#y_axis_log10+
ylab(label_production)
# respiration
data<-aggregate(respiration~trophic_group+input_FOM+input_DOC+trophic_group_file, data=data_species, sum)
levels(data$trophic_group)<-c("microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales")
p4<-ggplot(data)+
geom_bar(aes(1,respiration,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
theme+
palette_multi_channel$scale_colour_bar_trophic_groups
legend_1<-get_legend(p4)
p4<-ggplot(data)+
geom_bar(aes(1,respiration,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
palette_multi_channel$scale_colour_bar_trophic_groups+
scale_y_continuous(labels = scales::percent)+
ylab(label_respiration_fraction)
data$relative<-0
data$relative[data$trophic_group_file==label_multichannel]<-data$respiration[data$trophic_group_file==label_multichannel]/sum(data$respiration[data$trophic_group_file==label_multichannel])
data$relative[data$trophic_group_file==label_size_structured]<-data$respiration[data$trophic_group_file==label_size_structured]/sum(data$respiration[data$trophic_group_file==label_size_structured])
data$relative<-round(data$relative,3)*100
# Respiration distribution empiric # ----
### Calculation of metabolic parameters
# data from Johnston and Sibly (2018) (10.1038/s41559-018-0648-6)
joule_to_carbon=1/20.1*0.5363
hour_to_day=1/24
celsius_to_kelvin=273.15
kB=8.62e-5 # Bolzmann's constant
fresh_to_dry=0.2 # Makarieva et al. (2005), Ehnes et al. (2011) Johnston and Sibly (2018)
dry_to_C=0.42 # Andrieux et al. (2021) (for animals and litter)
# metabolic rate : J.h-1
# body mass : mg fresh weight
data_metabo<-read.csv(file=paste(path_data,"Metabolic_rates_Johnston_2018/JohnstonSiblySoilBiotaMetabolicData.csv",sep=""), header=TRUE)
names(data_metabo)<-c("study","Fauna_group","taxon","metabolic_rate","bodymass","temperature")
# conversion
data_metabo$temperature_arrhenius<--1/(celsius_to_kelvin+data_metabo$temperature)*1/kB
data_metabo$metabolic_rate<-data_metabo$metabolic_rate*joule_to_carbon/hour_to_day
data_metabo$bodymass<-data_metabo$bodymass*fresh_to_dry*dry_to_C
data_metabo$metabolic_rate<-data_metabo$metabolic_rate/data_metabo$bodymass # conversion into mass metabolic rate
model=lm(data=data_metabo,
formula=log(metabolic_rate)~Fauna_group+log(bodymass):Fauna_group+temperature_arrhenius:Fauna_group)
params<-summary(model)
params
Anova(model,type=2,test.statisticf="F")
params<-(params$coefficients)[,1]
names(params)[1]="Fauna_groupMacrofauna"
params[2]=params[2]+params[1] # total effect for mesofauna
params[3]=params[3]+params[1] # total effect for microbes
params<-as.data.frame(params)
params$names<-row.names(params)
params<-params %>% separate(names,c("Fauna_group","parameter"),sep=":",fill="right")
params<-params %>% separate(Fauna_group,c(NA,"Fauna_group"),sep=11)
params$parameter[is.na(params$parameter)]="R0"
params$parameter<-as.factor(params$parameter)
levels(params$parameter)=c("s_R","R0","E_R")
params<-dcast(params,Fauna_group~parameter,value.var="params")
### Calculation of the respiration of each sub-food web
data_abundance<-read.table(paste0(path_data,"Metabolic_rates_Johnston_2018/JohnstonSiblySoilBiotaAbundanceData.csv"),header=T,sep=",")
data_bodymass<-read.table(paste0(path_data,"Metabolic_rates_Johnston_2018/JohnstonSiblySoilGroupIndividualBodymass.csv"),header=T,sep=",")
Mkm2_to_m2=1e6*1e6 # million km² to km²
Pg_to_mg=1e18 # petagram to milligram
# mean body mass of the main soil taxa
data_bodymass$bodymass<-data_bodymass$Individual_bodymass_mg_DM*dry_to_C # body
data_bodymass$bodymass_fresh<-data_bodymass$Individual_bodymass_mg_DM/fresh_to_dry
data_bodymass$trophic_group<-"macro-food web"
data_bodymass$trophic_group[data_bodymass$bodymass_fresh<0.001]="micro-food web"
data_bodymass$trophic_group[data_bodymass$Soil_biota_group=="Bacteria"]<-"microbes"
data_bodymass$Individual_bodymass_mg_DM<-NULL
# abundance of the main soil fauna taxa
data_abundance<-data_abundance[,c("Biome_ecosystem","Soil_biota_group","Biomass_g_m2")]
data_abundance$biomass_density<-data_abundance$Biomass_g_m2*1e3*fresh_to_dry*dry_to_C # conversion into mgC
data_abundance$Biomass_g_m2<-NULL
# abundance of soil microbes
microbes<-read.table("Data/Metabolic_rates_Johnston_2018/Xu_2013_Global_Ecology_and_Biogeography_aggregated_data.csv",header=T,sep=",") # data from the main text of Xu et al 2013 Global Ecology and Biogeography
microbes$biomass_density<-microbes$soil_microbial_C_Pg_0_30_cm*Pg_to_mg/(microbes$Area_Mkm2*Mkm2_to_m2)
microbes$Soil_biota_group="Bacteria"
microbes<-microbes[,names(data_abundance)]
# final data set
data<-rbind(data_abundance,microbes)
rm(data_abundance,microbes)
data<-merge(data,data_bodymass,by=c("Soil_biota_group"))
data$trophic_group<-factor(data$trophic_group,levels=c("microbes","micro-food web","macro-food web"))
data<-data[data$Biome_ecosystem%in%c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"),]
data$Biome_ecosystem<-factor(data$Biome_ecosystem,levels=c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"))
levels(data$Biome_ecosystem)<-c("Tundra","Boreal\nforest","Temperate\nforest","Temperate\ngrassland","Tropical\nforest")
data<-merge(data,params,by=c("Fauna_group")) # add the metabolic parameters
# respiration
data$metabolism<-exp(data$R0)*(data$bodymass^data$s_R)*exp(-data$E_R/(kB*(celsius_to_kelvin+15)))
data$respiration<-data$metabolism*data$biomass_density
data<-aggregate(respiration~Biome_ecosystem+trophic_group+Soil_biota_group,data,mean)
data<-aggregate(respiration~trophic_group,data,sum)
data$trophic_group_file="Empirical data"
p5<-ggplot(data=data)+
geom_col(aes(1,respiration,fill=trophic_group),position=position_fill(reverse=TRUE))+
theme+theme(legend.title=element_blank())+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))
legend_2<-get_legend(p5)
p5<-ggplot(data=data)+
geom_col(aes(1,respiration,fill=trophic_group),position=position_fill(reverse=TRUE))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
legend.title=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
xlab("Biome")+
ylab(label_respiration_fraction)
data$percent<-data$respiration/sum(data$respiration)*100
### Final graph #----
h_logo=0.1
graph<-ggdraw(xlim = c(0, 3), ylim = c(0, 2+2*h_logo)) +
draw_plot(p1, 0, 1+2*h_logo, 1.5, 1)+
draw_plot(p2, 1.5, 1+2*h_logo, 1.5, 1)+
draw_plot(p3, 0, h_logo, 0.9, 1)+
draw_plot(p4, 0.9, h_logo, 1, 1)+
draw_plot(legend_2, 1.9, h_logo, 0.5, 0.4)+
draw_plot(legend_1, 1.9, 0.4+h_logo, 0.5, 0.4)+
draw_plot(p5, 2.4, h_logo, 0.6, 1)+
draw_plot_label(c("A","B","C","D","E"), c(0,1.5,0,0.9,2.3), c(2,2,1,1,1)*(1+h_logo), size = 30)
ggsave(paste(path_figures,"figure_detritus_respiration.pdf",sep=""), graph, width = 14, height = 10, device=cairo_pdf)
# graph<-ggdraw(xlim = c(0, 2.7), ylim = c(0, 2.15)) +
# draw_plot(p1, 0, 1.15, 1, 1)+
# draw_plot(p2, 1.05, 1.15, 1, 1)+
# draw_plot(p3, 2.1, 1.15, 0.6, 1)+
# draw_plot(p4, 0, 0, 1.5, 1)+
# draw_plot(p5, 1.5, 0, 1.1, 1)+
# draw_plot_label(c("A","B","C","D","E"), c(0,0.95,2,0,1.4), c(2.15,2.15,2.15,1.02,1.02), size = 30)
# ggsave(paste(path_figures,"figure_detritus_respiration.pdf",sep=""), graph, width = 12, height = 9, device=cairo_pdf)
# Stock ratios (Table 1) # ----
data<-aggregate(biomass~trophic_group+trophic_group_file, data=data_species, sum)
data<-data[data$trophic_group=="microbes",]
temp<-aggregate(biomass~type+trophic_group_file, data=rbind(data_multi_channel$data_detritus,
data_size_structured$data_detritus), sum)
temp$trophic_group_file<-as.factor(temp$trophic_group_file)
levels(temp$trophic_group_file)=c(label_multichannel,label_size_structured)
names(temp)[1]="trophic_group"
data<-rbind(data,temp)
rm(temp)
data<-dcast(data, trophic_group_file ~ trophic_group, value.var="biomass")
data$ratio_C<-data$microbes/(data$SOM+data$microbes)*100
data$ratio_detritus<-data$FOM/(data$SOM+data$FOM)*100
ratio<-data[,c(2:ncol(data))]
ratio[1,]<-ratio[1,]/ratio[2,]
# total soil C according to Xu et al. 2013, total value in table 4
area=128.3e6 # km²
area=area*1e6 # conversion to m²
C_mic=16.72 # Pg C
C_mic=C_mic*1e18 # conversion to mg
C_SOM=C_mic/area*(1/0.012-1)
data_FOM<-read.table(paste0(path_data,"Hedenec 2022/litter.csv"),header=T,sep=",") # data of FOM and DOC inputs in the main land ecosystems
data_FOM<-data_FOM[,c(1,4)]
dry_to_C=0.42 # Andrieux et al. (2021) (for animals and litter)
data_FOM$litter_stock<-data_FOM$litter_stock_kg_ha*dry_to_C*1e6/1e4 # conversion to mg C per m²
C_FOM=mean(data_FOM$litter_stock)
C_FOM/(C_SOM+C_FOM)*100
# PREDICTION OF BIOMASS DISTRIBUTION # ----
# Biomass distribution model # ----
data<-data_species
data$biomass[data$biomass<1e-7]=NA
data<-aggregate(biomass~trophic_group+input_FOM+input_DOC+trophic_group_file, data=data, sum)
levels(data$trophic_group)<-c("microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales")
# raw biomass per trophic group
p1<-ggplot(data)+
#geom_bar(aes(trophic_group,biomass,fill=trophic_group),stat="identity")+
geom_rect(aes(xmin=as.numeric(trophic_group)-0.5,xmax=as.numeric(trophic_group)+0.5,ymin=min(biomass,na.rm=TRUE)*0.5,ymax=biomass,fill=trophic_group))+
facet_wrap(~trophic_group_file)+
theme+theme(#legend.position="bottom",
#legend.direction="vertical",
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank())+
palette_multi_channel$scale_colour_bar_trophic_groups+
#guides(fill=guide_legend(nrow=3,reverse=TRUE))+
scale_x_continuous(labels=levels(data$trophic_group), breaks=c(1:length(levels(data$trophic_group))))+
coord_flip()+
scale_y_log10(breaks = 10^seq(-1,3,1),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
ylab(label_biomass)
# relative biomass distribution
data$relative<-0
data$relative[data$trophic_group_file==label_multichannel]<-data$biomass[data$trophic_group_file==label_multichannel]/sum(data$biomass[data$trophic_group_file==label_multichannel])
data$relative[data$trophic_group_file==label_size_structured]<-data$biomass[data$trophic_group_file==label_size_structured]/sum(data$biomass[data$trophic_group_file==label_size_structured])
data$relative<-data$relative*100
p2<-ggplot(data=data)+
geom_bar(aes(1,biomass,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.line.y=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
palette_multi_channel$scale_colour_bar_trophic_groups+
scale_y_continuous(labels = scales::percent)+
ylab(label_biomass_fraction)
data_model<-data # stores the modelling results for comparison with empirical data
# Biomass distribution empiric # ----
# Johnston and Sibly 2018 Nature Ecology and Evolution
data_abundance<-read.table("Data/Metabolic_rates_Johnston_2018/JohnstonSiblySoilBiotaAbundanceData.csv",header=T,sep=",")
data_bodymass<-read.table("Data/Metabolic_rates_Johnston_2018/JohnstonSiblySoilGroupIndividualBodymass.csv",header=T,sep=",")
fresh_to_dry=0.2 # Makarieva et al. (2005), Ehnes et al. (2011) Johnston and Sibly (2018)
dry_to_C=0.42 # Andrieux et al. (2021) (for animals and litter)
Mkm2_to_m2=1e6*1e6 # million km² to km²
Pg_to_mg=1e18 # petagram to milligram
data_bodymass$Individual_bodymass_mg_FM<-data_bodymass$Individual_bodymass_mg_DM/fresh_to_dry
data_bodymass$trophic_group<-"macro-food web"
data_bodymass$trophic_group[data_bodymass$Individual_bodymass_mg_FM<0.001]="micro-food web"
data_abundance<-merge(data_abundance,data_bodymass,by=c("Soil_biota_group"))
data_abundance$Individual_bodymass_mg_DM<-data_abundance$Individual_bodymass_mg_DM*dry_to_C # conversion into mgC
data_abundance$biomass_density<-data_abundance$Biomass_g_m2*1e3*fresh_to_dry*dry_to_C # conversion into mgC
data_abundance<-data_abundance[,c("Soil_biota_group","Biome_ecosystem","Biomass_g_m2","Individual_bodymass_mg_DM","trophic_group","biomass_density")]
# bacteria<-data.frame(Soil_biota_group="Bacteria",
# Biome_ecosystem=c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"),
# Biomass_g_m2=c(22.8,23.5,22.6,22.4,22.3), # ln(Individuals) actually
# Individual_bodymass_mg_DM=2.83e-8*dry_to_C, # converted into C body mass
# trophic_group="microbes")
# bacteria$biomass_density<-exp(bacteria$Biomass_g_m2)*bacteria$Individual_bodymass_mg_DM
microbes<-read.table("Data/Metabolic_rates_Johnston_2018/Xu_2013_Global_Ecology_and_Biogeography_aggregated_data.csv",header=T,sep=",") # data from the main text of Xu et al 2013 Global Ecology and Biogeography
microbes$biomass_density<-microbes$soil_microbial_C_Pg_0_30_cm*Pg_to_mg/(microbes$Area_Mkm2*Mkm2_to_m2)
microbes$Soil_biota_group="microbes"
microbes$trophic_group="microbes"
data_abundance<-rbind(data_abundance[,c("Soil_biota_group","Biome_ecosystem","trophic_group","biomass_density")],
microbes[,c("Soil_biota_group","Biome_ecosystem","trophic_group","biomass_density")])
data_abundance$trophic_group<-factor(data_abundance$trophic_group,levels=c("microbes","micro-food web","macro-food web"))
data_abundance<-data_abundance[data_abundance$Biome_ecosystem%in%c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"),]
data_abundance$Biome_ecosystem<-factor(data_abundance$Biome_ecosystem,levels=c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"))
levels(data_abundance$Biome_ecosystem)<-c("Tundra","Boreal\nforest","Temperate\nforest","Temperate\ngrassland","Tropical\nforest")
data<-aggregate(biomass_density~trophic_group+Soil_biota_group,data_abundance,mean)
data<-aggregate(biomass_density~trophic_group,data,sum)
data$facet="Empirical data"
# p4<-ggplot(data=data)+
# geom_bar(aes(1,biomass_density,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
# theme+theme(legend.title=element_blank(),
# legend.justification = "left")+
# scale_fill_manual(values=c("lightgrey","darkgrey","black"),
# guide = guide_legend(reverse=TRUE))
# legend_1<-get_legend(p4)
p4<-ggplot(data=data)+
geom_bar(aes(1,biomass_density,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
facet_wrap(~facet)+
theme+theme(#legend.position="none",
legend.title=element_blank(),
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.line.y=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
ylab(label_biomass_fraction)
p4_zoom<-ggplot(data=data)+
geom_bar(aes(1,biomass_density,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
theme+theme(legend.position="none",
legend.title=element_blank(),
axis.title=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.line.x=element_blank(),
axis.text = element_text(size=15),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.background=element_rect(colour = "black", fill=NA, linetype="solid"))+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))+
scale_y_continuous(breaks=seq(0.945,0.95,0.0025),labels = scales::percent,
limits=c(0.945,0.95),oob = rescale_none,position = "right")+
ylab(label_biomass_fraction)
data$persent<-data$biomass_density/sum(data$biomass_density)*100
# Heděnec et al 2022 Scientific Reports
data_hedenec<-read.table(paste(path_hedenec,"biomass_invertebrates_raw.csv",sep=""),sep=",",header=T)
data_hedenec_trophic_groups<-read.table(paste(path_hedenec,"invertebrates_trophic_groups.csv",sep=""),sep=",",header=T)
data_hedenec<-merge(data_hedenec,data_hedenec_trophic_groups,by=c("Guilds","Fauna"))
rm(data_hedenec_trophic_groups)
data<-aggregate(Biomass~trophic_type+size_class, data=data_hedenec, sum)
data$size_class<-factor(data$size_class,levels=c("micro-foodweb","macro-foodweb"))
levels(data$size_class)=c("micro-food web","macro-food web")
data$trophic_type<-factor(data$trophic_type,levels=c("carnivores","herbivores","detritivores","omnivores","microbivores"))
data<-data[data$size_class=="macro-food web",]
data$Biomass[data$trophic_type=="carnivores"]=data$Biomass[data$trophic_type=="carnivores"]+data$Biomass[data$trophic_type=="omnivores"]
data<-data[-which(data$trophic_type%in%c("herbivores","omnivores")),]
data$facet="Empirical data"
data_model<-data_model[,c("trophic_group","trophic_group_file","biomass")]
names(data_model)=c("trophic_type","facet","Biomass")
data_model<-data_model[data_model$facet=="Multichannel",]
data_model$size_class="macro-food web"
data_model$Biomass[4]<-data_model$Biomass[4]+data_model$Biomass[6]
data_model<-data_model[c(4,5),c(1,4,3,2)]
data_model$trophic_type=c("detritivores","carnivores")
data<-rbind(data,data_model)
p3<-ggplot(data=data[data$size_class=="macro-food web",])+
geom_bar(aes(size_class,Biomass,fill=trophic_type),stat="identity",position=position_fill())+
theme+theme(legend.title=element_blank())+
scale_fill_manual(values=c("red2","slateblue1","seagreen2"))
legend<-get_legend(p3)
p3<-ggplot(data=data[data$size_class=="macro-food web",])+
geom_bar(aes(size_class,Biomass,fill=trophic_type),stat="identity",position=position_fill())+
facet_wrap(~facet)+
theme+theme(legend.position="none",
legend.title=element_blank(),
axis.ticks.x=element_blank(),
#axis.title.x=element_blank(),
axis.text.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank())+
scale_fill_manual(values=c("red2","slateblue1","seagreen2"))+
scale_x_discrete(expand = c(0,0.5))+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
#xlab("Food web")+
xlab("Macro-food web")+
ylab(label_biomass_fraction)
### Final graph #----
temp<-plot_grid(p3, NULL, p2, p4, labels = NULL, align = "h", rel_widths = c(0.7, 0.1, 0.55, 0.65),nrow=1)
graph<-ggdraw(xlim = c(0,2), ylim = c(0,2)) +
draw_plot(legend, 0.05, 1.05, 0.3, 0.3)+
draw_plot(p1, 0.4, 1, 1.5, 1)+
draw_plot(temp, 0, 0, 2, 1)+
#draw_plot(p3, 0, 0, 0.7, 1)+
#draw_plot(p2, 0.8, 0, 0.55, 1)+
#draw_plot(p4, 1.33, 0, 0.65, 1)+
draw_plot(p4_zoom, 1.71, 0.65, 0.25, 0.3)+
draw_plot_label(c("A","B","C"), c(0.35,0,0.75), c(2,1.03,1.03), size = 25)
ggsave(paste(path_figures,"figure_biomass.pdf",sep=""), graph, width = 12, height = 8, device=cairo_pdf)
# ROBUSTNESS OVER ENVIRONMENTAL GRADIENTS # ----
# Predicted biomass distribution in each biome # ----
path_results<-"results_final_FOM_SOM/"#"results_23_02_06/"
data_multi_channel<-Rload_data(path_results,1)
data_size_structured<-Rload_data(path_results,3)
palette_multi_channel<-Rmake_palettes(data_multi_channel$species_traits,
data_multi_channel$n_tot_species,
data_multi_channel$n_faeces)
data_species<-rbind(data_multi_channel$data_species,
data_size_structured$data_species)
data_species$trophic_group_file<-as.factor(data_species$trophic_group_file)
levels(data_species$trophic_group_file)=c(label_multichannel,label_size_structured)
data<-aggregate(biomass~trophic_group+trophic_group_file+input_FOM+input_DOC, data=data_species, sum, na.rm = TRUE)
#data$biomass[data$biomass<1e-7]=NA
data$foodweb<-"micro-food web"
data$foodweb[data$trophic_group%in%c("macro-food web detritivores","macro-food web carnivores","trophic whales")]<-"macro-food web"
input_DOC<-unique(data$input_DOC)
input_FOM<-unique(data$input_FOM)
data_FOM_DOC<-read.table(paste0(path_data,"data_FOM_SOM.csv"),header=T,sep=",") # data of FOM and DOC inputs in the main land ecosystems
for (i in 1:nrow(data_FOM_DOC)){
data_FOM_DOC$input_DOC[i]<-input_DOC[which.min(abs(input_DOC-data_FOM_DOC$input_DOC[i]))] # place the values by the nearest values used for the simulations
data_FOM_DOC$input_FOM[i]<-input_FOM[which.min(abs(input_FOM-data_FOM_DOC$input_FOM[i]))]
}
data<-merge(data,data_FOM_DOC,by=c("input_DOC","input_FOM"))
levels(data$trophic_group)<-c("microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales")
data$Biome<-factor(data$Biome,levels=c("Desert","Tundra","Boreal forest","Temperate forest","Mediterranean vegetation","Temperate grassland","Tropical grassland","Tropical forest"))
data<-data[data$Biome%in%c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"),]
data$Biome<-droplevels(data$Biome)
levels(data$Biome)<-c("Tundra","Boreal\nforest","Temperate\nforest","Temperate\ngrassland","Tropical\nforest")
p1<-ggplot(data=data)+
geom_bar(aes(Biome,biomass,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
theme+theme(legend.margin=margin(l = 0, unit='pt'))+
palette_multi_channel$scale_colour_bar_trophic_groups
legend_1<-get_legend(p1)
p1<-ggplot(data=data)+
geom_bar(aes(Biome,biomass,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
facet_wrap(~trophic_group_file)+
theme+theme(legend.position="none",
axis.text.y=element_text(size=15),
axis.title.y=element_blank(),
axis.title.x=element_text(hjust=1),
panel.grid=element_blank(),
panel.spacing.x = unit(1.5, "lines"))+
palette_multi_channel$scale_colour_bar_trophic_groups+
coord_flip()+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
ylab(label_biomass_fraction)
# Biomass empiric #----
data_abundance<-read.table("Data/Metabolic_rates_Johnston_2018/JohnstonSiblySoilBiotaAbundanceData.csv",header=T,sep=",")
data_bodymass<-read.table("Data/Metabolic_rates_Johnston_2018/JohnstonSiblySoilGroupIndividualBodymass.csv",header=T,sep=",")
fresh_to_dry=0.2 # Makarieva et al. (2005), Ehnes et al. (2011) Johnston and Sibly (2018)
dry_to_C=0.42 # Andrieux et al. (2021) (for animals and litter)
Mkm2_to_m2=1e6*1e6 # million km² to km²
Pg_to_mg=1e18 # petagram to milligram
data_bodymass$Individual_bodymass_mg_FM<-data_bodymass$Individual_bodymass_mg_DM/fresh_to_dry
data_bodymass$trophic_group<-"macro-food web"
data_bodymass$trophic_group[data_bodymass$Individual_bodymass_mg_FM<0.001]="micro-food web"
data_abundance<-merge(data_abundance,data_bodymass,by=c("Soil_biota_group"))
data_abundance$Individual_bodymass_mg_DM<-data_abundance$Individual_bodymass_mg_DM*fresh_to_dry*dry_to_C # conversion into mgC
data_abundance$biomass_density<-data_abundance$Biomass_g_m2*1e3*fresh_to_dry*dry_to_C # conversion into mgC
data_abundance<-data_abundance[,c("Soil_biota_group","Biome_ecosystem","Biomass_g_m2","Individual_bodymass_mg_DM","trophic_group","biomass_density")]
microbes<-read.table("Data/Metabolic_rates_Johnston_2018/Xu_2013_Global_Ecology_and_Biogeography_aggregated_data.csv",header=T,sep=",") # data from the main text of Xu et al 2013 Global Ecology and Biogeography
microbes$biomass_density<-microbes$soil_microbial_C_Pg_0_30_cm*Pg_to_mg/(microbes$Area_Mkm2*Mkm2_to_m2)
microbes$Soil_biota_group="microbes"
microbes$trophic_group="microbes"
data_abundance<-rbind(data_abundance[,c("Soil_biota_group","Biome_ecosystem","trophic_group","biomass_density")],
microbes[,c("Soil_biota_group","Biome_ecosystem","trophic_group","biomass_density")])
data_abundance$trophic_group<-factor(data_abundance$trophic_group,levels=c("microbes","micro-food web","macro-food web"))
data_abundance<-data_abundance[data_abundance$Biome_ecosystem%in%c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"),]
data_abundance$Biome_ecosystem<-factor(data_abundance$Biome_ecosystem,levels=c("Tundra","Boreal forest","Temperate forest","Temperate grassland","Tropical forest"))
levels(data_abundance$Biome_ecosystem)<-c("Tundra","Boreal\nforest","Temperate\nforest","Temperate\ngrassland","Tropical\nforest")
data<-aggregate(biomass_density~Biome_ecosystem+trophic_group+Soil_biota_group,data_abundance,mean)
data<-aggregate(biomass_density~Biome_ecosystem+trophic_group,data,sum)
data$facet="Empirical data"
p2<-ggplot(data=data)+
geom_bar(aes(Biome_ecosystem,biomass_density,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
theme+theme(legend.title=element_blank(),
legend.margin=margin(l = 0, unit='pt'))+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))
legend_2<-get_legend(p2)
p2<-ggplot(data=data)+
geom_bar(aes(Biome_ecosystem,biomass_density,fill=trophic_group),stat="identity",position=position_fill(reverse = TRUE))+
facet_wrap(~facet)+
theme+theme(legend.position="none",
axis.title=element_blank(),
axis.text.y=element_blank(),
axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid=element_blank())+
scale_fill_manual(values=c("lightgrey","darkgrey","black"),
guide = guide_legend(reverse=TRUE))+
coord_flip()+
scale_y_continuous(breaks=seq(0,1,0.25),labels = scales::percent)+
ylab(label_biomass_fraction)
### Final graph #----
temp<-plot_grid(p1,NULL, p2, labels = NULL, align = "h", rel_widths = c(2,0.05, 0.8),nrow=1)
graph<-ggdraw(xlim = c(0,3.6), ylim = c(0,1)) +
#draw_plot(p1, 0, 0, 2, 1)+
#draw_plot(p2, 2, 0, 0.9, 1)+
draw_plot(temp, 0, 0, 2.9, 1)+
draw_plot(legend_1, 2.9, 0.5, 0.75, 0.3)+
draw_plot(legend_2, 2.9, 0.1, 0.7, 0.3)
ggsave(paste(path_figures,"figure_biomass_biome.pdf",sep=""), graph, width = 12, height = 4, device=cairo_pdf)
########################## ----
# SUPPORTING INFORMATION # ----
########################## ----
# load data # ----
path_results<-"results_final_main/"#"results_23_02_15/"#"results_23_02_03/"#"results_23_01_06/"
data_multi_channel<-Rload_data(path_results,1)
palette_multi_channel<-Rmake_palettes(data_multi_channel$species_traits,
data_multi_channel$n_tot_species,
data_multi_channel$n_faeces)
data_size_structured<-Rload_data(path_results,3)
palette_size_structured<-Rmake_palettes(data_size_structured$species_traits,
data_size_structured$n_tot_species,
data_size_structured$n_faeces)
data_detritivore_spectrum<-Rload_data(path_results,2)
palette_detritivore_spectrum<-Rmake_palettes(data_detritivore_spectrum$species_traits,
data_detritivore_spectrum$n_tot_species,
data_detritivore_spectrum$n_faeces)
params<-read.table(paste0(path_results,"parameters_simulation.txt"),sep=",",header=TRUE)
# species data
data_species<-rbind(data_multi_channel$data_species,
data_size_structured$data_species)
data_species$trophic_group_file<-as.factor(data_species$trophic_group_file)
levels(data_species$trophic_group_file)=c(label_multichannel,label_size_structured)
# detritus data
data_detritus<-data_multi_channel$data_detritus
# detritus production data
production<-data_multi_channel$data_detritus_production
# Food web diagrams # ----
get_data_foodweb<-function(input_FOM,input_DOC,data_results,palette){
with(data_results,{
data<-data_species[data_species$input_FOM==input_FOM & data_species$input_DOC==input_DOC,]
# changes the position of macro-food web carnivores to have them above detritivores
if (length(data$trophic_group=="macro-food web detritivores")>0){
data$num[data$trophic_group=="macro-food web carnivores"]=data$num[data$trophic_group=="macro-food web carnivores"]-length(data$trophic_group[data$trophic_group=="macro-food web detritivores"])
}
diet<-read.table(paste(path_results,"diet_",data$simu_ID[1],".txt",sep=""),sep=",",header=T)
# remove irrelevant species (almoast extinct)
diet[data$num[data$TL==0],]=NA
diet[,data$num[data$TL==0]]=NA
diet$pred<-names_species
diet<-Rmake_table_for_plot(diet,"pred","prey","flow") # makes diet a long table
levels(diet$prey)<-names_tot
# add the information on prey
data_prey<-data[,c("bodymass","TL","names","num")]
names(data_prey)<-c("bodymass_prey","TL_prey","prey","num_prey")
diet<-merge(diet,data_prey,by=c("prey"),all=T) # add the information on prey to the data set
diet$TL_prey[is.na(diet$TL_prey)]=0 # detritus
diet$bodymass_prey[is.na(diet$bodymass_prey)]=0 # body mass
names_abio<-c("DOC","SOM",names_faeces,"FOM","N")
for (i in 1:(n_tot_detritus+n_nutrients)){
diet$num_prey[diet$prey==names_abio[i]]=i # sets the position of detritus
}
plus_SOM=6
plus_faeces=10
plus_FOM=18
plus_N=26
diet$num_prey[diet$prey=="SOM"]=diet$num_prey[diet$prey=="SOM"]+plus_SOM # to make the dots more readable
diet$num_prey[which(diet$prey%in%names_faeces)]=diet$num_prey[which(diet$prey%in%names_faeces)]+plus_faeces
diet$num_prey[diet$prey=="FOM"]=diet$num_prey[diet$prey=="FOM"]+plus_FOM
# add the information on predators
data_pred<-data[,which(names(data)%in%c("bodymass","TL","names","num"))]
names(data_pred)<-c("pred","TL_pred","bodymass_pred","num_pred")
diet<-merge(diet,data_pred,by=c("pred"),all=T) # add the information on predators to the data set
diet<-diet[diet$prey!=diet$pred,] # removes self-interactions
diet<-diet[diet$flow!=0 & is.na(diet$flow)==F,] # removes empty flows
# position of nodes
nodes<-data[,c("names","TL","num")]
#nodes<-nodes[order(nodes$names),]
#nodes$num<-c(1:n_tot_species)
nodes$TL[nodes$TL==0]=NA
temp<-data.frame(names=names_abio)
temp$TL=0
temp$num<-c(1:(n_tot_detritus+n_nutrients))
nodes<-rbind(nodes,temp)
rm(temp)
nodes$num[nodes$names=="SOM"]=nodes$num[nodes$names=="SOM"]+plus_SOM # to make the dots more readable
nodes$num[which(nodes$names%in%names_faeces)]=nodes$num[which(nodes$names%in%names_faeces)]+plus_faeces
nodes$num[nodes$names=="FOM"]=nodes$num[nodes$names=="FOM"]+plus_FOM
nodes$num[nodes$names=="N"]=nodes$num[nodes$names=="N"]+plus_N
nodes$names<-factor(nodes$names,levels=names_tot)
# removes species without interactions
for (i in 1:n_tot_species){
if (length(diet$flow[diet$pred==nodes$names[i]])==0){
nodes$TL[i]=NA
nodes$num[i]=NA
}
}
# removes empty compartments
for (i in (n_tot_species+1):n_tot){
if (data_density$biomass[data_density$input_FOM==input_FOM &
data_density$input_DOC==input_DOC &
data_density$names==nodes$names[i]]==0){
nodes$TL[i]=NA
nodes$num[i]=NA
nodes$num[(i+1):n_tot]=nodes$num[(i+1):n_tot]-1
diet$num_prey[diet$prey%in%nodes$names[(i+1):n_tot]]=diet$num_prey[diet$prey%in%nodes$names[(i+1):n_tot]]-1
}
}
return(list(diet=diet,
nodes=nodes,
scale_colour_line_total=palette$scale_colour_line_total))
})
}
# multi-channel
data_foodweb<-get_data_foodweb(300,150,data_multi_channel,palette_multi_channel)
data_foodweb$nodes[data_foodweb$nodes$names=="N",c("TL","num")]=NA
p1<-ggplot()+
geom_curve(data=data_foodweb$diet,
aes(x=num_prey, xend=num_pred,
y=TL_prey, yend=TL_pred,
size=flow, alpha=flow),
curvature=0)+
geom_point(data=data_foodweb$nodes,
aes(num,TL,colour=names),size=7)+
data_foodweb$scale_colour_line_total+
scale_size_continuous(name=expression("Flow (mgC m"^-2*" day"^-1*")"))+
scale_alpha(name=expression("Flow (mgC m"^-2*" day"^-1*")"))+
guides(colour="none")+
theme+theme(legend.position=c(0.01, 1),
legend.justification=c(0, 1),
legend.background=element_blank(),
text=element_text(size=30),
axis.text=element_text(size=30),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.line.x = element_line(arrow = arrow(angle = 15, length = unit(.15,"inches"))),
panel.grid=element_blank())+
scale_y_continuous(breaks=c(0:floor(max(data_foodweb$nodes$TL,na.rm=TRUE))),
limits=c(-0.5,NA))+
xlab(label_bodymass_approx)+
ylab(label_TL)+
ggtitle(label_multichannel)
#p1<-align_legend(p1,1)
# size structured
data_foodweb<-get_data_foodweb(300,150,data_size_structured,palette_size_structured)
data_foodweb$nodes[data_foodweb$nodes$names=="N",c("TL","num")]=NA
p2<-ggplot()+
geom_curve(data=data_foodweb$diet,
aes(x=num_prey, xend=num_pred,
y=TL_prey, yend=TL_pred,
size=flow, alpha=flow),
curvature=0)+
geom_point(data=data_foodweb$nodes,
aes(num,TL,colour=names),size=7)+
data_foodweb$scale_colour_line_total+
scale_size_continuous(name=expression("Flow (mgC m"^-2*" day"^-1*")"))+
scale_alpha(name=expression("Flow (mgC m"^-2*" day"^-1*")"))+
guides(colour="none")+
theme+theme(legend.position=c(0.01, 1),
legend.justification=c(0, 1),
legend.background=element_blank(),
text=element_text(size=30),
axis.text=element_text(size=30),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.line.x = element_line(arrow = arrow(angle = 15, length = unit(.15,"inches"))),
panel.grid=element_blank())+
scale_y_continuous(breaks=c(0:floor(max(data_foodweb$nodes$TL,na.rm=TRUE))),
limits=c(-0.5,NA))+
xlab(label_bodymass_approx)+
ylab(label_TL)+
ggtitle(label_size_structured)
# legend
data<-data_species[data_species$trophic_group_file==label_multichannel,c("trophic_group","names")]
levels(data$trophic_group)<-c("microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales")
databis<-data_detritus[data_detritus$trophic_group_file==1,c("type","names")]
names(databis)<-c("trophic_group","names")
data<-rbind(data,databis)
rm(databis)
data$trophic_group<-factor(data$trophic_group,c("DOC","SOM","faeces","FOM","microbes","microbivores","micro-carnivores","macro-detritivores","macro-carnivores","trophic whales"))
legend<-ggplot(data=data)+
geom_bar(aes(trophic_group,fill=names),stat="count")+
theme_void()+theme(legend.position="none",
axis.text.y=element_text(family="serif",size=15,hjust=1))+
palette_multi_channel$scale_colour_bar_total+
coord_flip()+
ylab(label_biomass_fraction)
# final graph
space=0.1
graph<-ggdraw(xlim = c(0, 1), ylim = c(0, 2+space)) +
draw_plot(p1, 0, 1+space, 1, 1)+
draw_plot(p2, 0, 0, 1, 1)+
draw_plot(legend, 0.65, 1.5+space, 0.3, 0.5)+
draw_plot_label(c("A","B"), c(0,0), c(2+space,1), size = 30)
ggsave(paste(path_figures,"supp_foodweb.pdf",sep=""), graph, width = 16, height = 14, device=cairo_pdf)
# Biomass distribution per species # ----
p1<-ggplot(data_multi_channel$data_species)+
geom_rect(aes(xmin=as.numeric(names)-0.5,xmax=as.numeric(names)+0.5,ymin=min(biomass,na.rm=TRUE)*0.5,ymax=biomass,fill=names))+
theme+theme(legend.position="none",
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank(),
plot.margin = margin(t=5.5,r=5.5,b=5.5,l=40, "pt"))+ # 5.5 is the default value
palette_multi_channel$scale_colour_bar_species+
coord_flip()+
scale_y_log10(breaks = 10^c(0:3),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
ylab(label_biomass)+
ggtitle(label_multichannel)
p2<-ggplot(data_detritivore_spectrum$data_species)+
geom_rect(aes(xmin=as.numeric(names)-0.5,xmax=as.numeric(names)+0.5,ymin=min(biomass,na.rm=TRUE)*0.5,ymax=biomass,fill=names))+
annotate("errorbarh",xmin=24,xmax=32,y=1,height = 0.2)+
annotate("text",x=29,y=10^1.2,label="extinct detritivores",family="serif",size=6)+
theme+theme(legend.position="none",
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank(),
plot.margin = margin(t=5.5,r=5.5,b=5.5,l=40, "pt"))+ # 5.5 is the default value
palette_detritivore_spectrum$scale_colour_bar_species+
coord_flip()+
scale_y_log10(breaks = 10^c(0:3),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
ylab(label_biomass)+
ggtitle(label_detritivores_spectrum)
p3<-ggplot(data_size_structured$data_species)+
geom_rect(aes(xmin=as.numeric(names)-0.5,xmax=as.numeric(names)+0.5,ymin=min(biomass,na.rm=TRUE)*0.5,ymax=biomass,fill=names))+
theme+theme(legend.position="none",
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank(),
plot.margin = margin(t=5.5,r=5.5,b=5.5,l=40, "pt"))+ # 5.5 is the default value
palette_size_structured$scale_colour_bar_species+
coord_flip()+
scale_y_log10(breaks = 10^c(0:3),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
ylab(label_biomass)+
ggtitle(label_size_structured)
# Biomass distribution compared with Potapov
data_potapov<-read.table(paste(path_potapov,"RawData_MassDensityTrophicLevel.csv",sep=""),sep=",",header=T)
#data_potapov<-data_potapov[,c("SizeClass","Biomass_mg_gC")]
data_potapov<-data_potapov %>% separate(SizeClass, c("bodymass",NA), sep = "-")
data_potapov$bodymass[data_potapov$bodymass==">1000"]=1000
data_potapov$bodymass<-as.numeric(data_potapov$bodymass)
data_potapov<-aggregate(Biomass_mg_gC~bodymass+Layer+TrophicGroup,data=data_potapov,mean) # mean over sites
data_potapov<-aggregate(Biomass_mg_gC~bodymass,data=data_potapov,sum) # sum of trophic groups inside each size class
p4<-ggplot(data_potapov)+
geom_ribbon(aes(x=bodymass,ymin=min(Biomass_mg_gC), ymax=Biomass_mg_gC),fill="lightgray")+
theme+theme(legend.position="none")+
palette_multi_channel$scale_colour_line_species+
coord_flip()+
x_axis_log10+
y_axis_log10+
xlab(label_bodymass)+
ylab(label_biomass_potapov)+
ggtitle("Empirical")