General project set-up

# Libraries
  library(reshape2)
  library(tidyverse)
  library(tidyr)
  library(lubridate)
  library(gridExtra)

# Graphs

theme_set (theme_classic() + theme(panel.grid.major = element_blank(),
                              panel.grid.minor = element_blank(), 
                              axis.line = element_line(colour = "black"),
                              legend.position="bottom",
                              legend.title=element_blank(),
                              #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()
                              ))

col_4_col<-scale_colour_manual(values = c("#88CCEE",  "#332288",
                                          "#DDCC77","#117733"))
col_4_fill<-scale_fill_manual(values = c("#88CCEE",  "#332288",
                                          "#DDCC77","#117733"))

Temperature data

Temperature.data<-read.csv("Data/Temp_experiment.csv", header = T)
summary(Temperature.data)
##                   Time             Tank        Temperature   
##  2020-09-14 19:30:00:     8   Tank.1 :16856   Min.   :18.06  
##  2020-09-14 19:35:00:     8   Tank.2 :16856   1st Qu.:27.99  
##  2020-09-14 19:40:00:     8   Tank.3 :16856   Median :28.02  
##  2020-09-14 19:45:00:     8   Tank.4 :16856   Mean   :27.90  
##  2020-09-14 19:50:00:     8   Tank.5 :16856   3rd Qu.:28.05  
##  2020-09-14 19:55:00:     8   Tank.6 :16856   Max.   :33.78  
##  (Other)            :134800   (Other):33712                  
##         Period          Content     
##  Experiment:134848   A-T1   :13118  
##                      A-T2   :13118  
##                      A-T5   :13118  
##                      A-T6   :13118  
##                      N-T3   :13118  
##                      N-T4   :13118  
##                      (Other):56140
# Data types
    Temperature.data$Temperature<-as.numeric(Temperature.data$Temperature)
    Temperature.data$Tank<-as.factor(Temperature.data$Tank)
    Temperature.data$Content<-factor(Temperature.data$Content)
    Temperature.data$Time<-strptime(Temperature.data$Time, "%Y-%m-%d %H:%M:%S")
    Temperature.data$Time <- as.POSIXct(Temperature.data$Time)
    Temperature.data$Date<-as.Date(ymd_hms(Temperature.data$Time))
    
    Temperature.data<- Temperature.data %>%
       group_by(Tank, Content, Date)%>%
            dplyr::summarise(Temperature=mean(Temperature))
    Temperature.data
    Temperature.data$Day<-weekdays(Temperature.data$Date)
    Temperature.data$Data<-ifelse (Temperature.data$Day=="Friday",
                         15, NA)
    Temperature.data$Day<-as.numeric(Temperature.data$Date)-18518
    Temperature.data<-Temperature.data[Temperature.data$Day>0, ]
    
    Temperature.data$Week<-(Temperature.data$Day)/7
    Temperature.data$Week<-as.integer(Temperature.data$Week)+1
    summary(Temperature.data)
##       Tank        Content         Date             Temperature         Day    
##  Tank.1 : 61   A-T1   : 47   Min.   :2020-09-14   Min.   :20.05   Min.   : 1  
##  Tank.2 : 61   A-T2   : 47   1st Qu.:2020-09-29   1st Qu.:27.97   1st Qu.:16  
##  Tank.3 : 61   A-T5   : 47   Median :2020-10-14   Median :28.01   Median :31  
##  Tank.4 : 61   A-T6   : 47   Mean   :2020-10-14   Mean   :27.89   Mean   :31  
##  Tank.5 : 61   N-T3   : 47   3rd Qu.:2020-10-29   3rd Qu.:28.03   3rd Qu.:46  
##  Tank.6 : 61   N-T4   : 47   Max.   :2020-11-13   Max.   :29.44   Max.   :61  
##  (Other):122   (Other):206                                                    
##       Data          Week  
##  Min.   :15    Min.   :1  
##  1st Qu.:15    1st Qu.:3  
##  Median :15    Median :5  
##  Mean   :15    Mean   :5  
##  3rd Qu.:15    3rd Qu.:7  
##  Max.   :15    Max.   :9  
##  NA's   :416
ExperimentTemperature<- ggplot(Temperature.data) +
  #geom_vline(xintercept=1500)+
  
  geom_jitter(aes(Date, Temperature, colour=factor(Content)),
              alpha=0.5, size=0.01)+
  geom_line(aes(Date, Data), alpha=1, size=0.05)+
  scale_y_continuous(limits = c(15, 35),
                         breaks = seq(15, 35, 2),  
                         expand = c(0, 0),
                         name=("Temperature (C)"))

