General project set-up

# Get all libraries and sources required to run the script

library(dplyr)
library(plyr)
library(reshape2)
library(ggplot2)
library(ggthemes)

theme_set (theme_classic() + theme(panel.grid.major = element_blank(),
                              panel.grid.minor = element_blank(), 
                              axis.line = element_line(colour = "black"),
                              legend.position="none",
                              axis.text.x = element_text(angle = 90, vjust = 0.5),
                              plot.title = element_text(size=12, face="bold"),
                              #panel.border = element_rect(colour = "black", fill=NA, size=1)
                              panel.border = element_blank()
                              ))

Data and data clean-up

# 1. Import data: 

  # Long format Ssid YII
    YII.Tall<-read.csv("YII_tall.csv")
  #summary(YII.Tall)


# 2. Data clean-up an types: 
  
  # Variable types 
    YII.Tall$Time<-as.numeric(YII.Tall$Time)
    YII.Tall$Date<-as.Date(YII.Tall$Date, "%Y-%m-%d")
    
  # Treatments
    YII.Tall$Nutrients<-factor(YII.Tall$Nutrients, 
                             levels= c("Ambient", "NH4"), ordered=TRUE)
    YII.Tall$Heat<-factor(YII.Tall$Heat, 
                             levels= c("No", "Yes"), ordered=TRUE)
    YII.Tall$Treatment<-factor(YII.Tall$Treatment, 
                             levels= c("Control", "NH4", "Heat"), ordered=TRUE)
    
  # Replicates
    YII.Tall$Genotype<-factor(YII.Tall$Genotype, ordered=FALSE)
  
  summary(YII.Tall)
##       Time          Date               Genotype     Fragment    Nutrients 
##  Min.   :1.0   Min.   :2020-11-13   FM14   :14   207    : 2   Ambient:50  
##  1st Qu.:1.0   1st Qu.:2020-11-13   FM9    :14   214    : 2   NH4    :26  
##  Median :1.5   Median :2020-11-13   Elkhorn:12   217    : 2               
##  Mean   :1.5   Mean   :2020-11-13   FM19   :12   220    : 2               
##  3rd Qu.:2.0   3rd Qu.:2020-11-14   FM6    : 8   224    : 2               
##  Max.   :2.0   Max.   :2020-11-14   U44    : 8   225    : 2               
##                                     (Other): 8   (Other):64               
##   Heat      Treatment       YII                    Sample  
##  No :56   Control:30   Min.   :0.0670   2020-11-13_207: 1  
##  Yes:20   NH4    :26   1st Qu.:0.1415   2020-11-13_214: 1  
##           Heat   :20   Median :0.2925   2020-11-13_217: 1  
##                        Mean   :0.3060   2020-11-13_220: 1  
##                        3rd Qu.:0.4718   2020-11-13_224: 1  
##                        Max.   :0.5380   2020-11-13_225: 1  
##                                         (Other)       :70

Data exploration

Genotype

# Genotype

