---
title: "Ribbons et al. Danish Tree Species"
author: "Relena Ribbons"
date: "June 6 2018"
output:
  pdf_document: default
  html_document: default
  word_document: default
---
##Set working directory, load libraries
```{r, echo= FALSE}
setwd("~/Dropbox/Denmark/R")
require("vegan")
require("ggplot2")
require("graphics")
source("summary_se.r")
require("gridExtra")
require("car")
require("lme4")
require("lmerTest")
require("lsmeans")
source("multiplot.r")
require("MASS")
require("FactoMineR")
library("devtools")
library("ggbiplot")
#install.packages("piecewiseSEM")
require("corrplot")
require("piecewiseSEM")
require("stringr")
require("multcomp")
require("multcompView")

r.data<-read.csv("DK_data_C_std.csv")
str(r.data)
```

##Barplots of DNA and 15N data- last checked June 6 2018
```{r DNA barplots}
####Figure 2  in the manuscript
##new palette used for final MS: deep red, rockabilly (teal), Now that's my color (purple), caribou sun (orange), bobraun (brown), and Dazure (blue). 
pal<-c("#F97400","#007FFF", "#00A08A",  "#c40b0b",  "#724006","#800080" )

####Genes barplots averaged across sites (one bar for each species only) 
ITS.2<- summarySE(r.data, measurevar="ITS", groupvars=("Tree_Species"))
bac.2<- summarySE(r.data, measurevar="bac", groupvars=("Tree_Species"))
nirS.2<- summarySE(r.data, measurevar="nirS", groupvars=("Tree_Species"))
nirK.2<- summarySE(r.data, measurevar="nirK", groupvars=("Tree_Species"))
AOA.2<- summarySE(r.data, measurevar="AOA", groupvars=("Tree_Species"))
AOB.2<- summarySE(r.data, measurevar="AOB", groupvars=("Tree_Species"))
AOA.AOB.2<- summarySE(r.data, measurevar="AOAAOB", groupvars=("Tree_Species"))
fung.bac.2<- summarySE(r.data, measurevar="ITS16S", groupvars=("Tree_Species"))

#ITS barplot
ITS.plot2<-ggplot(ITS.2, aes(x=Tree_Species, y=ITS,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=ITS+se, ymin=ITS-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(5, 14)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("ITS log gene copies g-1 soil C") +
  annotate("text", x = 0.5, y = 14, label = "A")+
  annotate("text", x= 6, y= 14, label = "p = 0.052")
ITS.plot2

#16S barplot
bact.plot2<-ggplot(bac.2, aes(x=Tree_Species, y=bac,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=bac+se, ymin=bac-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(5, 14.5)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("16S log gene copies g-1 soil C") +
  annotate("text", x = 0.5, y = 14.4, label = "B")+
  annotate("text", x=1, y=14.3, label="A")+
  annotate("text", x=2, y=14.15, label="A")+
  annotate("text", x=3, y=14.15, label="A")+
  annotate("text", x=4, y=14.2, label="A")+
  annotate("text", x=5, y=14.1, label="A")+
  annotate("text", x=6, y=13.3, label="B")+
  annotate("text", x= 6, y= 14.4, label = "p  < 0.001")
bact.plot2

#NIRS barplot
nirS.plot2<-ggplot(nirS.2, aes(x=Tree_Species, y=nirS,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=nirS+se, ymin=nirS-se), width=0,position=position_dodge(.9)) +
  coord_cartesian(ylim = c(5, 14)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("nirS log gene copies g-1 soil C") +
  annotate("text", x = 0.5, y = 14, label = "C")+
  annotate("text", x=1, y=9, label="A")+
  annotate("text", x=2, y=9, label="A")+
  annotate("text", x=3, y=9.2, label="A")+
  annotate("text", x=4, y=9.2, label="A")+
  annotate("text", x=5, y=9, label="A")+
  annotate("text", x=6, y=8.35, label="B")+
  annotate("text", x= 6, y= 14, label = "p < 0.001")
nirS.plot2

#NIRK barplot
nirK.plot2<-ggplot(nirK.2, aes(x=Tree_Species, y=nirK,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=nirK+se, ymin=nirK-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(5, 14)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") +theme(text = element_text(size=9))    +
  xlab (" ") + ylab("nirK log gene copies g-1 soil C") +
  annotate("text", x = 0.5, y = 14, label = "D")+
  annotate("text", x=1, y=10, label="A")+
  annotate("text", x=2, y=10, label="A")+
  annotate("text", x=3, y=10, label="A")+
  annotate("text", x=4, y=10, label="A")+
  annotate("text", x=5, y=10, label="A")+
  annotate("text", x=6, y=9.5, label="B")+
  annotate("text", x= 6, y= 14, label = "p < 0.001")
nirK.plot2

#AOA barplot
AOA.plot2<-ggplot(AOA.2, aes(x=Tree_Species, y=AOA,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  #scale_y_continuous(breaks=c(4,6,8,10,12))+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=AOA+se, ymin=AOA-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(5, 14)) +
  #scale_y_discrete(limits=c("0","5","10", "14"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") +theme(text = element_text(size=9))    +
  xlab (" ") + ylab("AOA amoA log gene copies g-1 soil C") +
  annotate("text", x = 0.5, y = 14, label = "E")+
  annotate("text", x=1, y=10.5, label="AB")+
  annotate("text", x=2, y=11, label="A")+
  annotate("text", x=3, y=11, label="A")+
  annotate("text", x=4, y=11, label="A")+
  annotate("text", x=5, y=11, label="A")+
  annotate("text", x=6, y=10, label="B")+
  annotate("text", x= 6, y= 14, label = "p < 0.001")
AOA.plot2

#AOB barplot
AOB.plot2<-ggplot(AOB.2, aes(x=Tree_Species, y=AOB,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=AOB+se, ymin=AOB-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(5, 14)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("AOB amoA log gene copies g-1 soil C")+
  annotate("text", x = 0.5, y =14, label = "F")+
  annotate("text", x=1, y=11.4, label="AB")+
  annotate("text", x=2, y=10.8, label="AB")+
  annotate("text", x=3, y=11, label="AB")+
  annotate("text", x=4, y=11.4, label="A")+
  annotate("text", x=5, y=11, label="AB")+
  annotate("text", x=6, y=10.8, label="B")+
  annotate("text", x= 6, y= 14, label = "p = 0.01")
AOB.plot2

#Ratio of AOA/AOB gene copies barplot
AOA.AOB.plot<-ggplot(AOA.AOB.2, aes(x=Tree_Species, y=AOAAOB,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=AOAAOB+se, ymin=AOAAOB-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(0.25, 1.1)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("AOA/AOB amoA")+
  annotate("text", x=0.5, y=1.1, label="H")+  
  annotate("text", x=1, y=.97, label="AB")+
  annotate("text", x=2, y=1.05, label="A")+
  annotate("text", x=3, y=1.05, label="AB")+
  annotate("text", x=4, y=1, label="AB")+
  annotate("text", x=5, y=1.05, label="AB")+
  annotate("text", x=6, y=1, label="B")+
  annotate("text", x=6, y=1.1, label="p = 0.016")
AOA.AOB.plot

#Ratio of bac/fung gene copies barplot
fung.bac.plot<-ggplot(fung.bac.2, aes(x=Tree_Species, y=ITS16S,  fill=Tree_Species)) + 
  geom_bar(stat="boxplot", position=position_dodge(), width=1)+
  scale_fill_manual(values = pal)+
  geom_errorbar(aes(ymax=ITS16S+se, ymin=ITS16S-se), width=0,position=position_dodge(.9)) +  
  coord_cartesian(ylim = c(0.25, 1)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=9))    +
  xlab (" ") + ylab("ITS/16S")+
  annotate("text", x=0.5, y=1, label="G")+
  annotate("text", x=6, y=1, label="p = 0.08")
fung.bac.plot

#Figure 2
#tiff("Figure.2.Final.tiff", width = 12, height = 10, units = 'in', res = 300)
grid.arrange(ITS.plot2, bact.plot2, nirS.plot2, nirK.plot2, AOA.plot2, AOB.plot2, AOA.AOB.plot,fung.bac.plot, ncol=2)
#dev.off()

```

