rm(list = ls())

install.packages("lemon")
install.packages("plyr")
install.packages("lme4")
install.packages("lmerTest")
install.packages("metafor")

install.packages("dplyr")
install.packages("reshape")
install.packages("data.table")

install.packages("ggmap")
install.packages("maps")
install.packages("effects")
install.packages("lsmeans")
install.packages("multcomp")

install.packages("ggplot2")
install.packages("ggpmisc")

install.packages("gridExtra") 
install.packages("ggpubr")
install.packages("gtable")
install.packages("Hmisc")
install.packages("PerformanceAnalytics")
install.packages("interplot")
install.packages(" MuMIn")
install.packages("viridis")


library(lemon)
library(plyr)
library(lme4)
library(data.table)
library(lmerTest)
library(metafor)
library(car)
library(dplyr)
library(reshape)
library(boot)
library(MuMIn)
library(ggmap)

library(effects)
library(lsmeans)
library(multcomp)
library(maps)
library(ggplot2)
library(ggpmisc)
library(grid) 
library(gridExtra) 
library(ggpubr)
library(gtable)
library(Hmisc)
library(PerformanceAnalytics)
library(cowplot)
library(multcomp)
library(interplot)
library(viridis)


dat<-data.table(read.csv("NCOMMS-18-27910B_Source Data File.csv"))
dat[,yi:=log(Xt/Xc)]
dat[,Wr:=Nt*Nc/(Nt+Nc)]
str(dat) 


############FIG. 1-PLOT MAP#############
nrow(unique(dat[,c("LAT", "LON")])) 
length(dat[,Study])
length(unique(dat[,Study]))
dat$Study<-as.factor(dat$Study)


DatMAP<-dat[Ecosystem=="Grassland"|Ecosystem=="ForestN"|Ecosystem=="ForestP"|Ecosystem=="Cropland"]

DatMAP$Ecosystem<-factor(DatMAP$Ecosystem, 
                         levels = c("Cropland","Grassland", "ForestP",  "ForestN"), 
                         labels=c("2Cropland","3Grassland", "4ForestP",  "5ForestN"))
DatMAP$Ecosystem1<-factor(DatMAP$Ecosystem, 
                          levels = c("2Cropland","3Grassland", "4ForestP",  "5ForestN"), 
                          labels=c("Cropland","Grassland", "Planted forest",  "Natural forest"))


world_map <- map_data("world")
p <- ggplot()+ 
  coord_fixed() +
  xlab("") + ylab("")+
  geom_map(dat=world_map,map=world_map, 
           aes(map_id=region), 
           colour="black", fill="#fed976",size=0.2)+
  expand_limits(x = c(-170,177), y = c(-54,90))+
  theme_map()+
  theme(
    panel.background = element_rect(fill = 'lightskyblue', colour = 'lightskyblue'), 
    axis.line = element_blank(), 
    legend.position  = c(0.13,0.15),
    legend.text = element_text(size = 9),
    legend.key.height=unit(0.4,"line"),
    legend.key.width =unit(0.5,"line"),
    axis.ticks=element_blank(), 
    axis.text.x=element_blank(),
    axis.text.y=element_blank())+
  geom_point(data = DatMAP,aes(x=LON,y=LAT,fill=Ecosystem1),shape=21,size=2,col="black")+
  scale_fill_manual(name  ="",values=c("magenta","purple","springgreen1","forestgreen"))+
  guides(fill=guide_legend(nrow=2,byrow=TRUE),size=F)


p

ggsave(filename="FIG 1 MAP.tiff",dpi =500, plot=p,width = 9.35, height = 3.9, units = "in")
ggsave(filename="FIG 1 MAP.pdf",dpi =500, plot=p,width = 9.35, height = 3.9, units = "in")





#######EXAMINE SOIL DEPTH, METHOD#####

DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]
DatMGG<-dat[Trait=="GPN"]
DatMCN<-dat[Trait=="CN"]
DatMR<-dat[Trait=="MR"]
DatMQ<-dat[Trait=="MetabolicQ"]


MA_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMA,na.action = na.omit)
MB_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMB,na.action = na.omit)
MF_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMF,na.action = na.omit)
MFB_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMFB,na.action = na.omit)
MGG_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMGG,na.action = na.omit)
MCN_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMCN,na.action = na.omit)
MR_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMR,na.action = na.omit)
MQ_Depth<-lmer(yi~SoilDepth+(1|Study), weights=Wr, data=DatMQ,na.action = na.omit)
Depth_effect<-rbind(anova(MA_Depth)[1,],anova(MB_Depth)[1,],
                    anova(MF_Depth)[1,],anova(MFB_Depth)[1,],
                    anova(MGG_Depth)[1,],anova(MCN_Depth)[1,],
                    anova(MR_Depth)[1,],anova(MQ_Depth)[1,])
Depth_effect
write.csv(Depth_effect, "Depth_effect.csv")

####METHOD


DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]



MA_Method<-lmer(yi~Method+(1|Study), weights=Wr, data=DatMA,na.action = na.omit)
MB_Method<-lmer(yi~Method+(1|Study), weights=Wr, data=DatMB,na.action = na.omit)
MF_Method<-lmer(yi~Method+(1|Study), weights=Wr, data=DatMF,na.action = na.omit)
MFB_Method<-lmer(yi~Method+(1|Study), weights=Wr, data=DatMFB,na.action = na.omit)

Method_effect<-rbind(anova(MA_Method)[1,],anova(MB_Method)[1,],
                     anova(MF_Method)[1,],anova(MFB_Method)[1,])
Method_effect
write.csv(Method_effect, "Method_effect.csv")


#####MODEL SELECTION--ACROSS ALL ECOSYSTEMS#####
DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]
DatMGG<-dat[Trait=="GPN"]
DatMCN<-dat[Trait=="CN"]
DatMR<-dat[Trait=="MR"]
DatMQ<-dat[Trait=="MetabolicQ"]
options(na.action = na.fail)


####R & A####
MMA_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMA)
MMB_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMB)
MMF_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMF)
MMFB_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMFB)
MMGG_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMGG)
MMCN_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMCN)
MMR_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMR)
MMQ_FULL_1<-lmer(yi~scale(MixRichness)*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMQ)



MMA_S_1<-dredge(MMA_FULL_1);MMA_S_1
MMB_S_1<-dredge(MMB_FULL_1);MMB_S_1
MMF_S_1<-dredge(MMF_FULL_1);MMF_S_1
MMFB_S_1<-dredge(MMFB_FULL_1);MMFB_S_1
MMGG_S_1<-dredge(MMGG_FULL_1);MMGG_S_1
MMCN_S_1<-dredge(MMCN_FULL_1);MMCN_S_1
MMR_S_1<-dredge(MMR_FULL_1);MMR_S_1
MMQ_S_1<-dredge(MMQ_FULL_1);MMQ_S_1



MMA1<-lmer(yi~scale(MixRichness)*scale(StandAge)+(1|Study), weights=Wr, DatMA)
MMB1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMB)
MMF1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMF)
MMFB1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMFB)
MMGG1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMGG)
MMCN1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMCN)
MMR1<-lmer(yi~scale(MixRichness)*scale(StandAge)+(1|Study), weights=Wr, DatMR)
MMQ1<-lmer(yi~scale(MixRichness)+scale(StandAge)+(1|Study), weights=Wr, DatMQ)



#######LOG(R) & A#######
MMA_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMA)
MMB_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMB)
MMF_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMF)
MMFB_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMFB)
MMGG_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMGG)
MMCN_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMCN)
MMR_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMR)
MMQ_FULL_2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*Ecosystem+(1|Study), weights=Wr, DatMQ)



MMA_S_2<-dredge(MMA_FULL_2);MMA_S_2
MMB_S_2<-dredge(MMB_FULL_2);MMB_S_2
MMF_S_2<-dredge(MMF_FULL_2);MMF_S_2
MMFB_S_2<-dredge(MMFB_FULL_2);MMFB_S_2
MMGG_S_2<-dredge(MMGG_FULL_2);MMGG_S_2
MMCN_S_2<-dredge(MMCN_FULL_2);MMCN_S_2
MMR_S_2<-dredge(MMR_FULL_2);MMR_S_2
MMQ_S_2<-dredge(MMQ_FULL_2);MMQ_S_2



