dyplot.default.R 3.38 KB
Newer Older
1
dyplot.default <- function(x, col.Precip = "royalblue", 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
25
  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, "")
    }
  } 
  
    if (any(class(x) %in% "ObsGR")) {
unknown's avatar
unknown committed
26
27
28
29
30
31
32
33
34
35
36
37
    data <- data.frame(DatesR = x$InputsModel$DatesR,
                       Precip = x$InputsModel$Precip,
                       Qobs   = x$Qobs)
  } else {
    data <- data.frame(DatesR = x$OutputsModel$DatesR,
                       Precip = x$OutputsModel$Precip,
                       Qobs   = x$Qobs,
                       Qsim   = x$OutputsModel$Qsim)
    if (length(col.Q) < 2) {
      col.Q <- rep(col.Q, 2)
    }
  }
38
39

  
unknown's avatar
unknown committed
40
41
  data.xts <- xts(data[, -1L], order.by = data$DatesR)
  
42
43
  dg <- dygraph(data.xts, main = main)
  dg <- dyAxis(dygraph = dg, name = "y", label = ylab[1L],
unknown's avatar
unknown committed
44
                     valueRange = range(data.xts[, grep("^Q", colnames(data.xts))], na.rm = TRUE) * c(0.01, 1.59))
45
  dg <- dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
46
                     valueRange = c(max(data.xts[, "Precip"], na.rm = TRUE), -1e-3) * c(2.99, 0.01))
47
48
49
50
  dg <- dySeries(dygraph = dg, name = "Qobs", drawPoints = TRUE)
  dg <- dySeries(dygraph = dg, name = tail(grep("^Q", colnames(data.xts), value = TRUE), 1),
                 axis = 'y' , color = col.Q[seq_along(grep("^Q", colnames(data.xts)))])
  dg <- dySeries(dygraph = dg, name = "Precip", axis = 'y2', plotter = barChartPrecip, color = col.Precip)
unknown's avatar
unknown committed
51
  # if (ModelPeriod) {
52
53
  #   dg <- dyShading(dygraph = dg, from = x$PeriodModel$WarmUp[1L], to = x$PeriodModel$WarmUp[2L], color = "orangered")
  #   dg <- dyShading(dygraph = dg, from = x$PeriodModel$Run[1L]   , to = x$PeriodModel$Run[2L]   , color = "blue")
unknown's avatar
unknown committed
54
55
  # }
  if (RangeSelector) {
56
    dg <- dyRangeSelector(dygraph = dg, height = 15)
unknown's avatar
unknown committed
57
  }
58
59
60
61
62
63
  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))) {
64
      dg <- dyShading(dygraph = dg,
65
66
67
68
69
                            from = as.character(data$DatesR)[IDna[i, "start"]],
                            to   = as.character(data$DatesR)[IDna[i, "end"  ]],
                            color = col.na)
    }
  }
70
  if (Roller) {
71
    dg <- dyRoller(dygraph = dg, rollPeriod = 5)
72
73
  }
  if (is.numeric(Roller)) {
74
    dg <- dyRoller(dygraph = dg, rollPeriod = Roller)
75
76
  }
  if (any(LegendShow %in% c("follow", "auto", "always", "onmouseover", "never"))) {
77
    dg <- dyLegend(dygraph = dg, show = LegendShow[1L])
78
79
  }

80
81
  dg <- dyOptions(dygraph = dg, useDataTimezone = TRUE)
  return(dg)
unknown's avatar
unknown committed
82
83
  
}