```{r 15N barplots}
#N rates by tree species only with C standardized data these units are mg N/kg soil C/day

####Figure 1
ammo1<- summarySE(r.data, measurevar="GA", groupvars=("Tree_Species"))
ammo.plot1<-ggplot(ammo1, aes(x=Tree_Species, y=GA, fill=Tree_Species)) +
  geom_bar(stat="boxplot", position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=GA-se, ymax=GA+se), width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 1150))+
  xlab (" ") + ylab("Gross ammonification")+ 
  annotate("text", x = 0.5, y = 1150, label = "E")+
  annotate("text", x=1, y=1070, label="A")+
  annotate("text", x=2, y=400, label="B")+
  annotate("text", x=3, y=350, label="B")+
  annotate("text", x=4, y=500, label="AB")+
  annotate("text", x=5, y=400, label="AB")+
  annotate("text", x=6, y=70, label="C")+
  annotate("text", x=6, y=1150, label="p < 0.001")
ammo.plot1

#Gross NH4 consumption
nh4cons1<- summarySE(r.data, measurevar="AC", groupvars=c("Tree_Species"))
nh4cons.plot1<-ggplot(nh4cons1, aes(x=Tree_Species, y=AC, fill=Tree_Species)) +
  geom_bar(stat="boxplot",  position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=AC-se, ymax=AC+se), width=0, position=position_dodge(.9)) +
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 1600))+
  xlab (" ") + ylab("NH4+ consumption")  +
  annotate("text", x = 0.5, y = 1600, label = "G")+
  annotate("text", x=1, y=1600, label="A")+
  annotate("text", x=2, y=750, label="AB")+
  annotate("text", x=3, y=400, label="B")+
  annotate("text", x=4, y=700, label="AB")+
  annotate("text", x=5, y=650, label="AB")+
  annotate("text", x=6, y=150, label="B")+
  annotate("text", x=6, y=1600, label="p = 0.003")
nh4cons.plot1

str(r.data)
#Net ammonification (averaging across sites):
net.ammo1<- summarySE(r.data, measurevar="NA.", groupvars=c("Tree_Species"))
net.ammo.plot1<-ggplot(net.ammo1, aes(x=Tree_Species, y=NA., fill=Tree_Species)) +
  geom_bar(stat="boxplot", position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=NA.-se, ymax=NA.+se),  width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(-500, 100))+
  xlab (" ") + ylab("Net Ammonification")  +
  annotate("text", x = 0.5, y = 100, label = "C")+
  annotate("text", x=1, y=25, label="A")+
  annotate("text", x=2, y=25, label="AB")+
  annotate("text", x=3, y=25, label="B")+
  annotate("text", x=4, y=25, label="AB")+
  annotate("text", x=5, y=25, label="AB")+
  annotate("text", x=6, y=25, label="B")+
  annotate("text", x=6, y=100, label="p =0.04")
net.ammo.plot1

#Gross nitrification by species only 
grossnit1<- summarySE(r.data, measurevar="GN", groupvars=c("Tree_Species"))
grossnit.plot1<-ggplot(grossnit1, aes(x=Tree_Species, y=GN, fill=Tree_Species)) +
  geom_bar(stat="boxplot",  position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=GN-se, ymax=GN+se), width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 1150))+
  xlab (" ") + ylab("Gross Nitrification") +
  annotate("text", x = 0.5, y = 1150, label = "F")+
  annotate("text", x=1, y=350, label="AB")+
  annotate("text", x=2, y=300, label="AB")+
  annotate("text", x=3, y=100, label="B")+
  annotate("text", x=4, y=250, label="AB")+
  annotate("text", x=5, y=500, label="A")+
  annotate("text", x=6, y=500, label="A")+
  annotate("text", x=6, y=1150, label="p = 0.006")
grossnit.plot1

#Gross NO3 consumption plot by species only
n03cons1<- summarySE(r.data, measurevar="NC", groupvars=c("Tree_Species"))
n03cons.plot1<-ggplot(n03cons1, aes(x=Tree_Species, y=NC, fill=Tree_Species)) +
  geom_bar(stat="boxplot", position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=NC-se, ymax=NC+se), width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 1600))+
  xlab (" ") + ylab("NO3- Consumption")  +
  annotate("text", x = 0.5, y = 1600, label = "H") + 
  annotate("text", x=1, y=400, label="AB")+
  annotate("text", x=2, y=300, label="AB")+
  annotate("text", x=3, y=200, label="B")+
  annotate("text", x=4, y=350, label="AB")+
  annotate("text", x=5, y=600, label="A")+
  annotate("text", x=6, y=500, label="A")+
  annotate("text", x=6, y=1600, label="p = 0.005")
n03cons.plot1

#Net nitrification by species only
netnit1<- summarySE(r.data, measurevar="NN", groupvars=c("Tree_Species"))
netnit.plot1<-ggplot(netnit1, aes(x=Tree_Species, y=NN, fill=Tree_Species)) +
  geom_bar(stat="boxplot", position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=NN-se, ymax=NN+se), width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(-500,100))+
  xlab (" ") + ylab("Net Nitrification")  +
  annotate("text", x = 0.5, y = 100, label = "D")+
  annotate("text", x=6, y=100, label="p = 0.47")
netnit.plot1

##############
#Ammonium and nitrate concentrations by species
ammonia<- summarySE(r.data, measurevar="NH4", groupvars=c("Tree_Species"))
ammonia.plot<-ggplot(ammonia, aes(x=Tree_Species, y=NH4, fill=Tree_Species)) +
  geom_bar(stat="identity",  position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=NH4-se, ymax=NH4+se),  width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 420))+
  xlab (" ") + ylab("Ammonium") + 
  annotate("text", x = 0.5, y = 420, label = "A")+
  annotate("text", x=1, y=260, label="AB")+
  annotate("text", x=2, y=230, label="AB")+
  annotate("text", x=3, y=200, label="AB")+
  annotate("text", x=4, y=330, label="A")+
  annotate("text", x=5, y=250, label="AB")+
  annotate("text", x=6, y=170, label="B")+
  annotate("text", x=6, y=420, label="p = 0.05")
ammonia.plot

nitrate<- summarySE(r.data, measurevar="NO3", groupvars=c("Tree_Species"))
nitrate.plot<-ggplot(nitrate, aes(x=Tree_Species, y=NO3, fill=Tree_Species)) +
  geom_bar(stat="identity",  position=position_dodge(), width=1) + 
  scale_fill_manual(values = pal)+
  geom_errorbar (aes(ymin=NO3-se, ymax=NO3+se), width=0, position=position_dodge(.9)) + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position="none") + theme(text = element_text(size=12))    +
  scale_y_continuous(limits = c(0, 420))+
  xlab (" ") + ylab("Nitrate") +
  annotate("text", x = 0.5, y = 420, label = "B")+
  annotate("text", x= 6, y= 420, label = "p = 0.108")
#nitrate.plot

#tiff("Figure.1.Final.tiff", width = 8, height = 11, units = 'in', res = 300)
grid.arrange(ammonia.plot, nitrate.plot, net.ammo.plot1, netnit.plot1, ammo.plot1, grossnit.plot1, nh4cons.plot1, n03cons.plot1,  ncol=2)
#dev.off()


#Figure 1
grid.arrange(ammonia.plot, nitrate.plot, net.ammo.plot1, netnit.plot1, ammo.plot1, grossnit.plot1, nh4cons.plot1, n03cons.plot1,  ncol=2)

#Figure 2
grid.arrange(ITS.plot2, bact.plot2, nirS.plot2, nirK.plot2, AOA.plot2, AOB.plot2, AOA.AOB.plot,fung.bac.plot, ncol=2)
```

