dyplot.default.R 4.29 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
  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)
unknown's avatar
unknown committed
31
32
33
34
35
36
37
38
39
  } 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)
    }
  }
40
41
42
43
44
45
46
47
  
  Plim <- c(-1e-3, max(data$Precip, na.rm = TRUE))
  
  # if (any(names(x$InputsModel) == "LayerPrecip")) {
  #   data$Psol <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip) * as.data.frame(x$InputsModel$LayerFracSolidPrecip))
  #   data$Pliq <- data$Precip - data$Psol
  #   data$Precip <- NULL
  # }
48
  
unknown's avatar
unknown committed
49
50
  data.xts <- xts(data[, -1L], order.by = data$DatesR)
  
51
  dg <- dygraph(data.xts, main = main)
52
53
54
55
56
  
  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])
  dg <- dySeries(dygraph = dg, name = "Precip", axis = 'y2', plotter = barChartPrecip, color = col.Precip)
  
57
  dg <- dyAxis(dygraph = dg, name = "y", label = ylab[1L],
58
               valueRange = range(data.xts[, grep("^Q", colnames(data.xts))], na.rm = TRUE) * c(0.01, 1.59))
59
  dg <- dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
               valueRange = rev(Plim) * c(2.99, 0.01))  
  
  # if (any(names(x$InputsModel) == "LayerPrecip")) {
  #   dg <- dyStackedBarGroup(dygraph = dg, name = c("Psol", "Pliq"), axis = "y2", color = c("lightblue", col.Precip))
  #   dg <- dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
  #                valueRange = rev(Plim))# * c(0.01, 2.99))
  # } else {
    # dg <- dySeries(dygraph = dg, name = "Precip", axis = 'y2', plotter = barChartPrecip, color = col.Precip)
    # dg <- dyStackedBarGroup(dygraph = dg, name = c("Precip"), axis = "y2", color = col.Precip)
    # dg <- dyAxis(dygraph = dg, name = "y2", label = ylab[2L], independentTicks = FALSE,
    #              valueRange = rev(Plim) * c(0.01, 2.99))
  # }
  


unknown's avatar
unknown committed
75
  # if (ModelPeriod) {
76
77
  #   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
78
79
  # }
  if (RangeSelector) {
80
    dg <- dyRangeSelector(dygraph = dg, height = 15)
unknown's avatar
unknown committed
81
  }
82
83
84
85
86
87
  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))) {
88
      dg <- dyShading(dygraph = dg,
89
90
91
92
93
                            from = as.character(data$DatesR)[IDna[i, "start"]],
                            to   = as.character(data$DatesR)[IDna[i, "end"  ]],
                            color = col.na)
    }
  }
94
  if (Roller) {
95
    dg <- dyRoller(dygraph = dg, rollPeriod = 5)
96
97
  }
  if (is.numeric(Roller)) {
98
    dg <- dyRoller(dygraph = dg, rollPeriod = Roller)
99
100
  }
  if (any(LegendShow %in% c("follow", "auto", "always", "onmouseover", "never"))) {
101
    dg <- dyLegend(dygraph = dg, show = LegendShow[1L])
102
103
  }

104
105
  dg <- dyOptions(dygraph = dg, useDataTimezone = TRUE)
  return(dg)
unknown's avatar
unknown committed
106
107
  
}