dyplot.default.R 4.57 KB
Newer Older
1
2
dyplot.default <- function(x, Qsup = NULL, Qsup.name = "Qsup",
                           col.Precip = c("royalblue", "lightblue"), col.Q = c("black", "orangered", "grey"), col.na = "lightgrey",
3
                           ylab = NULL, main = NULL,
4
5
                           plot.na = TRUE, RangeSelector = TRUE, Roller = FALSE,
                           LegendShow = c("follow", "auto", "always", "onmouseover", "never"), ...) {
6
  
7
8
  # barChartPrecip <- scan(file = system.file("plugins/barChartPrecip.js", package = "airGRteaching"),
  #                        what = "character", quiet = TRUE)
9
  
10
11
  if (! any(class(x) %in% c("PrepGR", "CalGR", "SimGR"))) {
    stop("Non convenient data for x argument. Must be of class \"PrepGR\", \"CalGR\" or \"SimGR\"")
unknown's avatar
unknown committed
12
13
  }
  
14
15
16
17
18
19
20
21
22
  if (is.null(ylab)) {
    yunit <- .TypeModelGR(x)$TimeUnit
    ylab  <- paste0(c("flow [mm/", "precip. [mm/"), yunit, "]")
  } else {
    if (length(ylab) < 2) {
      ylab <- c(ylab, "")
    }
  } 
  
23
24
25
26
27
28
29
30
31
32
33
34
  if (is.null(Qsup)) {
    Qsup <- as.numeric(rep(NA, length.out = length(x$Qobs)))
  } 
  if (!is.numeric(Qsup)) {
    stop("'Qsup' must be numeric")
  }
  if (length(Qsup) != length(x$Qobs)) {
    stop("Incorrect length of 'Qsup', must be of length ", length(x$Qobs))
  }
  if (!is.character(Qsup.name)) {
    Qsup.name <- as.character(Qsup.name)
  }
35
36
  
  
37
  if (any(class(x) %in% "PrepGR")) {
unknown's avatar
unknown committed
38
39
    data <- data.frame(DatesR = x$InputsModel$DatesR,
                       Precip = x$InputsModel$Precip,
40
                       Qobs   = x$Qobs,
41
42
                       Qsim   = NA,
                       Qsup   = Qsup)
43
44
45
46
47
    if (grepl("CemaNeige", x$TypeModel)) {
      data$Psol <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip) * as.data.frame(x$InputsModel$LayerFracSolidPrecip), na.rm = TRUE)
      data$Pliq <- data$Precip - data$Psol
      data$Precip <- NULL
    } 
unknown's avatar
unknown committed
48
49
50
51
  } else {
    data <- data.frame(DatesR = x$OutputsModel$DatesR,
                       Precip = x$OutputsModel$Precip,
                       Qobs   = x$Qobs,
52
53
                       Qsim   = x$OutputsModel$Qsim,
                       Qsup   = Qsup)
54
55
56
57
    if (grepl("CemaNeige", x$TypeModel)) {
      data$Psol <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, function(x) x$Psol))
      data$Pliq <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, function(x) x$Pliq))
      data$Precip <- NULL
unknown's avatar
unknown committed
58
59
    }
  }
60
  data.xts <- xts::xts(data[, -1L], order.by = data$DatesR, tz = "UTC")
61
  
62
  
63
64
65
  rgba <- function(x, alpha = 1) {
    sprintf("rgba(%s, %f)", paste0(col2rgb(x), collapse = ", "), alpha)
  }
66
67
68
69
70
  if (length(col.Q) == 1) {
    col.Q <- c(rgba(col.Q), rgba(col.Q, alpha = 0.5), rgba(col.Q, alpha = 0.3))
  }
  if (length(col.Q) == 2) {
    col.Q <- c(rgba(col.Q[1L]), rgba(col.Q[2L]), rgba(col.Q[2L], alpha = 0.5))
71
72
73
74
  }
  if (length(col.Precip) < 2) {
    col.Precip <- c(rgba(col.Precip), rgba(col.Precip, alpha = 0.5))
  }
75
  
76
  
77
78
79
80
81
82
  if (grepl("CemaNeige", x$TypeModel)) {
    Plim <- c(-1e-3, max(data$Psol+data$Pliq, na.rm = TRUE))
  } else {
    Plim <- c(-1e-3, max(data$Precip, na.rm = TRUE))
    col.Precip <- col.Precip[1L]
  }
unknown's avatar
unknown committed
83
  
84
  
85
86
87
88
  dg <- dygraphs::dygraph(data.xts, main = main, ...)
  dg <- dygraphs::dySeries(dygraph = dg, name = "Qobs", axis = "y", color = col.Q[1L], drawPoints = TRUE)
  dg <- dygraphs::dySeries(dygraph = dg, name = "Qsim", axis = "y", color = col.Q[2L])
  dg <- dygraphs::dySeries(dygraph = dg, name = "Qsup", axis = "y", color = col.Q[3L], label = Qsup.name, strokePattern = "dashed")
89
  dg <- dygraphs::dyStackedBarGroup(dygraph = dg, name = rev(grep("^P", colnames(data.xts), value = TRUE)), axis = "y2", color = (col.Precip))
90
91
92
93
  dg <- dygraphs::dyAxis(dygraph = dg, name = "y" , label = ylab[1L],
                         valueRange = range(data.xts[, grep("^Q", colnames(data.xts))], na.rm = TRUE) * c(0.01, 1.59))
  dg <- dygraphs::dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
                         valueRange = rev(Plim) * c(2.99, 0.01))
unknown's avatar
unknown committed
94
  if (RangeSelector) {
95
    dg <- dygraphs::dyRangeSelector(dygraph = dg, height = 15)
unknown's avatar
unknown committed
96
  }
97
  if (plot.na) {
98
99
    idNA <- .StartStop(data$Qobs, FUN = is.na)
    dg <- .DyShadingMulti(dygraph = dg, color = col.na,
100
                             ts = data$DatesR, idStart = idNA$start, IdStop = idNA$stop)
101
  }
102
  if (Roller) {
103
    dg <- dygraphs::dyRoller(dygraph = dg, rollPeriod = 5)
104
105
  }
  if (is.numeric(Roller)) {
106
    dg <- dygraphs::dyRoller(dygraph = dg, rollPeriod = Roller)
107
108
  }
  if (any(LegendShow %in% c("follow", "auto", "always", "onmouseover", "never"))) {
109
    dg <- dygraphs::dyLegend(dygraph = dg, show = LegendShow[1L])
110
  }
111
  dg <- dygraphs::dyOptions(dygraph = dg, useDataTimezone = TRUE)
112
  
113
  return(dg)
unknown's avatar
unknown committed
114
115
  
}