dyplot.default.R 4 KB
Newer Older
1
dyplot.default <- function(x, col.Precip = c("royalblue", "lightblue"), col.Q = c("black", "orangered"), col.na = "lightgrey",
2
                           xlab = NULL, ylab = NULL, main = NULL,
3
4
                           plot.na = TRUE, RangeSelector = TRUE, Roller = FALSE,
                           LegendShow = c("follow", "auto", "always", "onmouseover", "never"), ...) {
5
  
6
  barChartPrecip <- scan(file = system.file("plugins/barChartPrecip.js", package = "airGRteaching"),
7
8
                         what = "character", quiet = TRUE)
  
unknown's avatar
unknown committed
9
  if (! any(class(x) %in% c("ObsGR", "CalGR", "SimGR"))) {
10
    stop("Non convenient data for x argument. Must be of class \"ObsGR\", \"CalGR\" or \"SimGR\"")
unknown's avatar
unknown committed
11
12
  }
  
13
14
15
16
17
18
19
20
21
22
23
24
  if (is.null(xlab)) {
    xlab <- "Time"
  }
  if (is.null(ylab)) {
    yunit <- .TypeModelGR(x)$TimeUnit
    ylab  <- paste0(c("flow [mm/", "precip. [mm/"), yunit, "]")
  } else {
    if (length(ylab) < 2) {
      ylab <- c(ylab, "")
    }
  } 
  
25
26
  
  if (any(class(x) %in% "ObsGR")) {
unknown's avatar
unknown committed
27
28
    data <- data.frame(DatesR = x$InputsModel$DatesR,
                       Precip = x$InputsModel$Precip,
29
30
                       Qobs   = x$Qobs,
                       Qsim   = NA)
31
32
33
34
35
    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
36
37
38
39
40
  } else {
    data <- data.frame(DatesR = x$OutputsModel$DatesR,
                       Precip = x$OutputsModel$Precip,
                       Qobs   = x$Qobs,
                       Qsim   = x$OutputsModel$Qsim)
41
42
43
44
    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
45
46
    }
  }
47
48
  data.xts <- xts(data[, -1L], order.by = data$DatesR)

49
  
50
51
52
53
54
55
56
57
58
  rgba <- function(x, alpha = 1) {
    sprintf("rgba(%s, %f)", paste0(col2rgb(x), collapse = ", "), alpha)
  }
  if (length(col.Q) < 2) {
    col.Q <- c(rgba(col.Q), rgba(col.Q, alpha = 0.5))
  }
  if (length(col.Precip) < 2) {
    col.Precip <- c(rgba(col.Precip), rgba(col.Precip, alpha = 0.5))
  }
59
  
60
  
61
62
63
64
65
66
  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
67
  
68
  
69
  dg <- dygraph(data.xts, main = main)
70
71
  dg <- dySeries(dygraph = dg, name = "Qobs", axis = 'y' , color = col.Q[1L], drawPoints = TRUE)
  dg <- dySeries(dygraph = dg, name = "Qsim", axis = 'y' , color = col.Q[2L])
72
73
  dg <- dyStackedBarGroup(dygraph = dg, name = grep("^P", colnames(data.xts), value = TRUE), axis = "y2", color = rev(col.Precip))
  dg <- dyAxis(dygraph = dg, name = "y" , label = ylab[1L],
74
               valueRange = range(data.xts[, grep("^Q", colnames(data.xts))], na.rm = TRUE) * c(0.01, 1.59))
75
  dg <- dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
76
               valueRange = rev(Plim) * c(2.99, 0.01))
unknown's avatar
unknown committed
77
  if (RangeSelector) {
78
    dg <- dyRangeSelector(dygraph = dg, height = 15)
unknown's avatar
unknown committed
79
  }
80
81
82
83
84
85
  if (plot.na) {
    naQ_rle <- rle(is.na(data$Qobs))
    naQ_ide <- cumsum(naQ_rle$lengths)[naQ_rle$values] +1
    naQ_ids <- naQ_ide - naQ_rle$lengths[naQ_rle$values]  -1
    IDna   <- data.frame(start = naQ_ids, end = naQ_ide)
    for (i in seq_len(nrow(IDna))) {
86
      dg <- dyShading(dygraph = dg,
87
88
89
                      from    = as.character(data$DatesR)[IDna[i, "start"]],
                      to      = as.character(data$DatesR)[IDna[i, "end"  ]],
                      color   = col.na)
90
91
    }
  }
92
  if (Roller) {
93
    dg <- dyRoller(dygraph = dg, rollPeriod = 5)
94
95
  }
  if (is.numeric(Roller)) {
96
    dg <- dyRoller(dygraph = dg, rollPeriod = Roller)
97
98
  }
  if (any(LegendShow %in% c("follow", "auto", "always", "onmouseover", "never"))) {
99
    dg <- dyLegend(dygraph = dg, show = LegendShow[1L])
100
  }
101
  dg <- dyOptions(dygraph = dg, useDataTimezone = TRUE)
102
  
103
  return(dg)
unknown's avatar
unknown committed
104
105
  
}