#ANOVAs and post-hoc tests
```{r ANOVAs}
(pH_lme<-lmer(pH ~ Tree_Species + (1 | Site), r.data))
summary(pH_lme)
Anova((pH_lme), type="3")

(tn_lme<-lmer(tn ~ Tree_Species + (1 | Site), r.data))
summary(tn_lme)
Anova((tn_lme), type="3")

(tc_lme<-lmer(tc ~ Tree_Species + (1 | Site), r.data))
summary(tc_lme)
Anova((tc_lme), type="3")

(cn_lme<-lmer(cn ~ Tree_Species + (1 | Site), r.data))
summary(cn_lme)
Anova((cn_lme), type="3")

####2-way ANOVA using linear mixed effects models w/  unbalanced design-no Ash at one site.
#Spp effects for all, except AOB amoA

(ITS_lme<-lmer(ITS ~ Tree_Species + (1 | Site), r.data))
summary(ITS_lme)
Anova((ITS_lme), type="3")

(bac_lme<-lmer(bac ~ Tree_Species + (1 | Site), r.data))
summary(bac_lme)
Anova((bac_lme), type="3")

(nirK_lme<-lmer(nirK ~ Tree_Species + (1 | Site), r.data))
summary(nirK_lme)
Anova((nirK_lme), type="3")

(nirS_lme<-lmer(nirS ~ Tree_Species + (1 | Site), r.data))
summary(nirS_lme)
Anova((nirS_lme), type="3")

(AOA_lme<-lmer(AOA ~ Tree_Species + (1 | Site), r.data))
summary(AOA_lme)
Anova((AOA_lme), type="3")

(AOB_lme<-lmer(AOB ~ Tree_Species + (1 | Site), r.data))
summary(AOB_lme)
Anova((AOB_lme), type="3")

(fung.bac_lme<-lmer(ITS16S ~ Tree_Species + (1| Site), r.data))
summary(fung.bac_lme)
Anova((fung.bac_lme), type="3")

(AOA.AOB_lme<-lmer(AOAAOB ~ Tree_Species + (1| Site), r.data))
summary(AOA.AOB_lme)
Anova((AOA.AOB_lme), type="3")

####  15N data
###linear mixed effects models of 15N data with unbalanced design-no Ash at one site
(ammonia_lme<-lmer(NH4 ~ Tree_Species + (1 | Site), r.data))
summary(ammonia_lme)
Anova((ammonia_lme), type="3")

(net_ammonification_lme<-lmer(NA. ~ Tree_Species + (1 | Site), r.data))
summary(net_ammonification_lme)
Anova((net_ammonification_lme), type="3")

(gross_ammonification_lme<-lmer(GA ~ Tree_Species + (1 | Site), r.data))
summary(gross_ammonification_lme)
Anova((gross_ammonification_lme), type="3")

(gross_NH4_consumption_lme<-lmer(AC ~ Tree_Species + (1 | Site), r.data))
summary(gross_NH4_consumption_lme)
Anova((gross_NH4_consumption_lme), type="3")

(nitrate_lme<-lmer(NO3 ~ Tree_Species + (1 | Site), r.data))
summary(nitrate_lme)
Anova((nitrate_lme), type="3")

(net_nitrification_lme<-lmer(NN ~ Tree_Species + (1 | Site), r.data))
summary(net_nitrification_lme)
Anova((net_nitrification_lme), type="3")

(gross_nitrification_lme<-lmer(GN ~ Tree_Species + (1 | Site), r.data))
summary(gross_nitrification_lme)
Anova((gross_nitrification_lme), type="3")

(gross_NO3_consumption_lme<-lmer(NC ~ Tree_Species + (1 | Site), r.data))
summary(gross_NO3_consumption_lme)
Anova((gross_NO3_consumption_lme), type="3")


#Posthoc comparisons

ph.pairs<-pairs(lsmeans(pH_lme, ~Tree_Species), adjust="mvt")
tc.pairs<-pairs(lsmeans(tc_lme, ~Tree_Species), adjust="mvt")
tn.pairs<-pairs(lsmeans(tn_lme, ~Tree_Species), adjust="mvt")
cn.pairs<-pairs(lsmeans(cn_lme, ~Tree_Species), adjust="mvt")

ph.pairs
tc.pairs
tn.pairs
cn.pairs

nh4.pairs<-pairs(lsmeans(ammonia_lme, ~Tree_Species), adjust="mvt")
na.pairs<-pairs(lsmeans(net_ammonification_lme, ~Tree_Species), adjust="mvt")
ac.pairs<-pairs(lsmeans(gross_NH4_consumption_lme, ~Tree_Species), adjust="mvt")
ga.pairs<-pairs(lsmeans(gross_ammonification_lme, ~Tree_Species), adjust="mvt")

no3.pairs<-pairs(lsmeans(nitrate_lme, ~Tree_Species), adjust="mvt")
nn.pairs<-pairs(lsmeans(net_nitrification_lme, ~Tree_Species), adjust="mvt")
nc.pairs<-pairs(lsmeans(gross_NO3_consumption_lme, ~Tree_Species), adjust="mvt")
gn.pairs<-pairs(lsmeans(gross_nitrification_lme, ~Tree_Species), adjust="mvt")

nh4.pairs #no sig. diffs
na.pairs #ash sig. dif from lime and spruce
ga.pairs #ash sig. diff from beech and spruce, oak diff. from spruce, 
ac.pairs #ash sig. dif from lime an spruce, almost beech (0.06)

no3.pairs #no sig. diffs
nn.pairs #no sig. diffs
gn.pairs #lime sig. diff from oak and spruce
nc.pairs #lime sig. diff from oak and spruce, almost beech from oak (0.06)

its.pairs<-pairs(lsmeans(ITS_lme, ~Tree_Species), adjust="mvt")
bac.pairs<-pairs(lsmeans(bac_lme, ~Tree_Species), adjust="mvt")
nirS.pairs<-pairs(lsmeans(nirS_lme, ~Tree_Species), adjust="mvt")
nirK.pairs<-pairs(lsmeans(nirK_lme, ~Tree_Species), adjust="mvt")
AOA.pairs<-pairs(lsmeans(AOA_lme, ~Tree_Species), adjust="mvt")
AOB.pairs<-pairs(lsmeans(AOB_lme, ~Tree_Species), adjust="mvt")
fung.bac.pairs<-pairs(lsmeans(fung.bac_lme, ~Tree_Species), adjust="mvt")
AOA.AOB.pairs<-pairs(lsmeans(AOA.AOB_lme, ~Tree_Species), adjust="mvt")

its.pairs 
#beech sig diff from oak and spruce, lime sig diff from oak and spruce,maple sig diff from oak and spruce.
bac.pairs 
#spruce sig. diff from maple, lime, and ash. Beech, and maple sig diff from oak.
nirK.pairs 
#spruce sig. diff from maple, oak, lime, and ash. Beech, and maple sig diff from oak 
#(lime almost diff from oak 0.08), 
nirS.pairs 
#spruce sig. diff for maple, oak, lime, beech, and ash. Beech, maple and lime sig diff from oak.
AOA.pairs 
#spruce sig. diff from oak, maple, lime, and beech. beech and Maple sig diff from oak. Ash sig diff maple
#ash almost diff fomr spruce 0.07). 
AOB.pairs 
#Ash, beech, lime, and maple diff from spruce. Oak diff from maple, beech.
#ash almost diff from maple (0.09)
fung.bac.pairs 
#no sig. diffs.
AOA.AOB.pairs 
#beech sig. diff from spruce

###ph, TC, TN, and CN ANOVAs and paired comparisons among the 2 sites
(pH_lm2<-lm(pH ~ Site, r.data))
summary(pH_lm2)
Anova((pH_lm2), type="3")

(tn_lm2<-lm(tn ~ Site, r.data))
summary(tn_lm2)
Anova((tn_lm2), type="3")

(tc_lm2<-lm(tc ~ Site, r.data))
summary(tc_lm2)
Anova((tc_lm2), type="3")

(cn_lm2<-lm(cn ~ Site, r.data))
summary(cn_lm2)
Anova((cn_lm2), type="3")

ph.pairs2<-pairs(lsmeans(pH_lm2, ~Site), adjust="mvt")
tc.pairs2<-pairs(lsmeans(tc_lm2, ~Site), adjust="mvt")
tn.pairs2<-pairs(lsmeans(tn_lm2, ~Site), adjust="mvt")
cn.pairs2<-pairs(lsmeans(cn_lm2, ~Site), adjust="mvt")

ph.pairs2
tc.pairs2
tn.pairs2
cn.pairs2

##Annotate Figure 1 with letters to indicate sig. differences manually in each figure.
grid.arrange(ammonia.plot, nitrate.plot, net.ammo.plot1, netnit.plot1, ammo.plot1, grossnit.plot1, nh4cons.plot1, n03cons.plot1,  ncol=2)
```

