plot_coverage_ts.R 2.26 KB
Newer Older
1

2
#' plot_coverage_ts
3

4
#' plot_coverage_ts
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#' @param x object of class tbl_friendlyts
#' @param fromDate fromDate
#' @param toDate toDate
#' @param timeResolution timeResolution
#' @param tzone tzone
#' @param path path
#' @param name name
#' @param width_mm width_mm
#' @param height_mm height_mm
#' @param DESKTOP boolean
#' @keywords friendlyts
#' @export
#' @examples
#' \dontrun{
19
#' plot_coverage_ts()
20
#' }
21
plot_coverage_ts <- function(x,fromDate=NULL,toDate=NULL,timeResolution=NULL,tzone="UTC",path=tempdir(),name=NULL,width_mm=200,height_mm=200,DESKTOP=TRUE){
22
23
24
25


  # sub-period
  if (!is.null(fromDate) && !is.null(toDate) && !is.null(timeResolution)) {
26
     tmp <- plotfts::sub_period(tbl_fts=x,fromDate=fromDate,toDate=toDate,timeResolution=timeResolution,tzone=tzone)
27
28
29
30
31
32
33
34
35
36
  } else tmp <- x

  df <- friendlyts::dfts(tbl_fts=tmp,timeResolution=timeResolution,tzone=tzone)
  df[["date"]] <- as.POSIXct(df[["date"]])

  target <- setdiff(names(df), "date")

  tmp <- lapply(target,function(y) {
    res <- rep(1,length(df[[y]]))
    res[is.na(df[[y]])] <- NA
37
    res <- data.frame(date=df[["date"]],available=res,target=y,stringsAsFactors=FALSE)
38
39
40
41
    return(res)
  })

  tmp <- do.call("rbind",tmp)
42
  tmp <- data.frame(date=tmp[,"date"],available=tmp[,"available"]*as.integer(as.factor(tmp[,"target"])),target = as.factor(tmp[,"target"]))
43
44
45
46
47

    #start plot
  if (!(DESKTOP)) {

    if(is.null(name)) {
48
49
      filename <- normalizePath(file.path(path,paste0("available_ts.tiff")),mustWork = FALSE)
    } else filename <-normalizePath(file.path(path,paste0("available_ts_",name,".tiff")),mustWork = FALSE)
50
51
52
53
54
55
56
57
58
59
60

    grDevices::tiff(filename = filename, width = width_mm, height = height_mm,
          units = "mm", pointsize = 12,
          compression = "lzw",
          bg = "transparent", res = 300)

  } else grDevices::dev.new()

  # plot


61
  p <- ggplot2::ggplot(tmp, ggplot2::aes(x=date,y=available,colour=target)) +
62
  ggplot2::geom_point() +
63
  #ggplot2::labs(x = "date",y = "non-missing values") #+
64
65
  ggplot2::theme(
    axis.line.y=ggplot2::element_blank(),
66
67
68
69
    axis.text.y=ggplot2::element_blank()
    #panel.grid.major = ggplot2::element_blank(),
    #panel.grid.minor = ggplot2::element_blank()
  )
70
71
72
73
74
75
76
77
78
79
  print(p)

  if (!(DESKTOP)) {
    grDevices::dev.off()
    return(filename)
  } else {
    return(TRUE)
  }

}