MMA2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, DatMA)
MMB2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMB)
MMF2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMF)
MMFB2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMFB)
MMGG2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMGG)
MMCN2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMCN)
MMR2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, DatMR)
MMQ2<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, DatMQ)



summary(MMA2)
summary(MMB2)
summary(MMF2)
summary(MMFB2)
summary(MMGG2)
summary(MMCN2)
summary(MMR2)
summary(MMQ2)



####LOG(R) & LOG(A)#####
MMA_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMA)
MMB_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMB)
MMF_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMF)
MMFB_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMFB)
MMGG_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMGG)
MMCN_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMCN)
MMR_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMR)
MMQ_FULL_3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMQ)



MMA_S_3<-dredge(MMA_FULL_3);MMA_S_3
MMB_S_3<-dredge(MMB_FULL_3);MMB_S_3
MMF_S_3<-dredge(MMF_FULL_3);MMF_S_3
MMFB_S_3<-dredge(MMFB_FULL_3);MMFB_S_3
MMGG_S_3<-dredge(MMGG_FULL_3);MMGG_S_3
MMCN_S_3<-dredge(MMCN_FULL_3);MMCN_S_3
MMR_S_3<-dredge(MMR_FULL_3);MMR_S_3
MMQ_S_3<-dredge(MMQ_FULL_3);MMQ_S_3



MMA3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))+(1|Study), weights=Wr, DatMA)
MMB3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMB)
MMF3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMF)
MMFB3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMFB)
MMGG3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMGG)
MMCN3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMCN)
MMR3<-lmer(yi~scale(log(MixRichness))*scale(log(StandAge))+(1|Study), weights=Wr, DatMR)
MMQ3<-lmer(yi~scale(log(MixRichness))+scale(log(StandAge))+(1|Study), weights=Wr, DatMQ)




####R & LOG(A)#####

MMA_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMA)
MMB_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMB)
MMF_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMF)
MMFB_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMFB)
MMGG_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMGG)
MMCN_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMCN)
MMR_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMR)
MMQ_FULL_4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))*Ecosystem+(1|Study), weights=Wr, DatMQ)



MMA_S_4<-dredge(MMA_FULL_4);MMA_S_4
MMB_S_4<-dredge(MMB_FULL_4);MMB_S_4
MMF_S_4<-dredge(MMF_FULL_4);MMF_S_4
MMFB_S_4<-dredge(MMFB_FULL_4);MMFB_S_4
MMGG_S_4<-dredge(MMGG_FULL_4);MMGG_S_4
MMCN_S_4<-dredge(MMCN_FULL_4);MMCN_S_4
MMR_S_4<-dredge(MMR_FULL_4);MMR_S_4
MMQ_S_4<-dredge(MMQ_FULL_4);MMQ_S_4



MMA4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))+(1|Study), weights=Wr, DatMA)
MMB4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMB)
MMF4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMF)
MMFB4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMFB)
MMGG4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMGG)
MMCN4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMCN)
MMR4<-lmer(yi~scale(MixRichness)*scale(log(StandAge))+(1|Study), weights=Wr, DatMR)
MMQ4<-lmer(yi~scale(MixRichness)+scale(log(StandAge))+(1|Study), weights=Wr, DatMQ)





AIC<-cbind(rbind(AIC(MMA_FULL_2),AIC(MMB_FULL_2),AIC(MMF_FULL_2),AIC(MMFB_FULL_2),
                 AIC(MMGG_FULL_2),AIC(MMCN_FULL_2),AIC(MMR_FULL_2),AIC(MMQ_FULL_2)),
           rbind(AIC(MMA1),AIC(MMB1),AIC(MMF1),AIC(MMFB1),
                 AIC(MMGG1),AIC(MMCN1),AIC(MMR1),AIC(MMQ1)),
           rbind(AIC(MMA2),AIC(MMB2),AIC(MMF2),AIC(MMFB2),
                 AIC(MMGG2),AIC(MMCN2),AIC(MMR2),AIC(MMQ2)),
           rbind(AIC(MMA3),AIC(MMB3),AIC(MMF3),AIC(MMFB3),
                 AIC(MMGG3),AIC(MMCN3),AIC(MMR3),AIC(MMQ3)),
           rbind(AIC(MMA4),AIC(MMB4),AIC(MMF4),AIC(MMFB4),
                 AIC(MMGG4),AIC(MMCN4),AIC(MMR4),AIC(MMQ4)))

AIC
write.csv(AIC,"AIC_Ecosystem.csv")

anova_alldata_R<-rbind(anova(MMA2)[1,],anova(MMB2)[1,],anova(MMF2)[1,],
                       anova(MMFB2)[1,],anova(MMGG2)[1,],anova(MMCN2)[1,],
                       anova(MMR2)[1,],anova(MMQ2)[1,])

anova_alldata_R
write.csv(anova_alldata_R,"anova_alldata_R.csv")


anova_alldata_A<-rbind(anova(MMA2)[2,],anova(MMB2)[2,],anova(MMF2)[2,],
                       anova(MMFB2)[2,],anova(MMGG2)[2,],anova(MMCN2)[2,],
                       anova(MMR2)[2,],anova(MMQ2)[2,])

anova_alldata_A
write.csv(anova_alldata_A,"anova_alldata_A.csv")

anova_alldata_RA<-rbind(anova(MMA2)[3,],anova(MMB2)[3,],anova(MMF2)[3,],
                        anova(MMFB2)[3,],anova(MMGG2)[3,],anova(MMCN2)[3,],
                        anova(MMR2)[3,],anova(MMQ2)[3,])

anova_alldata_RA


#####MODEL SELECTION--NATURAL CLIMATE#####

datN<-dat[Ecosystem=="Grassland"|Ecosystem=="ForestN"|Ecosystem=="ForestP"]
datNMA<-datN[Trait=="MA"]
datNMB<-datN[Trait=="BacA"]
datNMF<-datN[Trait=="FunA"]
datNMFB<-datN[Trait=="FB"]
datNMGG<-datN[Trait=="GPN"]
datNMCN<-datN[Trait=="CN"]
datNMR<-datN[Trait=="MR"]
datNMQ<-datN[Trait=="MetabolicQ"]

#####MAT MODEL
M_MAT_A<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMA)
M_MAT_B<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMB)
M_MAT_F<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMF)
M_MAT_FB<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMFB)
M_MAT_GG<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMGG)
M_MAT_CN<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMCN)
M_MAT_R<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMR)
M_MAT_Q<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(MAT)+(1|Study), weights=Wr, datNMQ)



M_MAT_A_S<-dredge(M_MAT_A);M_MAT_A_S
M_MAT_B_S<-dredge(M_MAT_B);M_MAT_B_S
M_MAT_F_S<-dredge(M_MAT_F);M_MAT_F_S
M_MAT_FB_S<-dredge(M_MAT_FB);M_MAT_FB_S
M_MAT_GG_S<-dredge(M_MAT_GG);M_MAT_GG_S
M_MAT_CN_S<-dredge(M_MAT_CN);M_MAT_CN_S
M_MAT_R_S<-dredge(M_MAT_R);M_MAT_R_S
M_MAT_Q_S<-dredge(M_MAT_Q);M_MAT_Q_S




M_MAT_A1<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, datNMA)
M_MAT_B1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMB)
M_MAT_F1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMF)
M_MAT_FB1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMFB)
M_MAT_GG1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMGG)
M_MAT_CN1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMCN)
M_MAT_R1<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+scale(log(MixRichness))*scale(MAT)+(1|Study), weights=Wr, datNMR)
M_MAT_Q1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMQ)




anova_MAT_R<-rbind(anova(M_MAT_A1)[1,],anova(M_MAT_B1)[1,],anova(M_MAT_F1)[1,],
                   anova(M_MAT_FB1)[1,],anova(M_MAT_GG1)[1,],anova(M_MAT_CN1)[1,],
                   anova(M_MAT_R1)[1,],anova(M_MAT_Q1)[1,])

anova_MAT_R
write.csv(anova_MAT_R, 'anova_MAT_R.csv')