##PCA  Supplementary Figure 2
```{r PCA }
####Supplementary Figure 2
#load data file
pal<-c("#F97400","#B284BE", "#93C572",  "#c40b0b",  "#333399","#006A4E")

p.data<-read.csv("DK_data_C_std.csv", header=TRUE)
p.data<-na.omit(p.data)
str(p.data)
p.data= p.data[,c(1:21)]
data.preds= p.data[,c(4:21)]
data.stand<-decostand(data.preds,"stand") 
#data averaged across runs
#correlation matrix 
#cor(data.preds)
#str(data.preds)
#str(p.data)
pca.data= data.preds[,c(1:9,12:18)]
#str(pca.data)
#PCA of soil + genes using FactoMineR package with location as supplementary variables
relena.pca = PCA(pca.data, scale.unit=TRUE, ncp=5, quanti.sup=c(1:3), graph=T)

# Figure 3
#PCA for coordinate plots soil + genes excluding ratio of ITS:16S and AOA:AOB and excluding total C and total N, but including C:N ratio. used in Manuscript with grouping by Species: Figure 3.
pca <- prcomp(p.data[,c(4:12,15:21)], scale. = TRUE)
plot(pca, type="l")
Site<-p.data$Site
Species<-p.data$Tree_Species
pca.total.species <- ggbiplot(pca, obs.scale = 1, var.scale = 1, varname.adjust=3, varname.size=3.5, group=r.data$Tree_Species, ellipse = T) + 
  scale_colour_manual(values = pal) + 
  #scale_shape_manual(values = c(15, 8, 17, 18, 19, 20))+ 
  geom_point(size = 3, aes(colour=r.data$Tree_Species,shape=Site)) + theme_bw() + 
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+ 
  theme(legend.key = element_rect(colour = "white")) + theme(text=element_text(size = 12, colour="black"))
print(pca.total.species)


# Figure 4
#PCA for coordinate plots genes only
#Final version that is used in Manuscript, grouping by Species- this version without C or N, but including C:N ratio.
pca.genes <- prcomp(p.data[,(15:21)], scale. = TRUE)
pca.genes.species <- ggbiplot(pca.genes, obs.scale = 1, var.scale = 1, varname.adjust=3, varname.size=3.5, group=r.data$Tree_Species, ellipse = T) + 
  scale_colour_manual(values = pal) + 
  #scale_shape_manual(values = c(15, 8, 17, 18, 19, 20))+ 
  geom_point(size = 3, aes(colour=r.data$Tree_Species,shape=Site)) + theme_bw() + 
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+ 
  theme(legend.key = element_rect(colour = "white")) + theme(text=element_text(size = 12, colour="black"))
print(pca.genes.species)


#tiff("Figure.3.PCA.total.species.tiff", width = 4, height = 6, units = 'in', res = 300)
grid.arrange(pca.total.species, ncol=1)
#dev.off()

#tiff("Figure.4.PCA.genes.species.tiff", width = 4, height = 6, units = 'in', res = 300)
pca.genes.species
#dev.off()
```

