R/ds.plotMissings.R

Defines functions ds.plotMissings

Documented in ds.plotMissings

#' @title Draw plot with percentage of missings
#' 
#' @description Create a plot with the percentage of missings for the exposures and phenotypes of an 
#' ExposomeSet object
#'
#' @param missings \code{ds.tableMissings} Table of missings generated by \code{ds.tableMissings()}.
#' @param x.max \code{numeric} (default \code{100})  Fix the maxium value of the X-axis for the percentage plot.
#' @param datasources a list of \code{\link{DSConnection-class}} (default \code{NULL}) objects obtained after login
#'
#' @return List of objects of class \code{ggplot}, calling it will render the actual plot
#'
#' @examples
#' \dontrun{Refer to the package Vignette for examples.}
#' @export

ds.plotMissings <- function(missings, x.max = 100, datasources = NULL){
  
  if(!inherits(missings, "ds.tableMissings")){
    stop('Object provided is not of class "ds.tableMissings", create one using `ds.tableMissings()`')
  }
  
  if (is.null(datasources)) {
    datasources <- DSI::datashield.connections_find()
  }
  
  output <- missings$output
  set <- missings$set

  plot <- list()

  if(output == 'n'){
    for(i in 1:(length(missings)-2)){
      plot[[i]] <- ggplot2::ggplot(data.frame(missings[[i]]),
                              ggplot2::aes(seq_along(missings[[i]]), missings[[i]], fill = missings[[i]])) +
        ggplot2::geom_bar(stat = "identity", width = 1)
      plot[[i]] <- plot[[i]] + ggplot2::theme_bw() + ggplot2::xlim(names(missings[[i]]))
      plot[[i]] <- plot[[i]] + ggplot2::scale_fill_continuous(name = "%",
                                                    breaks = seq(0, max(missings[[i]]), 20),
                                                    limits = c(0, max(missings[[i]])), low="violet", high="violetred4")
      plot[[i]] <- plot[[i]] + ggplot2::theme(legend.position = "none")
      plot[[i]] <- plot[[i]] + ggplot2::ylab("Missing Data (ocurrences)")
      plot[[i]] <- plot[[i]] + ggplot2::xlab(set)
      plot[[i]] <- plot[[i]] + ggplot2::coord_flip()
    }
  }
  else{
    for(i in 1:(length(missings)-2)){
      plot[[i]] <- ggplot2::ggplot(data.frame(missings[[i]]) * 100,
                                   ggplot2::aes(seq_along(missings[[i]]), missings[[i]] * 100, fill = missings[[i]] * 100)) +
        ggplot2::geom_bar(stat = "identity", width = 1)
      plot[[i]] <- plot[[i]] + ggplot2::theme_bw() + ggplot2::xlim(names(missings[[i]]))
      plot[[i]] <- plot[[i]] + ggplot2::scale_fill_continuous(name = "%",
                                                              breaks = seq(0, 100, 20),
                                                              limits = c(0, 100), low="violet", high="violetred4")
      plot[[i]] <- plot[[i]] + ggplot2::ylab("Missing Data %")
      plot[[i]] <- plot[[i]] + ggplot2::xlab(set)
      plot[[i]] <- plot[[i]] + ggplot2::coord_flip()
      plot[[i]] <- plot[[i]] + ggplot2::scale_y_continuous(limits = c(0, x.max))
    }
  }

  names(plot) <- names(missings)[1:(length(missings)-3)]
  return(plot)
  
}
isglobal-brge/dsExposomeClient documentation built on March 5, 2024, 12:26 p.m.