1. Libraries and settings

# Libraries
    library(plyr)
    library(tidyverse)
    library(reshape2)
    library(ggthemes)
    library(lubridate)

# Default ggplot settings

    ggthe_bw<-theme(plot.background=element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          legend.box.background = element_rect(),
          panel.background =element_rect(fill = NA, color = "black")
          )+
    theme_bw()

2. Import the temperature data

Temperature<-read.csv("Day_Tem.csv", header = T)

Temperature$Period<-factor((Temperature$Period),levels=c
              ("Acer","Ofav-Ssid","Nutrients", "Ramping up",
                "Bleaching","Ramping down", "Recovery2"))
  
# Temperature data available from each treatment-replicate-day
Periods.days <- ddply (Temperature, .(Period, Treatment),summarise,
                Dmin = min (Day, na.rm = F), 
                Dmax = max (Day, na.rm = F))
Periods.days
Temperature.82<-subset(Temperature, Day=="82")
Temperature.111<-subset(Temperature, Day=="111")

Periods.82 <- ddply (Temperature.82, .(Treatment),summarise,
                Tmean = mean (Temperature, na.rm = F), 
                Tmin = min (Temperature, na.rm = F), 
                Tmax = max (Temperature, na.rm = F))
Periods.82
Periods.82 <- ddply (Temperature.82, .(Day),summarise,
                Tmean = mean (Temperature, na.rm = F), 
                Tmin = min (Temperature, na.rm = F), 
                Tmax = max (Temperature, na.rm = F))
Periods.82
Periods.82 <- ddply (Temperature.82, .(Day),summarise,
                Tmean = mean (Temperature, na.rm = F), 
                Tsd = sd (Temperature, na.rm = F))
Periods.82
Periods.110 <- ddply (Temperature.111, .(Day),summarise,
                Tmean = mean (Temperature, na.rm = F), 
                Tsd = sd (Temperature, na.rm = F))
Periods.110

3. Look at temperature conditions during each period of the experiment:

Temperature_Periods_R<- ggplot(Temperature, aes (Day, Temperature, 
                                               colour=factor(Period))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") +   ggthe_bw +
  xlab("Days in the experiment")  + guides(colour=guide_legend("Period")) +
  ylab("Temperature (C)") + facet_grid(Replicate~.) 
Temperature_Periods_R

Summary_Period <- ddply (Temperature, .(Period),summarise, 
                              Temp = mean (Temperature, na.rm = T),
                              Tsd  = sd (Temperature, na.rm = T), 
                              Tmax = max (Temperature, na.rm = T),
                              Tmin = min (Temperature, na.rm = T))
Summary_Period
Summary_Tanks <- ddply (Temperature, .(Period, Treatment, Replicate),summarise, 
                             Temp = mean (Temperature, na.rm = T),
                             Tsd  = sd (Temperature, na.rm = T), 
                             Tmax = max (Temperature, na.rm = T),
                             Tmin = min (Temperature, na.rm = T))
Summary_Tanks

4. Get the sample times and merge them with the temperature

Meassurments<-read.csv("Time_data.csv", header = T)
Meassurments$Date<-as.Date(Meassurments$Date)
Meassurments$Day<-as.numeric(Meassurments$Date)-17483
Meassurments$Date<-NULL

Temperature<-subset(Temperature, Day<114)
Temperature<-subset(Temperature, Day>-1)
data<-join(Temperature, Meassurments, by="Day")

Figure 1: Experimental conditions

Design<- ggplot(data) + theme_bw() +
  
  scale_y_continuous(limits = c(24,33.4),
                       name=(expression("Temperature"~(degree*C))),
                       breaks = seq(24, 32, 1),  
                       expand = c(0, 0)) +
  scale_x_continuous(name="Days in the experiment",
                       limits = c(-1,114),
                       breaks = seq(0, 113, 15),  
                       expand = c(0, 0))+
  
    annotate("segment", x = 2, xend = 91, y = 32, yend = 32,
              colour = "black", linetype=2)+
    annotate("segment", x = 79, xend = 79, y = 24, yend = 32,
              colour = "gray", linetype=1)+
    annotate("segment", x = 91, xend = 91, y = 24, yend = 32,
              colour = "gray", linetype=1)+

    annotate("text", x= c(46, 84, 101), y = c(32.5,25.5,24.5), 
             label=c("Nutrient treatments (Ambient, NH4, NH4+PO4)", " ", " "), size=3) +
    
    annotate("text", x= c(40, 84, 102), y = c(24.5, 24.5, 24.5), 
             label=c("Phase 1: Control temperature", 
                     "Phase2: \n Ramp-up",
                     "Phase 3:\n Heat stress"), size=3) +
   
    geom_smooth(aes (Day, Temperature),
                method="loess",
                span=0.1, colour="black")+
    
# Samples  
    annotate("point",x=c(75, 100, 111), y=c(32), 
             shape=21, size=2, fill="white") +
    annotate("point",x=c(82, 111), y=c(32, 32.5), 
             shape=21, size=2, fill="white")+
    annotate("point",x=c(82, 111), y=c(32, 32.5), 
             shape=4, size=2)+
# Labels
    annotate("point",x=c(3, 3), y=c(29, 30), 
               shape=21, size=2, fill="white")+
    annotate("point",x=c(3), y=c(29), 
               shape=4, size=2)+
    annotate("text", x= c(28,25), y = c(29,30), 
                 label=c( "Sacrificial samples - censored events",
                         "Microbiome samples"), size=3)+

    geom_point(aes (Day, Y, colour=Data, shape=Data))+
    scale_shape_manual(values=c(21,2,24))+
    scale_colour_manual(values = c("gray35","gray70", "gray70"))+
    
    theme(legend.position=c(0.2, 0.4),
          plot.background=element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          legend.box.background = element_rect(),
          panel.background =element_rect(fill = NA, color = "black")
          ) 
Design

#ggsave(file="Outputs/Figure_1_Experiment_design.svg", plot=Design, dpi = 300, width=6, height=4)