##Figures 5 and 6 linear relationships with pH- last checked June 6 2018
```{r Linear relationships with pH}
##red are ash, teal are beech, green are lime, orange are maple, brown are oak, and blue are spruce
###pal<-c("#c40b0b", "#00A08A", "#05bc6d", "#F97400", "#724006", "#216af2")


#solid lines are default, I am manually specifying dashed lines for insignificant relationships, leaving solid lines are signifiant relationships. Significant here means from slope differs to 0. 
# 16s ~ pH is only NOT sig. for spruce
pH.16S.plot<-ggplot(r.data, aes(x=pH, y=bac,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","dashed", "dashed"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("16S") 
pH.16S.plot

pH.ITS.plot<-ggplot(r.data, aes(x=pH, y=ITS,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","solid", "solid"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("ITS") 
pH.ITS.plot

pH.nirK.plot<-ggplot(r.data, aes(x=pH, y=nirK,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","dashed", "solid"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("nirK") 
pH.nirK.plot

pH.nirS.plot<-ggplot(r.data, aes(x=pH, y=nirS,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","dashed", "solid"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("nirS") 
pH.nirS.plot

pH.AOA.plot<-ggplot(r.data, aes(x=pH, y=AOA,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("solid", "solid", "dashed","dashed","solid", "solid"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("AOA") 
pH.AOA.plot

pH.AOB.plot<-ggplot(r.data, aes(x=pH, y=AOB,  colour=Tree_Species, linetype=Tree_Species)) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","solid", "dashed"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="none") + theme(text = element_text(size=14))    +
  xlab ("pH") + ylab("AOB") 
pH.AOB.plot

#tiff("Figure.5.pH.models.genes.tiff", width = 8, height = 12, units = 'in', res = 300)
grid.arrange(pH.16S.plot, pH.ITS.plot, pH.nirK.plot, pH.nirS.plot, pH.AOA.plot, pH.AOB.plot, ncol=2)
#dev.off()


AOB.GN.plot<-ggplot(r.data, aes(x=AOB, y=GN,  colour=Tree_Species,linetype=Tree_Species )) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
  scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","dashed", "dashed"))+
  theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="right") + theme(text = element_text(size=14))  +  xlab ("AOB") + ylab("Gross nitrification") 
AOB.GN.plot

AOA.GN.plot<-ggplot(r.data, aes(x=AOA, y=GN,  colour=Tree_Species, linetype=Tree_Species )) + 
  geom_point()+
  geom_smooth(method = "lm", se = F)+
  scale_colour_manual(values = pal)+
 scale_linetype_manual(values=c("dashed", "dashed", "dashed","dashed","dashed", "solid"))+
   theme_bw() +   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+   theme(legend.position="right") + theme(text = element_text(size=14))  +  xlab ("AOA") + ylab("Gross nitrification") 
AOA.GN.plot

#tiff("Figure.6.AOB.AOA.GN.tiff", width = 8, height = 6, units = 'in', res = 300)
grid.arrange(AOA.GN.plot, AOB.GN.plot,ncol=1)
#dev.off()

```

