R/plot_mi.R

Defines functions plot.mi

#' Plot for Missing Data Indicator
#'
#' Plot for Missing Data Indicator with class "mi"
#' @param x, class 'mi', recommend using function 'complete_ts'
#' @param ... , Other graphical parameters(see par).
#' @return Return to Missing Data Indicator Plot Using Image Function
#' @export
#' @examples
#' Not ready to use


  plot.mi <- function(x, by, ...){

    #if(require(pacman)==FALSE){install.packages("pacman")}
    #pacman::p_load(lubridate, dplyr, zoo)

    op <- par(no.readonly = T)
    par(family = "serif", mar = c(5,9,5,7))

    x <- ifelse(is.na(x), 0, 1)
    mratio <- apply(x, 2,
                    FUN =  function(m) format(round(length(m[m==0])*100/length(m), 1), nsmall = 1))

    image(x, col=c("black", "white"), axes=F, ...)

    xseq <- seq.POSIXt(as.POSIXct(dimnames(x)[[1]][1]),
                       as.POSIXct(dimnames(x)[[1]][length(x[,1])])+hours(1),
                       by = by)

    st_decimal <- decimal_date(ymd(xseq[1]))
    ed_decimal <- decimal_date(ymd(xseq[length(xseq)]))
    dt_decimal <- decimal_date(ymd(xseq))
    at <- (st_decimal-dt_decimal)/(st_decimal-ed_decimal)
    at <- ifelse(at>1, round(at), at)
    x_form <- if(by=="year"){
      format(xseq, "%Y")
    }else if(by=="month"){
      format(xseq, "%Y/%b")
    }

    axis(side = 1,
         at = at,
         labels = x_form,
         las = 1, ...)

    axis(side = 2,
         at = seq(0, 1, length.out = ncol(x)),
         labels = colnames(x),
         las = 1,...)

    par(mgp = c(3,3,0))

    axis(side = 4,
         at = seq(0, 1, length.out = ncol(x)),
         labels = mratio,
         las = 1, hadj = 1, ...)

    mtext(side = 1, text = "Time", line = 2.5,...)
    mtext(side = 2, text = "Observation Variables", line = 7,...)
    mtext(side = 4, text = "Missing Ratio (%)", line = 4,...)

    grid(0, ncol(x))
    box()
    par(op)

  }
Gi-Seop/ODA documentation built on Jan. 6, 2020, 12:49 p.m.