R/ggExplainedVariance.R

Defines functions ggExplainedVariance

Documented in ggExplainedVariance

#' Plotting of the explained variances
#'
#'
#' @param subnet a list generated by the \code{\link{generatePathwaysNetwork}} function
#' @param selection which groups should be used for the plot.
#' @import ggplot2
#' @import data.table
#' @export
#' @examples
#' data(qmdiab.data)
#' data(qmdiab.annos)
#' 
#' pathway.graph <- generatePathwaysNetwork(
#'   data = qmdiab.data,
#'   annotations = qmdiab.annos, level = "Sub.pathway"
#' )
#' ggExplainedVariance(pathway.graph)
ggExplainedVariance <- function(subnet, selection = NULL) {
  groups <- names(subnet$representatives$expvar)

  if (!is.null(groups)) {
    if (!is.null(selection)) {
      groups <- groups[selection]
    }

    DT <- data.table(
      group = character(), principal.component.number = character(),
      explained.variance = numeric()
    )
    for (group in groups) {
      expvar <- subnet$representatives$expvar[[group]]
      DT <- rbind(DT, data.table(
        group = group, principal.component.number = seq_len(length(expvar)),
        explained.variance = expvar
      ))
    }
    g <- ggplot(DT[principal.component.number == 1], aes(x = factor(group), y = explained.variance))
    g <- g + geom_point() + theme(axis.text.x = element_text(angle = 75, hjust = 1)) + ylim(c(0, 1)) + xlab("")
    g + ylab("explained variance of eigenmetabolites")
  } else {
    message("No explained variances available if representatives are not eigenmetabolites.\n")
  }
}
krumsieklab/MoDentify documentation built on March 24, 2021, 9:01 p.m.