#Table 3 stats
```{r Table 3 statistics to accompany figure 5 pH linear relationships}
str(r.data)

(aob_lme<-lmer(AOB ~ Tree_Species*pH + (1 | Site), r.data))
summary(aob_lme)
Anova((aob_lme), type="3")

(aoa_lme<-lmer(AOA ~ Tree_Species*pH + (1 | Site), r.data))
summary(aoa_lme)
Anova((aoa_lme), type="3")

(bac_lme<-lmer(bac ~ Tree_Species*pH + (1 | Site), r.data))
summary(bac_lme)
Anova((bac_lme), type="3")

(its_lme<-lmer(ITS ~ Tree_Species*pH + (1 | Site), r.data))
summary(its_lme)
Anova((its_lme), type="3")

(nirK_lme<-lmer(nirK ~ Tree_Species*pH + (1 | Site), r.data))
summary(nirK_lme)
Anova((nirK_lme), type="3")

(nirS_lme<-lmer(nirS ~ Tree_Species*pH + (1 | Site), r.data))
summary(nirS_lme)
Anova((nirS_lme), type="3")

(GN.aoa_lme<-lmer(GN ~ Tree_Species*AOA + (1 | Site), r.data))
summary(GN.aoa_lme)
Anova((GN.aoa_lme), type="3")

(GN.aob_lme<-lmer(GN ~ Tree_Species*AOB + (1 | Site), r.data))
summary(GN.aob_lme)
Anova((GN.aob_lme), type="3")


### These models not used in Table 3, relevant for Supplementary figure 4 though.
(GA_lme<-lmer(GA ~ Tree_Species*pH + (1 | Site), r.data))
summary(GA_lme)
Anova((GA_lme), type="3")

(AC_lme<-lmer(AC ~ Tree_Species*pH + (1 | Site), r.data))
summary(AC_lme)
Anova((AC_lme), type="3")

(NA_lme<-lmer(NA. ~ Tree_Species*pH + (1 | Site), r.data))
summary(NA_lme)
Anova((NA_lme), type="3")

(GN_lme<-lmer(GN ~ Tree_Species*pH + (1 | Site), r.data))
summary(GN_lme)
Anova((GN_lme), type="3")

(NC_lme<-lmer(NC ~ Tree_Species*pH + (1 | Site), r.data))
summary(NC_lme)
Anova((NC_lme), type="3")

(NN_lme<-lmer(NN ~ Tree_Species*pH + (1 | Site), r.data))
summary(NN_lme)
Anova((NN_lme), type="3")

#testing alternative for GN:
(GN_lme2<-lmer(GN ~ Tree_Species*pH + (1 | Site), r.data))
summary(GN_lme2)
Anova((GN_lme2), type="3")
lsmeans(GN_lme2, pairwise ~Tree_Species * pH)

#Post hoc paired comparisons 
(bac.pH.pairs<-pairs(lsmeans(bac_lme, ~Tree_Species), adjust="mvt"))
(its.pH.pairs<-pairs(lsmeans(its_lme, ~Tree_Species), adjust="mvt"))
#ash almost diff from spruce p = 0.057
(nirK.pH.pairs<-pairs(lsmeans(nirK_lme, ~Tree_Species), adjust="mvt"))
(nirS.pH.pairs<-pairs(lsmeans(nirS_lme, ~Tree_Species), adjust="mvt"))
(aoa.pH.pairs<-pairs(lsmeans(aoa_lme, ~Tree_Species), adjust="mvt"))
(aob.pH.pairs<-pairs(lsmeans(aob_lme, ~Tree_Species), adjust="mvt"))

(GA.pH.pairs<-pairs(lsmeans(GA_lme, ~Tree_Species), adjust="mvt"))
(AC.pH.pairs<-pairs(lsmeans(AC_lme, ~Tree_Species), adjust="mvt"))
(NA.pH.pairs<-pairs(lsmeans(NA_lme, ~Tree_Species), adjust="mvt"))
(GN.pH.pairs<-pairs(lsmeans(GN_lme, ~Tree_Species), adjust="mvt"))
#Beech sig. diff from ash, oak, spruce, almost maple.
(NC.pH.pairs<-pairs(lsmeans(NC_lme, ~Tree_Species), adjust="mvt"))
#Beech almost diff from spruce p = 0.06; sig diff from maple
(NN.pH.pairs<-pairs(lsmeans(NN_lme, ~Tree_Species), adjust="mvt"))


#Comparisons with automatic groupings when different.
#No differences for any genes
cld(bac.pH.pairs)
cld(its.pH.pairs)
cld(nirK.pH.pairs)
cld(nirS.pH.pairs)
cld(aoa.pH.pairs)
cld(aob.pH.pairs)

#Sig. grouping diffs for Gross nitrification and gross nitrate consumption.
#No differences for any other N transformations
cld(GA.pH.pairs)
cld(AC.pH.pairs)
cld(NA.pH.pairs)
cld(GN.pH.pairs)
cld(NC.pH.pairs)
cld(NN.pH.pairs)

```