ExperimentTemperature+facet_wrap(~Tank)

Salinity data

## Salinity
    Salinity.data<-read.csv("Data/System.csv")
    Salinity.data<-Salinity.data %>% select(Time, Salinity)
    Salinity.data$Time<-strptime(Salinity.data$Time, "%m/%d/%y %H:%M")
    Salinity.data$Time <- as.POSIXct(Salinity.data$Time)
    Salinity.data$Date <- as.Date(Salinity.data$Time)
    
    Salinity.data<- Salinity.data %>%
       group_by(Date)%>%
            dplyr::summarise(Salinity=mean(Salinity))
    Salinity.data
    Temperature.data<-left_join(Temperature.data, Salinity.data, by=("Date"))

Tank treatment info

# Get tank treatment info  

  Tank.info<-read.csv("Data/Tank_treatment.csv", header = T)

# Merge with temperature
  Temperature.data<-left_join(Temperature.data, Tank.info, by=("Tank"))
  
  Temperature.data$Treatment<-factor(Temperature.data$Combo, levels = c("Ambient+Placebo", "Ambient+Disease", 
                                                              "NH4+Placebo", "NH4+Disease"))

Temp + Sal plot

meanTemp<-ggplot(Temperature.data, aes(Week, Temperature, fill=Treatment)) +
  geom_line(data=Temperature.data, aes(x=Temperature.data$Day/7, y=Temperature.data$Salinity), linetype=2) + 
  
  stat_summary(fun=mean, geom="line", position=position_dodge(width=0.3)) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2, position=position_dodge(width=0.3)) + 
  stat_summary(fun=mean, geom="point", shape=21, size=3, position=position_dodge(width=0.3)) +
  
  scale_x_continuous(limits = c(0.8, 9.2),
                      breaks = seq(1, 9, 1),  
                      expand = c(0, 0))+
  scale_y_continuous(limits = c(25, 33),
                     # breaks = seq(0, 35, 2),  
                     # expand = c(0, 0),
                    name=expression("Temperature ("~degree~"C)"))+
  geom_vline(xintercept=7.8, linetype="dashed", colour="#990033") +
  col_4_fill+
  col_4_col
meanTemp

#ggsave(file="Outputs/Temperature_plot.svg", meanTemp, width=5.5, height=4)

Temp and sal summary

Temp.Summary<- as.data.frame(Temperature.data %>%
  group_by(Week, Treatment) %>%
  summarize(Temp_mean = mean (Temperature,  na.rm = T),
            Temp_sd = sd (Temperature,  na.rm = T)))
Temp.Summary<-reshape(Temp.Summary, idvar = "Treatment", timevar = "Week", direction = "wide")
Temp.Summary
write.csv(Temp.Summary, "Outputs/Temp_Summary.csv", row.names = F)


Sal.Summary<- as.data.frame(Temperature.data %>%
  group_by(Week, Treatment) %>%
  summarize(Sal_mean = min (Salinity,  na.rm = T),
            Sal_sd = sd (Salinity,  na.rm = T)))
Sal.Summary<-reshape(Sal.Summary, idvar = "Treatment", timevar = "Week", direction = "wide")
Sal.Summary
write.csv(Sal.Summary, "Outputs/Sal_Summary.csv", row.names = F)

Nutrients