anova_MAT_A<-rbind(anova(M_MAT_A1)[2,],anova(M_MAT_B1)[2,],anova(M_MAT_F1)[2,],
                   anova(M_MAT_FB1)[2,],anova(M_MAT_GG1)[2,],anova(M_MAT_CN1)[2,],
                   anova(M_MAT_R1)[2,],anova(M_MAT_Q1)[2,])

anova_MAT_A
write.csv(anova_MAT_A, 'anova_MAT_A.csv')



anova_MAT_RA<-rbind(anova(M_MAT_A1)[3,],anova(M_MAT_B1)[3,],anova(M_MAT_F1)[3,],
                    anova(M_MAT_FB1)[3,],anova(M_MAT_GG1)[3,],anova(M_MAT_CN1)[3,],
                    anova(M_MAT_R1)[4,],anova(M_MAT_Q1)[3,])

anova_MAT_RA


anova(M_MAT_R1)


####AI MODEL

M_AI_A<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMA)
M_AI_B<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMB)
M_AI_F<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMF)
M_AI_FB<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMFB)
M_AI_GG<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMGG)
M_AI_CN<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMCN)
M_AI_R<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMR)
M_AI_Q<-lmer(yi~scale(log(MixRichness))*scale(StandAge)*scale(AI)+(1|Study), weights=Wr, datNMQ)



M_AI_A_S<-dredge(M_AI_A);M_AI_A_S
M_AI_B_S<-dredge(M_AI_B);M_AI_B_S
M_AI_F_S<-dredge(M_AI_F);M_AI_F_S
M_AI_FB_S<-dredge(M_AI_FB);M_AI_FB_S
M_AI_GG_S<-dredge(M_AI_GG);M_AI_GG_S
M_AI_CN_S<-dredge(M_AI_CN);M_AI_CN_S
M_AI_R_S<-dredge(M_AI_R);M_AI_R_S
M_AI_Q_S<-dredge(M_AI_Q);M_AI_Q_S





M_AI_A1<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, datNMA)
M_AI_B1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMB)
M_AI_F1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMF)
M_AI_FB1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMFB)
M_AI_GG1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMGG)
M_AI_CN1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMCN)
M_AI_R1<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, datNMR)
M_AI_Q1<-lmer(yi~scale(log(MixRichness))+scale(StandAge)+(1|Study), weights=Wr, datNMQ)




anova_AI_R<-rbind(anova(M_AI_A1)[1,],anova(M_AI_B1)[1,],anova(M_AI_F1)[1,],
                  anova(M_AI_FB1)[1,],anova(M_AI_GG1)[1,],anova(M_AI_CN1)[1,],
                  anova(M_AI_R1)[1,],anova(M_AI_Q1)[1,])

anova_AI_R
write.csv(anova_AI_R, 'anova_AI_R.csv')


anova_AI_A<-rbind(anova(M_AI_A1)[2,],anova(M_AI_B1)[2,],anova(M_AI_F1)[2,],
                  anova(M_AI_FB1)[2,],anova(M_AI_GG1)[2,],anova(M_AI_CN1)[2,],
                  anova(M_AI_R1)[2,],anova(M_AI_Q1)[2,])

anova_AI_A
write.csv(anova_AI_A, 'anova_AI_A.csv')

anova_AI_RA<-rbind(anova(M_AI_A1)[3,],anova(M_AI_B1)[3,],anova(M_AI_F1)[3,],
                   anova(M_AI_FB1)[3,],anova(M_AI_GG1)[3,],anova(M_AI_CN1)[3,],
                   anova(M_AI_R1)[3,],anova(M_AI_Q1)[3,])

anova_AI_RA


####FIG. 2#####
summary(MMA2)
summary(MMB2)
summary(MMF2)
summary(MMFB2)
summary(MMGG2)
summary(MMCN2)
summary(MMR2)
summary(MMQ2)

MP1<-rbind(confint(MMA2, method="Wald")[3,],
           confint(MMB2, method="Wald")[3,],
           confint(MMF2, method="Wald")[3,], 
           confint(MMFB2, method="Wald")[3,], 
           confint(MMGG2, method="Wald")[3,],
           confint(MMCN2, method="Wald")[3,],
           confint(MMR2, method="Wald")[3,], 
           confint(MMQ2, method="Wald")[3,])

MP1<-data.frame(MP1)
MP1$mean<-(MP1$X2.5..+MP1$X97.5..)/2
MP1<- setNames(MP1, c("cl","cu", "mean"))
MP1$Trait<-c("MA","BacA", "FunA",  "FB","G+:G-","CN","MR","MetabolicQ")

dat[, .N, by=Trait]
dat[, .(NS= length(unique(Study))), by = Trait]

MP1$cl_p<-(exp(MP1$cl)-1)*100
MP1$cu_p<-(exp(MP1$cu)-1)*100
MP1$mean_p<-(MP1$cl_p+MP1$cu_p)/2
MP1


MP1$Trait<-factor(MP1$Trait, 
                  levels = c("MA","BacA", "FunA",  "FB","G+:G-","CN","MR","MetabolicQ"), 
                  labels=c("Microbial biomass","Bacterial biomass", "Fungal biomass", "Fungi : bacteria","G+ : G-", "Microbial C : N", "Microbial respiration","Metabolic quotient"))
MP1$Label<-c("416 (96)","127 (31)","126 (31)","129 (31)","82 (15)","110 (28)","186 (33)","156 (24)")




F2<-ggplot(MP1)+ geom_point(aes(x=Trait, y=mean_p),stat="identity", size=4)+
  geom_errorbar(aes(x=Trait,ymin=cl_p, ymax=cu_p), width=.15, size=0.5)+
  geom_text(aes(label=Label,x=Trait, y=35,hjust=0), size=3.5)+
  coord_flip(ylim = c(-40, 50))+ 
  scale_x_discrete(limits = rev(levels(MP1$Trait)))+
  geom_hline(aes(yintercept=0),linetype="dashed",colour="grey")+
  xlab("")+ylab("Plant mixture effect on microbial attributes (%)") +
  theme_bw()+ theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(),
                    panel.grid.minor = element_blank(),
                    panel.background = element_blank(),
                    axis.text  = element_text(size=11),
                    axis.title.y = element_text(size=12))+
  theme(strip.background = element_blank(), strip.placement = "outside")+
  theme(legend.position="none")
F2
ggsave(filename="FIG 2.tiff",dpi =400, plot=F2,width = 5, height = 5, units = "in")
ggsave(filename="FIG 2.pdf",dpi =400, plot=F2,width = 5, height = 5, units = "in")


#####FIG. 3####
MP2a<-rbind(confint(MMA2, method="Wald")[4,],
            confint(MMB2, method="Wald")[4,],
            confint(MMF2, method="Wald")[4,], 
            confint(MMFB2, method="Wald")[4,], 
            confint(MMGG2, method="Wald")[4,],
            confint(MMCN2, method="Wald")[4,],
            confint(MMR2, method="Wald")[4,], 
            confint(MMQ2, method="Wald")[4,])


MP2a<-data.frame(MP2a)
MP2a$mean<-(MP2a$X2.5..+MP2a$X97.5..)/2
MP2a<- setNames(MP2a, c("cl","cu", "mean"))
MP2a$Trait<-c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ")


