plot_isofrequency.R 2.55 KiB
#' Plot one lake isofrequency annual curves for one objective at one station
#'
#' @param x [list] containing one [data.frame] per lake
#' @param freq [numeric] vector of frequencies to plot
#' @param result.dir [character] The path to the result database (default: "database")
#'
#' @return [NULL]
#' @export
#'
plot_isofrequency <- function(x, freq, result.dir = "database") {
  lObj <- vgest_read_all(x, result.dir)
  nLakes <- nrow(x$lakes[[1]])
  if(nLakes > 1) {
    par(mfrow=c(ceiling(nLakes/2),2))
  lapply(1:nLakes, function(i) {plot_isofrequency_lake(lObj[[i]], freq, x$lakes[[1]][i,], ceiling(i/2))})
  sLowHigh <- c("low", "high")
  mtext(
    paste("Objective", x$threshold, "m3/s", sLowHigh[x$flood + 1], "flows threshold at", x$station),
    side = 3, col = "black", line = -2, cex = 1.2, font = 2, outer = TRUE
#' Plot one lake isofrequency annual curves for one objective at one station
#' This function is mainly called by [plot_isofrequency()].
#' @param vObj dataframe produced by [vgest_read_one()] and stored in a list by [vgest_read_all()]
#' @param frequencies vector of frequencies to plot
#' @param lake lake data extract from column `lakes` of objective data given by [objectives]
#' @param top.margin top margin applied on the plot for the title
#' @return [NULL]
#' @export
plot_isofrequency_lake <- function(vObj, frequencies, lake, top.margin) {
  frequencies <- paste0("F", format(frequencies, digits = 5, nsmall = 5))
  vObj[,-1] <- (vObj[,-1]) / 1E6 + lake$min
  # Contrasted color palette adapted from https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
  sColors <- c("#003A80", "#f58231", "#3cb44b", "#e6194B", "#911eb4", "#ffe119", "#f032e6", "#9A6324", "#000075", "#808000", "#42d4f4", "#a9a9a9", "#bfef45", "#469990")
  par(mgp=c(2,0.5,0), mar = c(2.5,3.5,5.5 - 2.2*top.margin,0.5))
  plot(
    rep(lake$min, 365),
    type = "l", lwd = 1.5, col = "grey", lty = 2,
    xaxt="n",
    main = NULL,
    ylab = "Reservoir Storage (mcm)",
    xlab = NULL,
    ylim = c(0, lake$max),
    cex.lab = 0.6, cex.axis = 0.6
  lines(rep(lake$max, 365), lwd = 1.5, col = "grey", lty = 2)
  i <- 0
  for(frequency in frequencies) {
    i <- i + 1
    lines(vObj[,frequency], lwd = 2, col = sColors[i])
  mtext(
    paste(lake$name, "reservoir (Active cap.", lake$max - lake$min, "mcm)"),
    side = 3, font = 2, cex = 0.75
  xtick <- c(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  xtick <- sapply(1:13, function(x) {sum(xtick[1:x])})
  axis(side=1, at=xtick, labels = unlist(strsplit("JFMAMJJASONDJ", NULL)), cex.axis = 0.6)