# Nutrients
Nutrients<-read.csv("Data/Nutrients.csv")
summary(Nutrients)
##    Sample.ID        Tank         Sample              Treatment      sample   
##  29-01  :  2   3      :107   31     : 17   Ambient+Disease:176   Chl   :408  
##  29-02  :  2   4      :107   43     : 17   Ambient+Placebo:176   frozen:261  
##  29-03  :  2   7      :107   29     : 16   NH4+Disease    :214   Frozen:139  
##  29-04  :  2   8      :107   35     : 16   NH4+Placebo    :214               
##  29-05  :  2   1      : 88   38     : 16   NA's           : 28               
##  29-06  :  2   2      : 88   39     : 16                                     
##  (Other):796   (Other):204   (Other):710                                     
##       NH4             Si.uM.          NO2.uM.          N.N.uM.      
##  Min.   : 0.000   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 2.270   1st Qu.: 0.000   1st Qu.: 0.400   1st Qu.: 3.547  
##  Median : 5.842   Median : 1.550   Median : 1.355   Median : 5.880  
##  Mean   : 6.984   Mean   : 2.417   Mean   : 1.569   Mean   : 5.665  
##  3rd Qu.: 9.960   3rd Qu.: 4.067   3rd Qu.: 2.337   3rd Qu.: 7.740  
##  Max.   :31.559   Max.   :22.200   Max.   :10.290   Max.   :24.514  
##  NA's   :3        NA's   :298      NA's   :306      NA's   :306     
##     NO3.uM.          PO4.uM.             Day             Week      
##  Min.   : 0.000   Min.   :0.00000   Min.   : 1.00   Min.   :1.000  
##  1st Qu.: 2.825   1st Qu.:0.08425   1st Qu.:41.00   1st Qu.:6.000  
##  Median : 4.340   Median :0.25400   Median :51.00   Median :7.000  
##  Mean   : 4.096   Mean   :0.69458   Mean   :47.45   Mean   :6.939  
##  3rd Qu.: 5.375   3rd Qu.:0.58000   3rd Qu.:56.00   3rd Qu.:8.000  
##  Max.   :19.948   Max.   :8.92000   Max.   :64.00   Max.   :9.000  
##  NA's   :306      NA's   :306                                      
##        X              X.1       
##  Min.   :14.00   Min.   :3.000  
##  1st Qu.:27.00   1st Qu.:4.000  
##  Median :36.00   Median :6.000  
##  Mean   :36.56   Mean   :5.812  
##  3rd Qu.:46.00   3rd Qu.:7.000  
##  Max.   :58.00   Max.   :9.000  
##  NA's   :84      NA's   :84
  # Nutrients$Date<-as.Date(Nutrients$Date, "%m-%d-%Y")
  # Nutrients$Time<-paste(Nutrients$Date, Nutrients$Hour, sep = " ")
  # Nutrients$Time<-strptime(Nutrients$Time, "%Y-%m-%d %H:%M:%S")
  # Nutrients$Time <- as.POSIXct(Nutrients$Time)
  # Nutrients$Date <- as.Date(Nutrients$Time)
  # Nutrients$Day<-as.numeric(Nutrients$Date)-18518
  # Nutrients$Week<-(Nutrients$Day)/7
  # Nutrients$Week<-as.integer(Nutrients$Week)+1
  Nutrients$Treatment<-factor(Nutrients$Treatment, levels = c("Ambient+Placebo", "Ambient+Disease",
                                                               "NH4+Placebo", "NH4+Disease"))
  summary(Nutrients)
##    Sample.ID        Tank         Sample              Treatment      sample   
##  29-01  :  2   3      :107   31     : 17   Ambient+Placebo:176   Chl   :408  
##  29-02  :  2   4      :107   43     : 17   Ambient+Disease:176   frozen:261  
##  29-03  :  2   7      :107   29     : 16   NH4+Placebo    :214   Frozen:139  
##  29-04  :  2   8      :107   35     : 16   NH4+Disease    :214               
##  29-05  :  2   1      : 88   38     : 16   NA's           : 28               
##  29-06  :  2   2      : 88   39     : 16                                     
##  (Other):796   (Other):204   (Other):710                                     
##       NH4             Si.uM.          NO2.uM.          N.N.uM.      
##  Min.   : 0.000   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 2.270   1st Qu.: 0.000   1st Qu.: 0.400   1st Qu.: 3.547  
##  Median : 5.842   Median : 1.550   Median : 1.355   Median : 5.880  
##  Mean   : 6.984   Mean   : 2.417   Mean   : 1.569   Mean   : 5.665  
##  3rd Qu.: 9.960   3rd Qu.: 4.067   3rd Qu.: 2.337   3rd Qu.: 7.740  
##  Max.   :31.559   Max.   :22.200   Max.   :10.290   Max.   :24.514  
##  NA's   :3        NA's   :298      NA's   :306      NA's   :306     
##     NO3.uM.          PO4.uM.             Day             Week      
##  Min.   : 0.000   Min.   :0.00000   Min.   : 1.00   Min.   :1.000  
##  1st Qu.: 2.825   1st Qu.:0.08425   1st Qu.:41.00   1st Qu.:6.000  
##  Median : 4.340   Median :0.25400   Median :51.00   Median :7.000  
##  Mean   : 4.096   Mean   :0.69458   Mean   :47.45   Mean   :6.939  
##  3rd Qu.: 5.375   3rd Qu.:0.58000   3rd Qu.:56.00   3rd Qu.:8.000  
##  Max.   :19.948   Max.   :8.92000   Max.   :64.00   Max.   :9.000  
##  NA's   :306      NA's   :306                                      
##        X              X.1       
##  Min.   :14.00   Min.   :3.000  
##  1st Qu.:27.00   1st Qu.:4.000  
##  Median :36.00   Median :6.000  
##  Mean   :36.56   Mean   :5.812  
##  3rd Qu.:46.00   3rd Qu.:7.000  
##  Max.   :58.00   Max.   :9.000  
##  NA's   :84      NA's   :84
  #write.csv(Nutrients, "Nutrients.csv")
  

