library(tidyverse)
library(here)

Display R and package versions:

devtools::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.2.3 (2023-03-15 ucrt)
##  os       Windows 10 x64 (build 19044)
##  system   x86_64, mingw32
##  ui       RTerm
##  language (EN)
##  collate  English_United States.utf8
##  ctype    English_United States.utf8
##  tz       America/Los_Angeles
##  date     2023-07-31
##  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date (UTC) lib source
##  bslib         0.4.2   2022-12-16 [1] CRAN (R 4.2.2)
##  cachem        1.0.8   2023-05-01 [1] CRAN (R 4.2.3)
##  callr         3.7.3   2022-11-02 [1] CRAN (R 4.2.2)
##  cli           3.6.1   2023-03-23 [1] CRAN (R 4.2.3)
##  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.2.2)
##  crayon        1.5.2   2022-09-29 [1] CRAN (R 4.2.1)
##  devtools      2.4.5   2022-10-11 [1] CRAN (R 4.2.1)
##  digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.2)
##  dplyr       * 1.1.2   2023-04-20 [1] CRAN (R 4.2.3)
##  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.1)
##  evaluate      0.21    2023-05-05 [1] CRAN (R 4.2.3)
##  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.2.2)
##  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.2.2)
##  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.2.2)
##  fs            1.6.2   2023-04-25 [1] CRAN (R 4.2.3)
##  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.1)
##  ggplot2     * 3.4.2   2023-04-03 [1] CRAN (R 4.2.3)
##  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.1)
##  gtable        0.3.3   2023-03-21 [1] CRAN (R 4.2.3)
##  here        * 1.0.1   2020-12-13 [1] CRAN (R 4.2.1)
##  hms           1.1.3   2023-03-21 [1] CRAN (R 4.2.3)
##  htmltools     0.5.5   2023-03-23 [1] CRAN (R 4.2.3)
##  htmlwidgets   1.6.2   2023-03-17 [1] CRAN (R 4.2.3)
##  httpuv        1.6.9   2023-02-14 [1] CRAN (R 4.2.2)
##  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.2.1)
##  jsonlite      1.8.4   2022-12-06 [1] CRAN (R 4.2.2)
##  knitr         1.42    2023-01-25 [1] CRAN (R 4.2.2)
##  later         1.3.0   2021-08-18 [1] CRAN (R 4.2.1)
##  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.1)
##  lubridate   * 1.9.2   2023-02-10 [1] CRAN (R 4.2.2)
##  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.1)
##  memoise       2.0.1   2021-11-26 [1] CRAN (R 4.2.1)
##  mime          0.12    2021-09-28 [1] CRAN (R 4.2.0)
##  miniUI        0.1.1.1 2018-05-18 [1] CRAN (R 4.2.1)
##  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.1)
##  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.2.3)
##  pkgbuild      1.4.0   2022-11-27 [1] CRAN (R 4.2.2)
##  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.1)
##  pkgload       1.3.2   2022-11-16 [1] CRAN (R 4.2.2)
##  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.2.1)
##  processx      3.8.1   2023-04-18 [1] CRAN (R 4.2.3)
##  profvis       0.3.7   2020-11-02 [1] CRAN (R 4.2.1)
##  promises      1.2.0.1 2021-02-11 [1] CRAN (R 4.2.1)
##  ps            1.7.5   2023-04-18 [1] CRAN (R 4.2.3)
##  purrr       * 1.0.1   2023-01-10 [1] CRAN (R 4.2.2)
##  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.1)
##  Rcpp          1.0.10  2023-01-22 [1] CRAN (R 4.2.2)
##  readr       * 2.1.4   2023-02-10 [1] CRAN (R 4.2.2)
##  remotes       2.4.2   2021-11-30 [1] CRAN (R 4.2.1)
##  rlang         1.1.1   2023-04-28 [1] CRAN (R 4.2.3)
##  rmarkdown     2.21    2023-03-26 [1] CRAN (R 4.2.3)
##  rprojroot     2.0.3   2022-04-02 [1] CRAN (R 4.2.1)
##  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
##  sass          0.4.6   2023-05-03 [1] CRAN (R 4.2.3)
##  scales        1.2.1   2022-08-20 [1] CRAN (R 4.2.1)
##  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.1)
##  shiny         1.7.4   2022-12-15 [1] CRAN (R 4.2.2)
##  stringi       1.7.12  2023-01-11 [1] CRAN (R 4.2.2)
##  stringr     * 1.5.0   2022-12-02 [1] CRAN (R 4.2.2)
##  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.2.3)
##  tidyr       * 1.3.0   2023-01-24 [1] CRAN (R 4.2.2)
##  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.1)
##  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.2.2)
##  timechange    0.2.0   2023-01-11 [1] CRAN (R 4.2.2)
##  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.2.3)
##  urlchecker    1.0.1   2021-11-30 [1] CRAN (R 4.2.1)
##  usethis       2.1.6   2022-05-25 [1] CRAN (R 4.2.1)
##  utf8          1.2.3   2023-01-31 [1] CRAN (R 4.2.2)
##  vctrs         0.6.2   2023-04-19 [1] CRAN (R 4.2.3)
##  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.1)
##  xfun          0.39    2023-04-20 [1] CRAN (R 4.2.3)
##  xtable        1.8-4   2019-04-21 [1] CRAN (R 4.2.1)
##  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.2)
## 
##  [1] C:/R/win-library/4.2
##  [2] C:/Program Files/R/R-4.2.3/library
## 
## ──────────────────────────────────────────────────────────────────────────────

Data prep

# Import Delta Inflow and Outflow data from Dayflow
dayflow <- readRDS(here("data/processed/hydrology/dayflow.rds"))

