plot_monthly_mean.R 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#' Plot monthly mean at one gauging station
#'
#' Used for simulated flows and climatic inputs.
#'
#' @details
#' Select time steps corresponding to one of the DRIAS 2020 periods:
#' "ref" for reference (1976-2005), "near" for near future (2021-2050),
#' "middle" for middle of the century (2041-2070), "end" for end of the century (2071-2100).
#'
#' @param rcp [character], "rcp4.5" or "rcp8.5"
#' @param period [character], see details
#' @param station [character], id of gauging station
#' @param drias [list], monthly data of each GCM/RCM projection and each period
#' @param hist [list], monthly data coming from observations
#' @param y_label [character], argument passed to [ggplot2::ylab]
#'
#' @return A ggplot object.
#' @import ggplot2
#' @export
#'
plot_monthly_mean <- function(rcp, period, station, drias, hist, y_label = "Flow (m³/s)") {
    scenarios <- names(drias)
    names(scenarios) <- scenarios
    drias <- lapply(scenarios, function(x) {
        if (grepl(rcp, x, fixed = TRUE)) {

            drias[[x]][[period]][station, ]
        } else {
            NULL
        }
    })
    drias <- drias[!sapply(drias,is.null)]
    df <- as.data.frame(do.call(cbind, drias))
    df$Mois <- seq.int(12)
    dfHist <- as.data.frame(do.call(cbind, lapply(hist, function(x) x[station, ])))
    dfHist$Mois <- seq.int(12)
    ggplot(tidyr::gather(df, key = "scenario", value = "Q", -Mois)) +
        geom_line(aes(x = Mois, y = Q, color = scenario), size = 1) +
        geom_line(data = tidyr::gather(dfHist, key = "historique", value = "Q", -Mois),
                  aes(x = Mois, y = Q, linetype = historique),
                  size = 1) +
        scale_x_continuous(breaks = seq.int(12),
                           labels =  strsplit("JFMAMJJASOND", "")[[1]]) +
        ylab(y_label) + xlab("")
}