# Tall format
  # Nutrients.tall <- gather(Nutrients, Nutrient, Concentration, 
  #                     NH4:PO4.uM., factor_key=TRUE)
  # Nutrients.tall$Date<-as.Date(Nutrients.tall$Date, "%m-%d-%Y")
  # #Nutrients.tall$Time<-paste(Nutrients.tall$Date, Nutrients$Hour, sep = " ")
  # summary(Nutrients.tall)

NH4

NH4Plot<- ggplot(filter(Nutrients, Treatment!="Source"),
                       aes (Week, NH4, fill=Treatment)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun=mean, geom="line") + 
  stat_summary(fun=mean, geom="point", shape=21, size=3) +
  
  scale_x_continuous(limits = c(0.8, 9.2),
                      breaks = seq(1, 9, 1),  
                      expand = c(0, 0))+
  scale_y_continuous(#limits = c(0, 20),
                     # breaks = seq(0, 35, 2),  
                     # expand = c(0, 0),
                          name=expression(NH[4]~"["~mu~"M]"))+
  #geom_vline(xintercept=5.5, linetype="dashed")+
  geom_vline(xintercept=7.8, linetype="dashed", colour="#990033") +
  col_4_fill
  
NH4Plot

#ggsave(file="Outputs/NH4_plot.svg", NH4Plot, width=5.5, height=4.0)

PO4

PH4_plot<- ggplot(filter(Nutrients, Treatment!="Source"),
                       aes (Week, PO4.uM., fill=Treatment)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun=mean, geom="line") + 
  stat_summary(fun=mean, geom="point", shape=21, size=3) +
  scale_x_continuous(limits = c(0.8, 9.2),
                      breaks = seq(1, 9, 1),  
                      expand = c(0, 0))+
  scale_y_continuous(name=expression(PO[4]~"["~mu~"M]"))+
  geom_vline(xintercept=7.8, linetype="dashed", colour="#990033") +
  col_4_fill
PH4_plot

#ggsave(file="Outputs/PO4_plot.svg", PH4_plot, width=5.5, height=4.0)

Nutrients summary

NH4.Summary<- as.data.frame(Nutrients %>%
  group_by(Week, Treatment) %>%
  summarize(Temp_mean = mean (NH4,  na.rm = T),
            Temp_sd = sd (NH4,  na.rm = T)))
NH4.Summary<-reshape(NH4.Summary, idvar = "Treatment", timevar = "Week", direction = "wide")
NH4.Summary
#write.csv(NH4.Summary, "Outputs/NH4_Summary.csv", row.names = F)

PO4.Summary<- as.data.frame(Nutrients %>%
  group_by(Week, Treatment) %>%
  summarize(Sal_mean = min (PO4.uM.,  na.rm = T),
            Sal_sd = sd (PO4.uM.,  na.rm = T)))
PO4.Summary<-reshape(PO4.Summary, idvar = "Treatment", timevar = "Week", direction = "wide")
PO4.Summary
#write.csv(PO4.Summary, "Outputs/PO4_Summary.csv", row.names = F)

Tank conditions plot

TanksPlot<-grid.arrange(meanTemp + theme(legend.position = "none",
                              axis.text.x=element_blank(),
                              axis.title.x = element_blank()),
             NH4Plot + theme(legend.position = "none",
                             axis.text.x=element_blank(), 
                             axis.title.x = element_blank()),
             PH4_plot, nrow=3)

#ggsave(file="Outputs/Tanks.svg", TanksPlot, width=7.5, height=7.5)

Packages used

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

Auguie, Baptiste. 2017. GridExtra: Miscellaneous Functions for "Grid" Graphics. https://CRAN.R-project.org/package=gridExtra.

Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.

Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.

Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.

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

Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2021. Lubridate: Make Dealing with Dates a Little Easier. https://CRAN.R-project.org/package=lubridate.

Wickham, Hadley. 2007. “Reshaping Data with the reshape Package.” Journal of Statistical Software 21 (12): 1–20. http://www.jstatsoft.org/v21/i12/.

———. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.

———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.

———. 2020. Reshape2: Flexibly Reshape Data: A Reboot of the Reshape Package. https://github.com/hadley/reshape.

———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.

———. 2021b. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.

———. 2021c. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.

Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.

Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. 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. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.

Wickham, Hadley, and Jim Hester. 2020. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.