# Prepare Dayflow data for plots
dayflow_c <- dayflow %>%
  mutate(
    Period_year_cat = factor(Period_year_cat, levels = c("1", "2", "3+")),
    Drought = factor(
      Drought, 
      levels = c("D", "N", "W"), 
      labels = c("Drought", "Neutral", "Wet")
    )
  )

Inflow plots

Total inflow seasonal hydrograph by year

dayflow_c %>% 
  ggplot(aes(x = WYday, y = InflowTotal, color = Period_year_cat, group = WaterYear)) +
  geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 25)) +
  facet_grid(rows = vars(Drought), scales = "free_y") +
  scale_color_viridis_d(name = "Period-year") +
  coord_cartesian(ylim = c(0, NA), expand = FALSE) +
  theme_bw()

Total inflow seasonal hydrograph by Period_year

dayflow_c %>%
  ggplot(aes(x = WYday, y = InflowTotal, color = Period_year_cat, group = Period_year_cat)) +
  geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 25)) +
  facet_grid(rows = vars(Drought), scales = "free_y") +
  scale_color_viridis_d(name = "Period-year") +
  scale_x_continuous(
    name = "Day of Water Year",
    breaks = seq.int(0, 360, by = 60),
    expand = expansion(mult = 0.005)
  ) +
  scale_y_continuous(limits = c(0, NA), expand = expansion()) +
  theme_bw()

Total inflow seasonal hydrograph by Period_year, scaled by summed inflow over the year

dayflow_c %>% 
  ggplot(aes(x = WYday, y = InflowTotal / InflowTotal_sum, color = Period_year_cat)) +
  geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 25)) +
  facet_grid(rows = vars(Drought)) +
  scale_color_viridis_d(name = "Period-year") +
  scale_x_continuous(
    name = "Day of Water Year",
    breaks = seq.int(0, 360, by = 60),
    expand = expansion(mult = 0.005)
  ) +
  scale_y_continuous(limits = c(0, NA), expand = expansion()) +
  theme_bw()

Total cumulative inflow seasonal hydrograph by Period_year

plt_inflow_wyday <- dayflow_c %>% 
  ggplot(
    aes(
      x = WYday, 
      y = InflowTotal_cum / InflowTotal_sum, 
      color = Period_year_cat, 
      group = WaterYear, 
      linetype = Period_year_cat
    )
  ) +
  geom_line() +
  facet_grid(rows = vars(Drought), scales = "free_y") +
  scale_color_viridis_d(name = "Period-year") +
  scale_linetype(name = "Period-year") +
  scale_x_continuous(
    name = "Day of Water Year",
    breaks = seq.int(0, 360, by = 60),
    expand = expansion(mult = 0.005)
  ) +
  scale_y_continuous(
    name = "Cumulative proportional inflow",
    limits = c(0, NA), 
    expand = expansion()
  ) +
  theme_bw() +
  theme(panel.spacing = unit(1, "lines"))

plt_inflow_wyday

This shows most clearly how in dry years, after 2+ years of a drought, water managers are more conservative early in the water year, saving up whatever rainfall they can. But in wet years after 2+ years of a wet period, the reservoirs are full and much more water is released earlier in the water year. Neutral years show no pattern, as expected since they represent flip-flopping among year types.

Same plot as above, but smoothed for each Period_year and drought category combo

dayflow_c %>% 
  ggplot(
    aes(
      x = WYday, 
      y = InflowTotal_cum / InflowTotal_sum, 
      color = Period_year_cat, 
      fill = Period_year_cat, 
      linetype = Period_year_cat
    )
  ) +
  geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 25), alpha = 0.5) +
  facet_grid(rows = vars(Drought), scales = "free_y") +
  scale_color_viridis_d(aesthetics = c("color", "fill"), name = "Period-year") +
  scale_linetype(name = "Period-year") +
   scale_x_continuous(
    name = "Day of Water Year",
    breaks = seq.int(0, 360, by = 60),
    expand = expansion(mult = 0.005)
  ) +
  scale_y_continuous(
    name = "Cumulative proportional inflow",
    limits = c(0, NA), 
    expand = expansion()
  ) +
  theme_bw()

Outflow plots

Total cumulative outflow seasonal hydrograph by Period_year

plt_outflow_wyday <- dayflow_c %>% 
  ggplot(
    aes(
      x = WYday, 
      y = Outflow_cum / Outflow_sum, 
      color = Period_year_cat, 
      group = WaterYear, 
      linetype = Period_year_cat
    )
  ) +
  geom_line() +
  facet_grid(rows = vars(Drought), scales = "free_y") +
  ylab("Cumulative proportional outflow") +
  scale_color_viridis_d(name = "Period-year") +
  scale_linetype(name = "Period-year") +
   scale_x_continuous(
    name = "Day of Water Year",
    breaks = seq.int(0, 360, by = 60),
    expand = expansion(mult = 0.005)
  ) +
  scale_y_continuous(
    name = "Cumulative proportional outflow",
    limits = c(0, NA), 
    expand = expansion()
  ) +
  theme_bw() +
  theme(panel.spacing = unit(1, "lines"))

plt_outflow_wyday

Export Figures

# Define file path for manuscript figures
fp_plots <- here("results/figures")

# Cumulative inflow figure
ggsave(
  file.path(fp_plots, "cumul_inflow_wyday.jpg"), 
  plot = plt_inflow_wyday, 
  width = 6, 
  height = 4, 
  units = "in",
  dpi = 300
)

# Cumulative outflow figure
ggsave(
  file.path(fp_plots, "cumul_outflow_wyday.jpg"), 
  plot = plt_outflow_wyday, 
  width = 6, 
  height = 4, 
  units = "in",
  dpi = 300
)