N_C<-dat[, .N, by=Trait]
N_S<-dat[, .(NS= length(unique(Study))), by = Trait]
NCS<-merge(N_C, N_S)
NCS<-NCS[order(match(Trait,c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ")))]
setnames(NCS, "Trait", "Trait1")

MP2a<-cbind(MP2a,NCS )
MP2a$st <-paste(MP2a$N,"(",MP2a$NS,")")   
MP2a$Slope <-paste("N")   


MP2b<-rbind(confint(MMA2, method="Wald")[5,],
            confint(MMB2, method="Wald")[5,],
            confint(MMF2, method="Wald")[5,], 
            confint(MMFB2, method="Wald")[5,], 
            confint(MMGG2, method="Wald")[5,],
            confint(MMCN2, method="Wald")[5,],
            confint(MMR2, method="Wald")[5,], 
            confint(MMQ2, method="Wald")[5,])

MP2b<-data.frame(MP2b)
MP2b$mean<-(MP2b$X2.5..+MP2b$X97.5..)/2
MP2b<- setNames(MP2b, c("cl","cu", "mean"))
MP2b$Trait<-c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ")

N_C<-dat[, .N, by=Trait]
N_S<-dat[, .(NS= length(unique(Study))), by = Trait]
NCS<-merge(N_C, N_S)
NCS<-NCS[order(match(Trait,c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ")))]
setnames(NCS, "Trait", "Trait1")

MP2b<-cbind(MP2b,NCS )
MP2b$st <-paste(MP2b$N,"(",MP2b$NS,")") 
MP2b$Slope <-paste("D")     

MP2<-rbind(MP2a, MP2b)

MP2$Trait<-factor(MP2$Trait, levels = c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ"), labels=c("Microbial biomass","Bacterial biomass", "Fungal biomass", "Fungi : bacteria","G+ : G-", "Microbial C : N", "Microbial respiration","Metabolic quotient"))
MP2$Slope<-factor(MP2$Slope, levels=c("N", "D"))
MP2$abc<-factor(MP2$Slope, levels=c("N", "D"),labels = c("a","b"))
MP2$cl_p<-(exp(MP2$cl)-1)*100
MP2$cu_p<-(exp(MP2$cu)-1)*100
MP2$mean_p<-(MP2$cl_p+MP2$cu_p)/2


label_names<-c(
  "N"=paste("Effect of species richness"), 
  "D"=paste("Effect of stand age")
)

F3<-ggplot(MP2)+ geom_point(aes(x=Trait, y=mean_p),stat="identity", shape=21,size=4)+
  geom_errorbar(aes(x=Trait,ymin=cl_p, ymax=cu_p), width=.2, size=0.5)+
  facet_grid(~Slope,labeller=as_labeller(label_names))+
  coord_flip(ylim = c(-40, 40))+ 
  scale_x_discrete(limits = rev(levels(MP2$Trait)))+
  geom_hline(aes(yintercept=0),linetype="dashed",colour="grey")+ 
  xlab("")+ylab("Effect size (%)") +
  theme_bw()+ theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(),
                    panel.grid.minor = element_blank(),
                    panel.background = element_blank())+
  theme(strip.background = element_blank(), strip.placement = "outside")+
  scale_colour_hue(l=40) + theme(legend.position="none")+
  geom_text(aes(label=abc,x=9, y=-Inf),hjust=-0.5,vjust=1.3,size=4,color="black",fontface="bold")

F3

ggsave(filename="FIG 3.tiff",dpi =400, plot=F3,width = 5.6, height = 5, units = "in")
ggsave(filename="FIG 3.pdf",dpi =400, plot=F3,width = 5.6, height = 5, units = "in")




########FIG 4 RICHNESS & STAND AGE INTERACTION#######

MMA2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, DatMA)


DatMA[,sca_age:=scale(StandAge)]
DatMA[,sca_r:=scale(MixRichness)]
mean(log(DatMA$MixRichness))
sd(log(DatMA$MixRichness))
DatMA[,scalog_richness:=scale(log(MixRichness))]



bootpts<-bootMer(MMA2,fixef,1000)

coefmean<-summary(MMA2)$coefficients[ , 1]

DatMA[StandAge<=1,StandAgenew:="1"]##For graph
DatMA[StandAge>1&StandAge<=5,StandAgenew:="5"]##For graph
DatMA[StandAge>5&StandAge<=10,StandAgenew:="10"]##For graph
DatMA[StandAge>10&StandAge<=20,StandAgenew:="20"]##For graph



coef_rare2<-function(x){
  rare=x
  cheater=c(((1)-  12.20222)/27.60247,((5)- 12.20222)/27.60247,((10)- 12.20222)/27.60247,((20)- 12.20222)/27.60247,
            0)
  cheater2=c("1","5","10","20","mean")
  if(which(cheater==rare)<5)richness=DatMA[StandAgenew==cheater2[which(cheater==rare)],seq(min(MixRichness),max(MixRichness),length=100)]
  if(which(cheater==rare)==5)richness=seq(min(DatMA[,MixRichness]),max(DatMA[,MixRichness]),length=100)
  
  richness=log(richness)
  richness=((richness-1.011948)/0.6615774)
  
  data.table(mean=coefmean[1]+(coefmean[2]*richness)+(coefmean[3]*rare)+
               (coefmean[4]*rare*richness),
             up=sapply(richness,function(x,r=rare){
               quantile((bootpts$t[,"(Intercept)"])+
                          (bootpts$t[,"scale(log(MixRichness))"]*x)+
                          (bootpts$t[,"scale(StandAge)"]*r)+
                          (bootpts$t[,"scale(log(MixRichness)):scale(StandAge)"]*x*r),prob=0.975)}),
             low=sapply(richness,function(x,r=rare){
               quantile((bootpts$t[,"(Intercept)"])+
                          (bootpts$t[,"scale(log(MixRichness))"]*x)+
                          (bootpts$t[,"scale(StandAge)"]*r)+
                          (bootpts$t[,"scale(log(MixRichness)):scale(StandAge)"]*x*r),prob=0.025)}),
             Y=richness,
             Richness=exp(richness*0.6615774+1.011948))
}

to_graphs1<-adply(c(((1)-  12.20222)/27.60247,((5)- 12.20222)/27.60247,((10)- 12.20222)/27.60247,((20)- 12.20222)/27.60247,
                    0),.margins=1,
                  .fun=function(x)coef_rare2(x))
to_graphs1$StandAge<-rep(c("1","5","10","20","mean"),each=100)
to_graphs1$StandAge<-factor(to_graphs1$StandAge,levels=c("1","5","10","20","mean"))

#####
MMR2<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+(1|Study), weights=Wr, DatMR)

DatMR[,log_r:=log(MixRichness)]

mean(DatMR$log_r)
sd(DatMR$log_r)

bootr<-bootMer(MMR2,fixef,1000)
coefmeanr<-summary(MMR2)$coefficients[ , 1]

DatMR[StandAge<=1,StandAgenew:="1"]##For graph
DatMR[StandAge>1&StandAge<=5,StandAgenew:="5"]##For graph
DatMR[StandAge>5&StandAge<=10,StandAgenew:="10"]##For graph
DatMR[StandAge>10&StandAge<=20,StandAgenew:="20"]##For graph

coef_rare3<-function(x){
  rare=x
  cheater=c(((1)-  7.410669)/20.0172,((5)- 7.410669)/20.0172,((10)- 7.410669)/20.0172,((20)- 7.410669)/20.0172,
            0)
  cheater2=c("1","5","10","20","mean")
  if(which(cheater==rare)<5)richness=DatMR[StandAgenew==cheater2[which(cheater==rare)],seq(min(MixRichness),max(MixRichness),length=100)]
  if(which(cheater==rare)==5)richness=seq(min(DatMR[,MixRichness]),max(DatMR[,MixRichness]),length=100)
  
  richness=log(richness)
  richness=((richness-1.137697)/0.7858083)
  data.table(mean=coefmeanr[1]+(coefmeanr[2]*richness)+(coefmeanr[3]*rare)+
               (coefmeanr[4]*rare*richness),
             up=sapply(richness,function(x,r=rare){
               quantile((bootr$t[,"(Intercept)"])+
                          (bootr$t[,"scale(log(MixRichness))"]*x)+
                          (bootr$t[,"scale(StandAge)"]*r)+
                          (bootr$t[,"scale(log(MixRichness)):scale(StandAge)"]*x*r),prob=0.975)}),
             low=sapply(richness,function(x,r=rare){
               quantile((bootr$t[,"(Intercept)"])+
                          (bootr$t[,"scale(log(MixRichness))"]*x)+
                          (bootr$t[,"scale(StandAge)"]*r)+
                          (bootr$t[,"scale(log(MixRichness)):scale(StandAge)"]*x*r),prob=0.025)}),
             Y=richness,
             Richness=exp(richness*0.7858083+1.137697))
}

to_graphs2<-adply(c(((1)-  7.410669)/20.0172,((5)- 7.410669)/20.0172,((10)- 7.410669)/20.0172,((20)- 7.410669)/20.0172,
                    0),.margins=1,
                  .fun=function(x)coef_rare3(x))
to_graphs2$StandAge<-rep(c("1","5","10","20","mean"),each=100)

to_graphs1<-data.table(to_graphs1);to_graphs2<-data.table(to_graphs2)
to_graphs1[,Trait:="MBC"];to_graphs2[,Trait:="MR"]
to_graphs<-rbind(to_graphs1,to_graphs2)
to_graphs$Trait<-factor(to_graphs$Trait, levels = c("MBC", "MR"),labels=c("Microbial biomass","Microbial respiration"))
to_graphs$StandAge<-factor(to_graphs$StandAge,levels=c("1","5","10","20","mean"),labels = c("1","5","10","20","mean"))
to_graphs$up<-(exp(to_graphs$up)-1)*100
to_graphs$low<-(exp(to_graphs$low)-1)*100
to_graphs$mean<-(exp(to_graphs$mean)-1)*100



F4a<-ggplot()+
  geom_line(data=to_graphs[to_graphs$Trait=='Microbial biomass',],aes(x=Richness,y=mean,col=StandAge,size=StandAge))+
  geom_ribbon(data=to_graphs[to_graphs$Trait=='Microbial biomass',],aes(x=Richness,ymin=low,ymax=up,group=StandAge,fill=StandAge),col=NA,alpha=0.1)+
  theme_classic()+
  scale_y_continuous(limits = c(-35,680))+
  scale_x_continuous(trans = "log", breaks =  c(2, 4, 8, 16, 60),labels =  c(2, 4, 8, 16, 60))+
  scale_size_manual(values=c(1,1,1,1,1.5))+guides(size=FALSE)+
  scale_colour_manual(values=c('green4','green','magenta','blue',"black"),
                      labels=c("1","5","10","20","mean",
                               name="Age (years)"),guide = guide_legend(title="Age (years)",ncol =1,label.position ="right",label.hjust=0.5,title.position="top"))+
  scale_fill_manual(values=c('green4','green','magenta','blue',"black"),
                    labels=c("1","5","10","20","mean",
                             name="Age (years)"),guide = FALSE)+
  
  geom_text(aes(label="a",x=2, y=Inf),hjust="left",vjust=1,size=5.5,color="black",fontface="bold")+
  geom_text(aes(label="Microbial biomass",x=120^0.5, y=Inf),hjust="middle",vjust=1,size=4.7,color="black")+
  geom_hline(aes(yintercept=0),linetype="dashed",colour="black")+
  ylab(expression("Effect size (%)"))+
  xlab(expression("Plant species richness in mixtures"))+
  theme(legend.position="none",
        axis.title=element_text(size=14),
        axis.text=element_text(size=12))

F4a


F4b<-ggplot()+
  geom_line(data=to_graphs[to_graphs$Trait=='Microbial respiration',],aes(x=Richness,y=mean,col=StandAge,size=StandAge))+
  geom_ribbon(data=to_graphs[to_graphs$Trait=='Microbial respiration',],aes(x=Richness,ymin=low,ymax=up,group=StandAge,fill=StandAge),col=NA,alpha=0.1)+
  theme_classic()+
  scale_y_continuous(limits = c(-35,680))+
  scale_x_continuous(trans = "log", breaks =  c(2, 4, 8, 16, 60),labels =  c(2, 4, 8, 16, 60))+
  scale_size_manual(values=c(1,1,1,1,1.5))+guides(size=FALSE)+
  scale_colour_manual(values=c('green4','green','magenta','blue',"black"),
                      labels=c("1","5","10","20","mean",
                               name="Age (years)"),guide = guide_legend(title="Age (years)",ncol =1,label.position ="right",label.hjust=0.5,title.position="top"))+
  scale_fill_manual(values=c('green4','green','magenta','blue',"black"),
                    labels=c("1","5","10","20","mean",
                             name="Age (years)"),guide = FALSE)+
  geom_text(aes(label="b",x=2, y=Inf),hjust="left",vjust=1,size=5.5,color="black",fontface="bold")+
  geom_text(aes(label="Microbial respiration",x=120^0.5, y=Inf),hjust="middle",vjust=1,size=4.7
            ,color="black")+
  
  geom_hline(aes(yintercept=0),linetype="dashed",colour="black")+
  xlab(expression("Plant species richness in mixtures"))+
  theme(
    legend.position="right",
    axis.title.y =element_blank(),
    axis.text.y =element_blank(),
    legend.title = element_text(size=14),
    legend.text = element_text(size=12),
    axis.title=element_text(size=14),
    axis.text=element_text(size=12))
F4b


MI<-ggarrange(F4a,F4b, ncol=2, nrow=1,common.legend = T,  legend="right",
              widths=c(1.13,1))

MI

ggsave(filename="FIG 4_1.tiff",dpi =400, plot=MI,width =9.6, height = 3.5, units = "in")
ggsave(filename="FIG 4_1.pdf",dpi =400, plot=MI,width =9.6, height = 3.5, units = "in")




#####FIG 5 LOSS PLOT#####

length(dat[,Study])
length(unique(dat[,Study]))



FB_1<-function(x)0.9^(0.12469+0.31695*x)
FB_2<-function(x)0.8^(0.12469+0.31695*x)
FB_3<-function(x)0.6^(0.12469+0.31695*x)
FB_4<-function(x)0.2^(0.12469+0.31695*x)

FR_1<-function(x)0.9^(0.10261+0.33869*x)
FR_2<-function(x)0.8^(0.10261+0.33869*x)
FR_3<-function(x)0.6^(0.10261+0.33869*x)
FR_4<-function(x)0.2^(0.10261+0.33869*x)

Loss_B <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))+
  stat_function(fun =FB_1,aes(color="10% lower species richness"),size=1.2) +
  stat_function(fun =FB_2,aes(color="20% lower species richness"),size=1.2) +
  stat_function(fun =FB_3,aes(color="40% lower species richness"),size=1.2) +
  stat_function(fun =FB_4,aes(color="80% lower species richness"),size=1.2) +
  xlim(0,130)+
  xlab("Age (years)")+
  ylab("Microbial biomass proportion")+
  scale_colour_manual(values=c('yellow2','orange','red','red4'))+
  annotate("text",x = Inf, y = Inf, label ="a", vjust=1.3,hjust=2,size=4.5,fontface =2)+
  theme(
    axis.title=element_text(size=12),
    axis.text=element_text(size=10),
    legend.position  = "none",
    legend.text = element_text(size=12),
    legend.title = element_blank())
Loss_B

Loss_R <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))+
  stat_function(fun =FR_1,aes(color="10% lower species richness"),size=1.2) +
  stat_function(fun =FR_2,aes(color="20% lower species richness"),size=1.2) +
  stat_function(fun =FR_3,aes(color="40% lower species richness"),size=1.2) +
  stat_function(fun =FR_4,aes(color="80% lower species richness"),size=1.2) +
  xlim(0,130)+
  xlab("Age (years)")+
  ylab("Microbial respiration proportion")+
  scale_colour_manual(values=c('yellow2','orange','red','red4'))+
  annotate("text",x = Inf, y = Inf, label ="b", vjust=1.3,hjust=2,size=4.5,fontface =2)+
  
  theme(
    axis.title=element_text(size=12),
    axis.text=element_text(size=10),
    legend.position  = c(0.3,0.7),
    legend.text = element_text(size=10),
    legend.title = element_blank())