##Contrasts to test significance of slopes in Figure 5 models.These are used to determine dashed vs. solid lines in figures. These contrasts are the most recent.last checked June 6 2018
```{r}
contrast.matrixg <- rbind(
  `Ash_pH = 0` =           c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
  `Beech_pH = 0` =         c(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0),
  `Lime_Ph = 0` =          c(0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0),
  `Maple_pH = 0` =         c(0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0),
  `Oak_ph = 0` =           c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0),
  `Spruce_ph = 0` =        c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1))
#aoa_lme<-lmer(AOA ~ Tree_Species*pH + (1 | Site), r.data)

comps <- glht(aoa_lme, contrast.matrixg)
summary(comps)
#ash, beech, oak, and spruce are sig. for AOA:pH

comps1 <- glht(aob_lme, contrast.matrixg)
summary(comps1)
#oak are sig. for AOB:pH

comps2 <- glht(bac_lme, contrast.matrixg)
summary(comps2)
#nothing significant for bacterial 6S

comps3 <- glht(nirK_lme, contrast.matrixg)
summary(comps3)
#spruce are sig for nirK:pH

comps4 <- glht(nirS_lme, contrast.matrixg)
summary(comps4)
#spruce are sig for nirS:pH

comps5 <- glht(its_lme, contrast.matrixg)
summary(comps5)
#spruce and oak sig for ITS:pH

comps6 <- glht(GN.aob_lme, contrast.matrixg)
summary(comps6)
#nothing sig for GN:AOB

comps7 <- glht(GN.aoa_lme, contrast.matrixg)
summary(comps7)
#spruce sig for GN:AOA

```

