Stratification Periods with weather variables

Load libraries

library(tidyr)
library(ggplot2)
library(lubridate)
library(here)
library(dplyr)
library(skimr)
library(visdat)
library(janitor)
library(readr)
library(patchwork)

Stratification Periods

Load in periods and isolate spell

StratP <- read_csv(file = here::here("data", "StratPeriods.csv"))

#Remove time data, and read as date
StratP2 <- StratP %>%
  select(-Stime, -Etime) %>%
  mutate(Sdate = dmy(Sdate),
         Edate = dmy(Edate))

#Isolate spell
StratP3 <- StratP2 %>%
  filter(Spell == 3)

load Flow Data

Flow1 <- read_csv(file = here::here("data", "DeltFlow.csv"))

#Convert date type

Flow1 <- Flow1 %>%
  mutate(Date = dmy(Date))

Flow plots

# Define the start and end date for the x-axis
start_date <- as.Date("2021-01-20")
end_date <- max(Flow1$Date)  # Adjust this as needed

#Create plot

FlowPlot2 <- ggplot() +
  geom_line(data = Flow1, aes(x = Date, y = Flow), color = "steelblue", linewidth = 1.5) +
  geom_rect(data = StratP3, aes(xmin = Sdate, xmax = Edate, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = StratP3 %>% filter(!duplicated(Sdate)), aes(xintercept = Sdate), 
             linetype = "solid", color = "skyblue4") +
  geom_vline(data = StratP3 %>% filter(!duplicated(Edate)), aes(xintercept = Edate), 
             linetype = "dashed", color = "skyblue4") +
  theme_minimal() +
  scale_x_date(limits = c(start_date, end_date), 
               date_breaks = "2 days", date_labels = "%d %b, %Y",
guide = guide_axis(angle = 60)) + xlab("") +ylab("Discharge (ML / Day)")

#error regarding 1-day strat period
#Instead, enter manually

#define period
one_day_start_date <- as.Date("2021-02-03")
one_day_end_date <- as.Date("2021-02-04")

#Create new Plot (adding manually defined dates)

FlowPlot3 <- FlowPlot2 +
  geom_rect(data = data.frame(xmin = one_day_start_date, xmax = one_day_end_date), 
            aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = data.frame(xintercept = one_day_start_date), 
             aes(xintercept = xintercept), linetype = "solid", color = "skyblue4") +
  geom_vline(data = data.frame(xintercept = one_day_end_date), 
             aes(xintercept = xintercept), linetype = "dashed", color = "skyblue4")

FlowPlot3

Maximum Temperature

  #Spell 3 periods are already isolated in StratP3
 
 #Remove NAs from Flow1 (Call it Flow3 - in line with then spell #)
Flow3 <- na.omit(Flow1)

tail(Flow3)
## # A tibble: 6 × 5
##   Date       DeltMax  Flow  Tmax  Tmin
##   <date>       <dbl> <dbl> <dbl> <dbl>
## 1 2021-02-18   16.7   3966  22    34.5
## 2 2021-02-19   31.3   3695  19    37.5
## 3 2021-02-20   24.4   3483  22    37.5
## 4 2021-02-21   10.6   3301  18    32  
## 5 2021-02-22    6.61  3182  13.5  27.5
## 6 2021-02-23   17.4   3422  10.5  25
#Make Temp Plot

Tempplot1 <- ggplot() +
  geom_line(data = Flow3, aes(x = Date, y = Tmax), color = "coral3", linewidth = 1.5) +
  geom_rect(data = StratP3, aes(xmin = Sdate, xmax = Edate, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = StratP3 %>% filter(!duplicated(Sdate)), aes(xintercept = Sdate), 
             linetype = "solid", color = "skyblue4") +
  geom_vline(data = StratP3 %>% filter(!duplicated(Edate)), aes(xintercept = Edate), 
             linetype = "dashed", color = "skyblue4") +
  theme_minimal() +
  scale_x_date(limits = c(start_date, end_date), 
               date_breaks = "2 days", date_labels = "%d %b, %Y",
guide = guide_axis(angle = 60)) + xlab("") +ylab("Daily Maximum Temperature")


#Add Missing Period

Tempplot2 <- Tempplot1 +
  geom_rect(data = data.frame(xmin = one_day_start_date, xmax = one_day_end_date), 
            aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = data.frame(xintercept = one_day_start_date), 
             aes(xintercept = xintercept), linetype = "solid", color = "skyblue4") +
  geom_vline(data = data.frame(xintercept = one_day_end_date), 
             aes(xintercept = xintercept), linetype = "dashed", color = "skyblue4")

Tempplot2

#Flow_Temp_Plot <- Flowplot3 +
#Make min Temp Plot

minTempplot1 <- ggplot() +
  geom_line(data = Flow3, aes(x = Date, y = Tmin), color = "coral3", linewidth = 1.5) +
  geom_rect(data = StratP3, aes(xmin = Sdate, xmax = Edate, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = StratP3 %>% filter(!duplicated(Sdate)), aes(xintercept = Sdate), 
             linetype = "solid", color = "skyblue4") +
  geom_vline(data = StratP3 %>% filter(!duplicated(Edate)), aes(xintercept = Edate), 
             linetype = "dashed", color = "skyblue4") +
  theme_minimal() +
  scale_x_date(limits = c(start_date, end_date), 
               date_breaks = "2 days", date_labels = "%d %b, %Y",
guide = guide_axis(angle = 60)) + xlab("") +ylab("Daily Minimum Temperature")


#Add Missing Period

minTempplot2 <- minTempplot1 +
  geom_rect(data = data.frame(xmin = one_day_start_date, xmax = one_day_end_date), 
            aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = data.frame(xintercept = one_day_start_date), 
             aes(xintercept = xintercept), linetype = "solid", color = "skyblue4") +
  geom_vline(data = data.frame(xintercept = one_day_end_date), 
             aes(xintercept = xintercept), linetype = "dashed", color = "skyblue4")

minTempplot2

Combine Min and Max into one plot

MinMaxTemp3a <- ggplot() +
  geom_line(data = Flow3, aes(x = Date, y = Tmin), color = "deepskyblue3", linewidth = 1.5) +
  geom_line(data = Flow3, aes(x = Date, y = Tmax), color = "coral3", linewidth = 1.5) +
  geom_rect(data = StratP3, aes(xmin = Sdate, xmax = Edate, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = StratP3 %>% filter(!duplicated(Sdate)), aes(xintercept = Sdate), 
             linetype = "solid", color = "skyblue4") +
  geom_vline(data = StratP3 %>% filter(!duplicated(Edate)), aes(xintercept = Edate), 
             linetype = "dashed", color = "skyblue4") +
  theme_minimal() +
  scale_x_date(limits = c(start_date, end_date), 
               date_breaks = "2 days", date_labels = "%d %b, %Y",
guide = guide_axis(angle = 60)) + xlab("") +ylab("Daily Average Temperatures")


#Add Missing Period

minmaxTemp3 <- MinMaxTemp3a +
  geom_rect(data = data.frame(xmin = one_day_start_date, xmax = one_day_end_date), 
            aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = data.frame(xintercept = one_day_start_date), 
             aes(xintercept = xintercept), linetype = "solid", color = "skyblue4") +
  geom_vline(data = data.frame(xintercept = one_day_end_date), 
             aes(xintercept = xintercept), linetype = "dashed", color = "skyblue4")

minmaxTemp3

With Legend

MinMaxTemp3b <- ggplot() +
  geom_line(data = Flow3, aes(x = Date, y = Tmin, color = "Average Daily Minimum"), linewidth = 1.5) +
  geom_line(data = Flow3, aes(x = Date, y = Tmax, color = "Average Daily Maximum"), linewidth = 1.5) +
  geom_rect(data = StratP3, aes(xmin = Sdate, xmax = Edate, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = StratP3 %>% filter(!duplicated(Sdate)), aes(xintercept = Sdate), 
             linetype = "solid", color = "skyblue4") +
  geom_vline(data = StratP3 %>% filter(!duplicated(Edate)), aes(xintercept = Edate), 
             linetype = "dashed", color = "skyblue4") +
  theme_minimal() +
  scale_x_date(limits = c(start_date, end_date), 
               date_breaks = "2 days", date_labels = "%d %b, %Y",
               guide = guide_axis(angle = 60)) + 
  labs(x = "", y = "Temperature (C)")+
  scale_color_manual(values = c("Average Daily Minimum" = "deepskyblue3", "Average Daily Maximum" = "coral3")) +
  theme(legend.title=element_blank())

MinMaxTemp3b

#Add Missing Period

minmaxTemp3c <- MinMaxTemp3b +
  geom_rect(data = data.frame(xmin = one_day_start_date, xmax = one_day_end_date), 
            aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
            fill = "gray", alpha = 0.5) +
  geom_vline(data = data.frame(xintercept = one_day_start_date), 
             aes(xintercept = xintercept), linetype = "solid", color = "skyblue4") +
  geom_vline(data = data.frame(xintercept = one_day_end_date), 
             aes(xintercept = xintercept), linetype = "dashed", color = "skyblue4")

minmaxTemp3c

FlowTemp Plot

Session Info:

sessionInfo()
## R version 4.3.1 (2023-06-16 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 11 x64 (build 22621)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8   
## [3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C                      
## [5] LC_TIME=English_Australia.utf8    
## 
## time zone: Australia/Sydney
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] patchwork_1.1.3 readr_2.1.4     janitor_2.2.0   visdat_0.6.0   
##  [5] skimr_2.1.5     dplyr_1.1.3     here_1.0.1      lubridate_1.9.2
##  [9] ggplot2_3.4.2   tidyr_1.3.0    
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.7        utf8_1.2.3        generics_0.1.3    stringi_1.7.12   
##  [5] hms_1.1.3         digest_0.6.33     magrittr_2.0.3    evaluate_0.21    
##  [9] grid_4.3.1        timechange_0.2.0  fastmap_1.1.1     rprojroot_2.0.3  
## [13] jsonlite_1.8.7    purrr_1.0.1       fansi_1.0.4       scales_1.2.1     
## [17] jquerylib_0.1.4   cli_3.6.1         crayon_1.5.2      rlang_1.1.1      
## [21] bit64_4.0.5       munsell_0.5.0     base64enc_0.1-3   withr_2.5.0      
## [25] repr_1.1.6        cachem_1.0.8      yaml_2.3.7        parallel_4.3.1   
## [29] tools_4.3.1       tzdb_0.4.0        colorspace_2.1-0  vctrs_0.6.3      
## [33] R6_2.5.1          lifecycle_1.0.3   snakecase_0.11.1  stringr_1.5.0    
## [37] bit_4.0.5         vroom_1.6.3       pkgconfig_2.0.3   pillar_1.9.0     
## [41] bslib_0.5.1       gtable_0.3.3      glue_1.6.2        xfun_0.40        
## [45] tibble_3.2.1      tidyselect_1.2.0  rstudioapi_0.15.0 knitr_1.44       
## [49] farver_2.1.1      htmltools_0.5.5   labeling_0.4.2    rmarkdown_2.24   
## [53] compiler_4.3.1