Loss_R


P_loss<-plot_grid(Loss_B,Loss_R,
                  align = "vh", nrow = 1,
                  rel_heights = c(1,1),
                  rel_widths = c(1,1))
P_loss
ggsave(filename="FIG 5.tiff",dpi =400, plot=P_loss,width = 7.8, height = 3, units = "in")
ggsave(filename="FIG 5.pdf",dpi =400, plot=P_loss,width = 7.8, height = 3, units = "in")



################FIG S1 ECOSYSTEM EFFECT########################
###x mean & ci table####
setwd("D:/001LAKEHEAD/Object 000-TRY/Object-T-005-Litter decomposition/Submit/NC")
dat<-data.table(read.csv("NC-data-181114.csv"))
dat[,yi:=log(Xt/Xc)]
dat[,Wr:=Nt*Nc/(Nt+Nc)]

DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]
DatMGG<-dat[Trait=="GPN"]
DatMCN<-dat[Trait=="CN"]
DatMR<-dat[Trait=="MR"]
DatMQ<-dat[Trait=="MetabolicQ"]




M<- function(data, indices) {
  d <- data[indices]
  return(mean(d))
}


ci<-cbind(
  rbind(
    boot.ci(boot(data=DatMA[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMA[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMA[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMA[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMB[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMB[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMB[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMB[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMF[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMF[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMF[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMF[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMFB[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMFB[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMFB[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMFB[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMGG[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMGG[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMGG[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMGG[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMCN[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    boot.ci(boot(data=DatMCN[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMCN[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMR[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMR[Ecosystem=="Cropland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMR[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMR[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    
    boot.ci(boot(data=DatMQ[Ecosystem=="Container",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA,
    boot.ci(boot(data=DatMQ[Ecosystem=="Grassland",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    boot.ci(boot(data=DatMQ[Ecosystem=="ForestP",MixRichness], statistic=M, R=1000),type=c("bca"))[["bca"]][,c(4,5)],
    NA),
  rbind(mean(DatMA[Ecosystem=="Container",MixRichness]),
        mean(DatMA[Ecosystem=="Cropland",MixRichness]),
        mean(DatMA[Ecosystem=="Grassland",MixRichness]),
        mean(DatMA[Ecosystem=="ForestP",MixRichness]),
        mean(DatMA[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMB[Ecosystem=="Container",MixRichness]),
        mean(DatMB[Ecosystem=="Cropland",MixRichness]),
        mean(DatMB[Ecosystem=="Grassland",MixRichness]),
        mean(DatMB[Ecosystem=="ForestP",MixRichness]),
        mean(DatMB[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMF[Ecosystem=="Container",MixRichness]),
        mean(DatMF[Ecosystem=="Cropland",MixRichness]),
        mean(DatMF[Ecosystem=="Grassland",MixRichness]),
        mean(DatMF[Ecosystem=="ForestP",MixRichness]),
        mean(DatMF[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMFB[Ecosystem=="Container",MixRichness]),
        mean(DatMFB[Ecosystem=="Cropland",MixRichness]),
        mean(DatMFB[Ecosystem=="Grassland",MixRichness]),
        mean(DatMFB[Ecosystem=="ForestP",MixRichness]),
        mean(DatMFB[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMGG[Ecosystem=="Container",MixRichness]),
        mean(DatMGG[Ecosystem=="Cropland",MixRichness]),
        mean(DatMGG[Ecosystem=="Grassland",MixRichness]),
        mean(DatMGG[Ecosystem=="ForestP",MixRichness]),
        mean(DatMGG[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMCN[Ecosystem=="Container",MixRichness]),
        mean(DatMCN[Ecosystem=="Cropland",MixRichness]),
        mean(DatMCN[Ecosystem=="Grassland",MixRichness]),
        mean(DatMCN[Ecosystem=="ForestP",MixRichness]),
        mean(DatMCN[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMR[Ecosystem=="Container",MixRichness]),
        mean(DatMR[Ecosystem=="Cropland",MixRichness]),
        mean(DatMR[Ecosystem=="Grassland",MixRichness]),
        mean(DatMR[Ecosystem=="ForestP",MixRichness]),
        mean(DatMR[Ecosystem=="ForestN",MixRichness]),
        
        mean(DatMQ[Ecosystem=="Container",MixRichness]),
        mean(DatMQ[Ecosystem=="Cropland",MixRichness]),
        mean(DatMQ[Ecosystem=="Grassland",MixRichness]),
        mean(DatMQ[Ecosystem=="ForestP",MixRichness]),
        mean(DatMQ[Ecosystem=="ForestN",MixRichness]))
)



colnames(ci) <- c("x_low", "x_up","x_mean")
ci<-data.table(ci)
ci[,Traitci:=paste(rep( c("1MA","2BacA", "3FunA",  "4FB","5GPN","6CN","7MR","8MetabolicQ"),each=5))]
ci[,Ecosystemci:=paste(rep( c("1Container","2Cropland","3Grassland", "4ForestP",  "5ForestN"),8))]
ci[,labelx:=paste(rep( c(2,4,8,16,32),8))]
ci$labelx<-as.numeric(ci$labelx)
ci[,Eco1Trait1:=paste0(Traitci,Ecosystemci)]


###scatter pointer table####


setwd("D:/001LAKEHEAD/Object 000-TRY/Object-T-005-Litter decomposition/Submit/NC")
dat<-data.table(read.csv("NC-data-181114.csv"))
dat[,yi:=log(Xt/Xc)]
dat[,Wr:=Nt*Nc/(Nt+Nc)]

DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]
DatMGG<-dat[Trait=="GPN"]
DatMCN<-dat[Trait=="CN"]
DatMR<-dat[Trait=="MR"]
DatMQ<-dat[Trait=="MetabolicQ"]



MA_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMA,na.action = na.omit)
MB_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMB,na.action = na.omit)
MF_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMF,na.action = na.omit)
MFB_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMFB,na.action = na.omit)
MGG_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMGG,na.action = na.omit)
MCN_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMCN,na.action = na.omit)
MR_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMR,na.action = na.omit)
MQ_Ecosystem<-lmer(yi~Ecosystem+(1|Study), weights=Wr, data=DatMQ,na.action = na.omit)
Ecosystem_effect<-rbind(anova(MA_Ecosystem)[1,],anova(MB_Ecosystem)[1,],
                        anova(MF_Ecosystem)[1,],anova(MFB_Ecosystem)[1,],
                        anova(MGG_Ecosystem)[1,],anova(MCN_Ecosystem)[1,],
                        anova(MR_Ecosystem)[1,],anova(MQ_Ecosystem)[1,])
Ecosystem_effect



dat$Ecosystem<-factor(dat$Ecosystem, 
                      levels = c("Container","Cropland","Grassland", "ForestP",  "ForestN"), 
                      labels=c("1Container","2Cropland","3Grassland", "4ForestP",  "5ForestN"))
dat$Ecosystem1<-factor(dat$Ecosystem, 
                       levels = c("1Container","2Cropland","3Grassland", "4ForestP",  "5ForestN"), 
                       labels=c("1Container","2Cropland","3Grassland", "4ForestP",  "5ForestN"))

dat$Trait<-factor(dat$Trait, 
                  levels = c("MA","BacA", "FunA",  "FB","GPN","CN","MR","MetabolicQ"), 
                  labels=c("1MA","2BacA", "3FunA",  "4FB","5GPN","6CN","7MR","8MetabolicQ"))
dat$Trait1<-factor(dat$Trait, 
                   levels = c("1MA","2BacA", "3FunA",  "4FB","5GPN","6CN","7MR","8MetabolicQ"), 
                   labels=c("1MA","2BacA", "3FunA",  "4FB","5GPN","6CN","7MR","8MetabolicQ"))


dat[,Eco1Trait1:=paste0(Trait1,Ecosystem1)]
dat[,yi_p:=(exp(yi)-1)*100]

###ecosystem observation data###
N_O<-dat[, .N, by=Eco1Trait1]
N_S<-dat[, .(NS= length(unique(Study))), by = Eco1Trait1]
N_T<-dat[!duplicated(dat$Eco1Trait1), c("Eco1Trait1","Trait1")]

NOS<-merge(merge(N_O, N_S),N_T)
NOS$st <-paste(NOS$N,"(",NOS$NS,")") 
datln<-merge(NOS,dat,by="Eco1Trait1")
datlo<-merge(ci,datln,by="Eco1Trait1")





####y mean & ci table####
setwd("D:/001LAKEHEAD/Object 000-TRY/Object-T-005-Litter decomposition/Submit/NC")
dat<-data.table(read.csv("NC-data-181114.csv"))
dat[,yi:=log(Xt/Xc)]
dat[,Wr:=Nt*Nc/(Nt+Nc)]

DatMA<-dat[Trait=="MA"]
DatMB<-dat[Trait=="BacA"]
DatMF<-dat[Trait=="FunA"]
DatMFB<-dat[Trait=="FB"]
DatMGG<-dat[Trait=="GPN"]
DatMCN<-dat[Trait=="CN"]
DatMR<-dat[Trait=="MR"]
DatMQ<-dat[Trait=="MetabolicQ"]


MA1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMA,na.action = na.omit)
MB1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMB,na.action = na.omit)
MF1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMF,na.action = na.omit)
MFB1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMFB,na.action = na.omit)
MGG1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMGG,na.action = na.omit)
MCN1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMCN,na.action = na.omit)
MR1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMR,na.action = na.omit)
MQ1_Ecosystem<-lmer(yi~Ecosystem-1+(1|Study), weights=Wr, data=DatMQ,na.action = na.omit)

summary(MA1_Ecosystem)$coefficients[ , 1]

boot1<-bootMer(MA1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot2<-bootMer(MB1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot3<-bootMer(MF1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot4<-bootMer(MFB1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot5<-bootMer(MGG1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot6<-bootMer(MCN1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot7<-bootMer(MR1_Ecosystem ,function(.)fixef(.),nsim=1000)
boot8<-bootMer(MQ1_Ecosystem ,function(.)fixef(.),nsim=1000)

RR<-rbind(
  c(quantile((boot1$t[,"EcosystemContainer"]),prob=0.025),quantile((boot1$t[,"EcosystemContainer"]),prob=0.975),summary(MA1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot1$t[,"EcosystemCropland"]),prob=0.025),quantile((boot1$t[,"EcosystemCropland"]),prob=0.975),summary(MA1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot1$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot1$t[,"EcosystemGrassland"]),prob=0.975),summary(MA1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot1$t[,"EcosystemForestP"]),prob=0.025),quantile((boot1$t[,"EcosystemForestP"]),prob=0.975),summary(MA1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot1$t[,"EcosystemForestN"]),prob=0.025),quantile((boot1$t[,"EcosystemForestN"]),prob=0.975),summary(MA1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  c(quantile((boot2$t[,"EcosystemContainer"]),prob=0.025),quantile((boot2$t[,"EcosystemContainer"]),prob=0.975),summary(MB1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot2$t[,"EcosystemCropland"]),prob=0.025),quantile((boot2$t[,"EcosystemCropland"]),prob=0.975),summary(MB1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot2$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot2$t[,"EcosystemGrassland"]),prob=0.975),summary(MB1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot2$t[,"EcosystemForestP"]),prob=0.025),quantile((boot2$t[,"EcosystemForestP"]),prob=0.975),summary(MB1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot2$t[,"EcosystemForestN"]),prob=0.025),quantile((boot2$t[,"EcosystemForestN"]),prob=0.975),summary(MB1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  c(quantile((boot3$t[,"EcosystemContainer"]),prob=0.025),quantile((boot3$t[,"EcosystemContainer"]),prob=0.975),summary(MF1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot3$t[,"EcosystemCropland"]),prob=0.025),quantile((boot3$t[,"EcosystemCropland"]),prob=0.975),summary(MF1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot3$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot3$t[,"EcosystemGrassland"]),prob=0.975),summary(MF1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot3$t[,"EcosystemForestP"]),prob=0.025),quantile((boot3$t[,"EcosystemForestP"]),prob=0.975),summary(MF1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot3$t[,"EcosystemForestN"]),prob=0.025),quantile((boot3$t[,"EcosystemForestN"]),prob=0.975),summary(MF1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  c(quantile((boot4$t[,"EcosystemContainer"]),prob=0.025),quantile((boot4$t[,"EcosystemContainer"]),prob=0.975),summary(MFB1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot4$t[,"EcosystemCropland"]),prob=0.025),quantile((boot4$t[,"EcosystemCropland"]),prob=0.975),summary(MFB1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot4$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot4$t[,"EcosystemGrassland"]),prob=0.975),summary(MFB1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot4$t[,"EcosystemForestP"]),prob=0.025),quantile((boot4$t[,"EcosystemForestP"]),prob=0.975),summary(MFB1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot4$t[,"EcosystemForestN"]),prob=0.025),quantile((boot4$t[,"EcosystemForestN"]),prob=0.975),summary(MFB1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  c(quantile((boot5$t[,"EcosystemContainer"]),prob=0.025),quantile((boot5$t[,"EcosystemContainer"]),prob=0.975),summary(MGG1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot5$t[,"EcosystemCropland"]),prob=0.025),quantile((boot5$t[,"EcosystemCropland"]),prob=0.975),summary(MGG1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot5$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot5$t[,"EcosystemGrassland"]),prob=0.975),summary(MGG1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot5$t[,"EcosystemForestP"]),prob=0.025),quantile((boot5$t[,"EcosystemForestP"]),prob=0.975),summary(MGG1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot5$t[,"EcosystemForestN"]),prob=0.025),quantile((boot5$t[,"EcosystemForestN"]),prob=0.975),summary(MGG1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  
  c(quantile((boot6$t[,"EcosystemContainer"]),prob=0.025),quantile((boot6$t[,"EcosystemContainer"]),prob=0.975),summary(MCN1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot6$t[,"EcosystemCropland"]),prob=0.025),quantile((boot6$t[,"EcosystemCropland"]),prob=0.975),summary(MCN1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot6$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot6$t[,"EcosystemGrassland"]),prob=0.975),summary(MCN1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot6$t[,"EcosystemForestP"]),prob=0.025),quantile((boot6$t[,"EcosystemForestP"]),prob=0.975),summary(MCN1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot6$t[,"EcosystemForestN"]),prob=0.025),quantile((boot6$t[,"EcosystemForestN"]),prob=0.975),summary(MCN1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  
  c(quantile((boot7$t[,"EcosystemContainer"]),prob=0.025),quantile((boot7$t[,"EcosystemContainer"]),prob=0.975),summary(MR1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot7$t[,"EcosystemCropland"]),prob=0.025),quantile((boot7$t[,"EcosystemCropland"]),prob=0.975),summary(MR1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot7$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot7$t[,"EcosystemGrassland"]),prob=0.975),summary(MR1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot7$t[,"EcosystemForestP"]),prob=0.025),quantile((boot7$t[,"EcosystemForestP"]),prob=0.975),summary(MR1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot7$t[,"EcosystemForestN"]),prob=0.025),quantile((boot7$t[,"EcosystemForestN"]),prob=0.975),summary(MR1_Ecosystem)$coefficients["EcosystemForestN" , 1]),
  
  
  c(quantile((boot8$t[,"EcosystemContainer"]),prob=0.025),quantile((boot8$t[,"EcosystemContainer"]),prob=0.975),summary(MQ1_Ecosystem)$coefficients["EcosystemContainer" , 1]),
  c(quantile((boot8$t[,"EcosystemCropland"]),prob=0.025),quantile((boot8$t[,"EcosystemCropland"]),prob=0.975),summary(MQ1_Ecosystem)$coefficients["EcosystemCropland" , 1]),
  c(quantile((boot8$t[,"EcosystemGrassland"]),prob=0.025),quantile((boot8$t[,"EcosystemGrassland"]),prob=0.975),summary(MQ1_Ecosystem)$coefficients["EcosystemGrassland" , 1]),
  c(quantile((boot8$t[,"EcosystemForestP"]),prob=0.025),quantile((boot8$t[,"EcosystemForestP"]),prob=0.975),summary(MQ1_Ecosystem)$coefficients["EcosystemForestP" , 1]),
  c(quantile((boot8$t[,"EcosystemForestN"]),prob=0.025),quantile((boot8$t[,"EcosystemForestN"]),prob=0.975),summary(MQ1_Ecosystem)$coefficients["EcosystemForestN" , 1]))



colnames(RR) <- c("y_low", "y_up","y_mean")
RR<-data.table(RR)
RR$y_low_p<-(exp(RR$y_low)-1)*100
RR$y_up_p<-(exp(RR$y_up)-1)*100
RR$y_mean_p<-(exp(RR$y_mean)-1)*100


RR[,TraitRR:=paste(rep( c("1MA","2BacA", "3FunA",  "4FB","5GPN","6CN","7MR","8MetabolicQ"),each=5))]
RR[,EcosystemRR:=paste(rep( c("1Container","2Cropland","3Grassland", "4ForestP",  "5ForestN"),8))]

RR[,Eco1Trait1:=paste0(TraitRR,EcosystemRR)]


datlp<-merge(RR,datlo,by="Eco1Trait1")


########plot#####





Pvalue<-c("italic(P) == 0.078",paste0("italic(P)==",deparse("0.930")),
          "italic(P) == 0.198","italic(P) == 0.108",
          "italic(P) == 0.189","italic(P) == 0.907",
          "italic(P) == 0.277",paste0("italic(P)==",deparse("0.408")))
ab<- c(paste0("bold(a)~~~~Microbial~biomass"),
       paste0("bold(b)~~~~Bacterial~biomass"),
       paste0("bold(c)~~~~Fungal~biomass"),
       paste0("bold(d)~~~~",deparse("Fungi:bacteria")),
       paste0("bold(e)~~~~",deparse("G+:G-")),
       paste0("bold(f)~~~~",deparse("Microbial C:N")),
       paste0("bold(g)~~~~Microbial~respiration"),
       paste0("bold(h)~~~~Metabolic~quotient"))





R_Eco<-ggplot(data=datlp)+
  facet_wrap(~TraitRR,ncol = 2,strip.position = "top")+
  geom_point(aes(x=MixRichness, y=yi_p, size = Wr,col=Ecosystemci),show.legend=F,shape=21,alpha=0.4)+
  geom_errorbar(aes(x=x_mean,ymin=y_low_p, ymax=y_up_p,col=Ecosystemci), width=.1, size=1)+
  geom_errorbarh(aes(xmin=x_low, xmax=x_up,y=y_mean_p,col=Ecosystemci),height = 10, size=1)+
  geom_hline(aes(yintercept=0),linetype="dashed",colour="grey") +
  scale_x_continuous(name="Plant species richness in mixtures",trans = "log",breaks=c(2,4,8,16,60))+
  scale_y_continuous(name="Effect size (%)", limits=c(-100, 200))+
  geom_text(aes(label=st,x=labelx, y=-95,col=Ecosystemci),hjust="left", size=4.3,position =position_dodge(width = 0.1),show.legend =F)+
  
  annotate("text",x = 60, y = 180, label = Pvalue , parse = TRUE,vjust=0,hjust= 1,size=4.5)+
  annotate("text",x = 2, y = 180, label =ab, vjust=0,hjust=0,size=5.5, parse = TRUE)+
  theme_bw()+
  theme(panel.background=element_blank(),panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        axis.title=element_text(size=16),
        axis.text=element_text(size=14),
        strip.background = element_blank(), 
        strip.placement = "outside",
        strip.text =element_blank(),
        legend.position = "bottom",
        legend.text = element_text(size=14),
        legend.title = element_text(size=14))+
  scale_color_manual(name  ="Ecosystem type",values=c("orange","red","purple","green3","forestgreen"),
                     label=c("Container","Cropland","Grassland", "Planted forest",  "Natural forest"))




ggsave(filename="FIG S1_3.tiff",dpi =400, plot=R_Eco,width = 10, height = 14, units = "in")




#######FIG S2--MAT EFFECT ON MR######
setwd("D:/001LAKEHEAD/Object 000-TRY/Object-T-005-Litter decomposition/Submit/NC")
dat<-data.table(read.csv("NC-data-181114.csv"))
dat[,yi:=log(Xt/Xc)]
dat[,Wr:=Nt*Nc/(Nt+Nc)]

datN<-dat[Ecosystem=="Grassland"|Ecosystem=="ForestN"|Ecosystem=="ForestP"]
datNMR<-datN[Trait=="MR"]
M_MAT_R1<-lmer(yi~scale(log(MixRichness))*scale(StandAge)+scale(log(MixRichness))*scale(MAT)+(1|Study), weights=Wr, datNMR)
anova(M_MAT_R1)

mean(datNMR$MAT)
sd(datNMR[,MAT])
datNMR[,sca_MAT:=scale(MAT)]
datNMR[,sca_richness:=scale(MixRichness)]
mean(datNMR[,log(MixRichness)])
sd(datNMR[,log(MixRichness)])


bootr<-bootMer(M_MAT_R1,fixef,1000)
coefmean_MAT<-summary(M_MAT_R1)$coefficients[ , 1]

datNMR[MAT<=2,MATnew:="2"]##For graph
datNMR[MAT>2&MAT<=8,MATnew:="8"]##For graph
datNMR[MAT>8&MAT<=16,MATnew:="16"]##For graph


coef_MAT<-function(x){
  Tem=x
  cheater=c(((2)-  9.564343)/4.711264,((8)- 9.564343)/4.711264,((16)- 9.564343)/4.711264,
            0)
  cheater2=c("2","8","16","mean")
  if(which(cheater==Tem)<4)richness=datNMR[MATnew==cheater2[which(cheater==Tem)],seq(min(MixRichness),max(MixRichness),length=100)]
  if(which(cheater==Tem)==4)richness=seq(min(datNMR[,MixRichness]),max(datNMR[,MixRichness]),length=100)
  
  richness=log(richness)
  richness=(richness-1.354278)/0.951871
  
  data.table(mean=coefmean_MAT[1]+(coefmean_MAT[2]*richness)+(coefmean_MAT[4]*Tem)+
               (coefmean_MAT[6]*Tem*richness),
             up=sapply(richness,function(x,r=Tem){
               quantile((bootr$t[,"(Intercept)"])+
                          (bootr$t[,"scale(log(MixRichness))"]*x)+
                          (bootr$t[,"scale(MAT)"]*r)+
                          (bootr$t[,"scale(log(MixRichness)):scale(MAT)"]*x*r),prob=0.975)}),
             low=sapply(richness,function(x,r=Tem){
               quantile((bootr$t[,"(Intercept)"])+
                          (bootr$t[,"scale(log(MixRichness))"]*x)+
                          (bootr$t[,"scale(MAT)"]*r)+
                          (bootr$t[,"scale(log(MixRichness)):scale(MAT)"]*x*r),prob=0.025)}),
             Y=richness,
             Richness=exp(richness*0.951871+1.354278))
  
}

to_graphs_MAT<-adply(c(((2)-  9.564343)/4.711264,((8)- 9.564343)/4.711264,((16)- 9.564343)/4.711264,
                       0),.margins=1,
                     .fun=function(x)coef_MAT(x))
to_graphs_MAT$MAT<-rep(c("2","8","16","mean"),each=100)

to_graphs_MAT<-data.table(to_graphs_MAT)
to_graphs_MAT$MAT<-factor(to_graphs_MAT$MAT,levels=c("2","8","16","mean"),labels = c("2","8","16","mean"))
to_graphs_MAT$up<-(exp(to_graphs_MAT$up)-1)*100
to_graphs_MAT$low<-(exp(to_graphs_MAT$low)-1)*100
to_graphs_MAT$mean<-(exp(to_graphs_MAT$mean)-1)*100

FS2<-ggplot()+
  geom_line(data=to_graphs_MAT,aes(x=Richness,y=mean,col=MAT,size=MAT))+
  geom_ribbon(data=to_graphs_MAT,aes(x=Richness,ymin=low,ymax=up,group=MAT,fill=MAT),col=NA,alpha=0.1)+
  theme_classic()+
  scale_x_continuous(trans = "log",limits = c(2,62),  breaks =  c(2, 4, 8, 16, 60),labels =  c(2, 4, 8, 16, 60))+
  scale_size_manual(values=c(1,1,1,1.5))+guides(size=FALSE)+
  scale_colour_manual(values=c('#feb24c','#fc4e2a','#bd0026',"black"),
                      labels=c("2","8","16","mean"),
                      guide = guide_legend(title=expression('MAT'~"("~degree~'C)'),
                                           ncol =1,label.position ="right",
                                           parse=T,
                                           label.hjust=0.5,title.position="top"))+
  scale_fill_manual(values=c('#feb24c','#fc4e2a','#bd0026',"black"),
                    labels=c("2","8","16","mean"),
                    guide = F)+
  geom_text(aes(label="Microbial respiration",x=120^0.5, y=Inf),hjust="middle",vjust=1,size=4.7
            ,color="black")+
  geom_hline(aes(yintercept=0),linetype="dashed",colour="black")+
  xlab("Plant species richness in mixtures")+
  ylab("Effect size (%)")+
  theme(legend.position="right",
        legend.title = element_text(size=13),
        legend.text = element_text(size=12),
        axis.title=element_text(size=14),
        axis.text=element_text(size=12))
FS2
ggsave(filename="FIG S2_1.tiff",dpi =400, plot=FS2,width = 6, height = 4, units = "in")

