#load in packages rm(list = ls(all.names = TRUE)) library(readxl) library(lme4) library(effects) library(ggplot2) library(ggthemes) library(RColorBrewer) library(car) library(MASS) library(lme4) library(rsq) library(scales) library(nlme) library(tidyverse) library(sjPlot) library(DHARMa) library(performance) library(dplyr) library(MuMIn) # Define a theme for ggplot my_theme <- function(){ theme_base()+theme(plot.background=element_rect(fill="white", colour=NA))+ theme(legend.position="none")+theme(text=element_text(family="serif"))+ theme(plot.title = element_text(hjust =-0.06)) } myPalette <- colorRampPalette(brewer.pal(9, "YlOrRd")) myPalette2 <- colorRampPalette(brewer.pal(9, "RdYlBu")) ### LOAD IN DATA ##### d <- read_excel("~/PHD WP2/Submission/EcolAppl round 1/Code and data for submission/moss growth.xlsx") View(d) str(d) d$year <- as.factor(d$year) d$Edge_ID <- as.factor(d$Edge_ID) d$Sample <- as.factor(d$Sample) d$growth <- as.numeric(d$growth) str(d) str #get in summer precipitation and canopy cover etc in d0 <- read_excel("~/PHD WP2/Submission/EcolAppl round 1/Code and data for submission/site-level data.xlsx") View(d0) str(d0) dd <- merge(d,d0,by=c("Edge_ID")) d2 <- subset(dd, year!="2019") #take out 2019 shoots from the dataset d2$DistanceL <- log(d2$Distance+2.5) ## check branching patterns vs distance to forest edge lm1 <- glmer(Nr_segments ~ DistanceL + (1|Edge_ID/Sample/year), data=d2, family=poisson) summary(lm1) #########LINEAR MODELS OF EDGE EFFECTS BETWEEN YEARS ######## #Scaling the variables d2$DistanceLS <- scale(d2$DistanceL) lm1 <- lmer(growth ~ DistanceLS*year +(1|Edge_ID/Sample/Individ), data=d2) Anova(lm1) summary(lm1) r.squaredGLMM(lm1) #check model with sj plot plot_model(lm1) check_model(lm1) #looks good #pairwise comparisons of the slopes between years library("lsmeans") lm1LS <- lstrends(lm1, ~ year, var = "DistanceLS") lm1LS # slope estimates and CIs pairs(lm1LS) #### PLOTTING THE DATA ####### ### make a new dataset for geom_point in the first figure so that it is the mean at each distance at each year d3 <- aggregate(d2$growth, by=list(Edge_ID=d2$Edge_ID, Distance=d2$Distance, Year=d2$year), FUN=mean) d3$DistanceL <- log(d3$Distance+2.5) d3$growth <- d3$x d3$year <- as.factor(d3$Year) str(d3) ## plot instead the predicted lines log transformed x-axis (not in paper) M <- lmer(growth ~ DistanceL*year + (1|Edge_ID/Sample/Individ), data=d2) index2<-data.frame(effect(term="DistanceL*year",mod=M, xlevels=list(DistanceL=seq(-2.3,4.1,0.1))), lines=list(multiline=TRUE)) View(index2) # Plot the graph dev.off() pd1 <- position_dodge(0.1) # move them 1 to the left and right ggplot(index2, aes(DistanceL, fit, group = as.factor(year)))+ geom_ribbon( aes(ymin = lower, ymax = upper, color = NULL), alpha = .1) + geom_smooth(method=lm,se=FALSE,size=1,aes(DistanceL,fit,color=year))+ xlab(" ")+ylab(" ")+ my_theme()+ scale_y_continuous(limit=c(10,55))+ scale_x_continuous(limit=c(0.8,4.1))+ xlab("Distance to forest edge (m) ")+ylab("Growth (mm)")+ geom_point(data=d3, aes(x=DistanceL, y=growth, color=year), alpha=1, size=1,position=pd1)+ scale_color_manual(values=c("#4C7AB7", "#93BFDB", "#FC8D59")) ## plot the predicted lines (Fig 2a.) M <- lmer(growth ~ Distance*year + (1|Edge_ID/Sample/Individ), data=d2) index2<-data.frame(effect(term="Distance*year",mod=M, xlevels=list(Distance=seq(0,50,1))), lines=list(multiline=TRUE)) View(index2) # Plot the graph dev.off() pd <- position_dodge(1) # move them 1 to the left and right ggplot(index2, aes(Distance, fit, color=year))+ scale_color_manual(values=c("#4C7AB7", "#93BFDB", "#FC8D59"))+ stat_smooth(method="lm",formula = (y) ~ log(x+2.5), se=F,size=1)+ # geom_ribbon(aes(ymin = lower, ymax = upper, color=year, alpha = .1)) + #add SE ylim(10,55)+ xlim(-1,51)+ xlab("Distance to forest edge (m) ")+ylab("Growth (mm)")+ my_theme()+ geom_point(data=d3, aes(x=Distance, y=growth, color=year), alpha=1, size=1, position=pd)+ scale_color_manual(values=c("#4C7AB7", "#93BFDB", "#FC8D59")) ### lower and upper SE line### ggplot(index2, aes(Distance, lower, color=year))+ scale_color_manual(values=c("#4C7AB7", "#93BFDB", "#FC8D59"))+ stat_smooth(method="lm",formula = (y) ~ log(x+2.5), se=F,size=0.1)+ # geom_ribbon(aes(ymin = lower, ymax = upper, color=year, alpha = .1)) + #add SE ylim(10,55)+ xlim(-1,51)+ xlab("Distance to forest edge (m) ")+ylab("Growth (mm)")+ my_theme() ggplot(index2, aes(Distance, upper, color=year))+ scale_color_manual(values=c("#4C7AB7", "#93BFDB", "#FC8D59"))+ stat_smooth(method="lm",formula = (y) ~ log(x+2.5), se=F,size=0.1)+ # geom_ribbon(aes(ymin = lower, ymax = upper, color=year, alpha = .1)) + #add SE ylim(10,55)+ xlim(-1,51)+ xlab("Distance to forest edge (m) ")+ylab("Growth (mm)")+ my_theme()