YII_Genet<- ggplot(YII.Tall, aes (Time, YII, colour=Genotype)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  #geom_jitter(alpha=0.5, shape=21)+
  theme(legend.position = "bottom")+
  scale_y_continuous(limits = c(0, .73),
                         breaks = seq(0, 0.7,0.2),  
                         expand = c(0.01, 0.01),
                         name=("YII (Fv/Fm)"))
  
YII_Genet

YII_Genet+ facet_wrap(~Nutrients)

YII_Genet+ facet_wrap(~Heat)

YII_Genet+ facet_wrap(~Treatment)

Treatment

YII_Treatment<- ggplot(YII.Tall, aes (Time, YII, colour=Treatment)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_point(shape=21)+
  #geom_jitter(alpha=0.5, shape=21)+
  theme(legend.position = "bottom")+
  scale_y_continuous(limits = c(0, .73),
                         breaks = seq(0, 0.7,0.2),  
                         expand = c(0.01, 0.01),
                         name=("YII (Fv/Fm)")) 
  
YII_Treatment

YII_Treatment+ facet_wrap(~Genotype)

Fragment

YII_Frag<- ggplot(YII.Tall, aes (Genotype, YII,
                                colour=factor(Treatment),
                                shape=factor(Time))) + 
      geom_point(size=3, alpha=.5)+
      
     scale_y_continuous(limits = c(0.0, .7),
                         breaks = seq(0, 0.7, 0.2),  
                         expand = c(0, 0),
                         name=("YII (Fv/Fm)"))+
    
    theme(legend.position="bottom",
        legend.title = element_blank(), 
        strip.background =element_rect(fill=NA)) 
YII_Frag

Model selection

# Libraries 
  library(lme4)
  library(multcomp)
  library(multcompView)
  library(emmeans)
  library(effects)
  library(lmerTest)

# More complex model 
    
# LM_1 <- lmer(YII ~ Treatment * Time + 
#                              (1|Genotype/Fragment), REML=TRUE, data= YII.Tall)
# 
# step(LM_1)
# LM_2 <- lmer(YII ~ Nutrients * Days + 
#                              (Nutrients|Genotype), REML=TRUE,  data= YII.Tall)
#  
# LM_3 <- lmer(YII ~ Nutrients * Days + 
#                              (1|Genotype), REML=TRUE, data= YII.Tall)
#  
# LM_4 <- lm(YII ~ Nutrients * Days, REML = FALSE, data= YII.Tall)
# 
# # Select model
# 
# anova(LM_1, LM_2, refit=FALSE)
# anova(LM_2, LM_3, refit=FALSE)
# anova(LM_3, LM_4)
#  
# # Final mdel 
# LM_Nutrients_Days<-lmer(YII ~ Nutrients * Days + 
#                              (Nutrients|Genotype), data= YII.nutrients)
#   anova(LM_1)
#   summary(LM_1)
#   coef(LM_1)
#   fitted(LM_1)
#       
#       layout(matrix(1:4,2,2))  
#       plot(LM_1)  
#       
# plot(Effect(c("Nutrients","DayF"), LM_1), x.var="DayF", multiline=T, ci.style="bars")
# 
# #  Pair-wise comparisons
# cld(emmeans(LM_1, "Nutrients"))
# YIIAcerEmm<-cld(emmeans(LM_1, specs = c("Nutrients", "DayF")))
# write.csv(YIIAcerEmm, "YIIAcerEmm.csv")

Packages used

# Creates bibliography 
#knitr::write_bib(c(.packages()), "packages.bib")

Arnold, Jeffrey B. 2019. Ggthemes: Extra Themes, Scales and Geoms for ’Ggplot2’. https://CRAN.R-project.org/package=ggthemes.

Bates, Douglas, and Martin Maechler. 2019. Matrix: Sparse and Dense Matrix Classes and Methods. https://CRAN.R-project.org/package=Matrix.

Bates, Douglas, Martin Maechler, Ben Bolker, and Steven Walker. 2019. Lme4: Linear Mixed-Effects Models Using ’Eigen’ and S4. https://CRAN.R-project.org/package=lme4.

Fox, John, Sanford Weisberg, and Brad Price. 2018. CarData: Companion to Applied Regression Data Sets. https://CRAN.R-project.org/package=carData.

Fox, John, Sanford Weisberg, Brad Price, Michael Friendly, and Jangman Hong. 2019. Effects: Effect Displays for Linear, Generalized Linear, and Other Models. https://CRAN.R-project.org/package=effects.

Genz, Alan, Frank Bretz, Tetsuhisa Miwa, Xuefei Mi, and Torsten Hothorn. 2019. Mvtnorm: Multivariate Normal and T Distributions. https://CRAN.R-project.org/package=mvtnorm.

Graves, Spencer, Hans-Peter Piepho, and Luciano Selzer with help from Sundar Dorai-Raj. 2015. MultcompView: Visualizations of Paired Comparisons. https://CRAN.R-project.org/package=multcompView.

Hothorn, Torsten. 2019. TH.data: TH’s Data Archive. https://CRAN.R-project.org/package=TH.data.

Hothorn, Torsten, Frank Bretz, and Peter Westfall. 2019. Multcomp: Simultaneous Inference in General Parametric Models. https://CRAN.R-project.org/package=multcomp.

Kuznetsova, Alexandra, Per Bruun Brockhoff, and Rune Haubo Bojesen Christensen. 2019. LmerTest: Tests in Linear Mixed Effects Models. https://CRAN.R-project.org/package=lmerTest.

Lenth, Russell. 2019. Emmeans: Estimated Marginal Means, Aka Least-Squares Means. https://CRAN.R-project.org/package=emmeans.

R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Ripley, Brian. 2019. MASS: Support Functions and Datasets for Venables and Ripley’s Mass. https://CRAN.R-project.org/package=MASS.

Therneau, Terry M. 2019. Survival: Survival Analysis. https://CRAN.R-project.org/package=survival.

Wickham, Hadley. 2016. Plyr: Tools for Splitting, Applying and Combining Data. https://CRAN.R-project.org/package=plyr.

———. 2017. Reshape2: Flexibly Reshape Data: A Reboot of the Reshape Package. https://CRAN.R-project.org/package=reshape2.

Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, and Hiroaki Yutani. 2019. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.

Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2019. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.