plot.PrepGR.R 1.64 KB
Newer Older
1
plot.PrepGR <- function(x, type = "l", col.Precip = "royalblue", col.Q = "black", col.na = "grey",
2
                       xlab = NULL, ylab =  NULL, main = NULL, plot.na = TRUE, ...) {
3

4
  if (!inherits(x, "PrepGR")) {
5
    stop("Non convenient data for x argument. Must be of class \"PrepGR\"")
unknown's avatar
unknown committed
6
  }
7

8
9
10
11
12
13
14
15
16
17
  if (is.null(xlab)) {
    xlab <- "Time"
  }
  if (is.null(ylab)) {
    yunit <- .TypeModelGR(x)$TimeUnit
    ylab  <- paste0(c("precip. [mm/", "flow [mm/"), yunit, "]")
  } else {
    if (length(ylab) < 2) {
      ylab <- c(ylab, "")
    }
18
19
  }

unknown's avatar
unknown committed
20
21
22
  data <- data.frame(DatesR = x$InputsModel$DatesR,
                     Precip = x$InputsModel$Precip,
                     Qobs   = x$Qobs)
23

24
25
  opar <- par(no.readonly = TRUE)
  on.exit(par(opar))
26

unknown's avatar
unknown committed
27
  layout(mat = matrix(1:2), widths = c(1, 2), heights = c(1, 2))
28

unknown's avatar
unknown committed
29
30
  par(mar = c(0.1, 4, 4, 2), xaxt = "n")
  plot(Precip ~ DatesR, data = data, type = "h", col = col.Precip,
31
       xlab = "", ylab = ylab[1L], main = main, ylim = rev(range(data$Precip)))
32

unknown's avatar
unknown committed
33
  par(mar = c(5, 4, 0.1, 2), xaxt = "s")
34
35
36
37
  if (all(is.na(data$Qobs))) {
    par(yaxt = "n")
    plot(x = range(data$DatesR), y = c(0, 0), type = "n",
         xlab = xlab, ylab = ylab[2L], main = "")
38
    text(x = median(data$DatesR), y = 0, labels = "No observed\ndischarges")
39
40
41
42
  } else {
    plot(Qobs ~ DatesR, data = data, type = type, col = col.Q,
         xlab = xlab, ylab = ylab[2L], main = "")
  }
43

unknown's avatar
unknown committed
44
45
  if (plot.na) {
    axis(side = 1, at = as.POSIXct(data$DatesR[is.na(data$Qobs)]),
46
         labels = FALSE, lwd.ticks = 3, col.ticks = col.na, tck = 0.025, lend = "butt")
unknown's avatar
unknown committed
47
48
    legend("topright", legend = "NA", pch = 15, col = col.na, bty = "n", cex = 0.8)
  }
49

50
  box()
51
52

}