R/plot_chip_qpcr.R

Defines functions plot_chip_qpcr

Documented in plot_chip_qpcr

#' @title plot_chip_qpcr
#'
#' @param chip_qpcr_df The data.frame generated by the \code{\link{do_chip_qpcr}} function
#' @param palette Any color palette from RColorBrewer
#' @param xlab x axis label
#' @param ylab y axis label
#' @param title Main title for plot
#' @param legend.title Title for plot legend
#' @param theme_classic Boolean value. TRUE will yield a plot with the 'classic' background. FALSE will yield a plot
#' with the default ggplot2 theme.
#' @param method Indicates which calculation should be performed. Must be one of ("i", "input") for input,
#' or ("fe", "fold enrichment") for fold enrichment.
#' @param hideInput Boolean value indicating whether or not the input samples themselves should be removed from the plot
#' @param inputName A character vector of the common name given to all input samples
#' @param hideMock Boolean value indicating whether or not the mock samples themselves should be removed from the plot
#' @param mockName A character vector of the common name given to all mock samples
#'
#' @details The `chip_qpcr_df` object passed to the function must be in the same format generated by the
#' \code{\link{do_chip_qpcr}} function.
#'
#' @return A ggplot2 barplot
#' @export
#'
#' @examples
#'
plot_chip_qpcr <- function(chip_qpcr_df, palette = "Set1", xlab = "", ylab = "", title = "",
                           legend.title = "", theme_classic = TRUE, method = NULL,
                           hideInput = FALSE, inputName, hideMock = FALSE, mockName){
  # need method for plotting
  if (!is.null(method)){
    if (!method %in% c("i", "input", "fe", "fold enrichment")){
      stop("method must be one of: 'i', 'input', 'fe', or 'fold enrichment'!")
    }
  } else {
    stop("method must be set!")
  }

  # check if user wants input/mock shown
  if (method %in% c("i", "input")){
    if(hideInput){
      if(missing(inputName)){
        stop("Must provide inputName to remove it from the plot!")
      }
      chip_qpcr_df <- chip_qpcr_df[!grepl(inputName, chip_qpcr_df$Var1), ]
    }
  }

  if (method %in% c("fe", "fold enrichment")){
    if(hideMock){
      if(missing(mockName)){
        stop("Must provide mockName to remove it from the plot!")
      }
      chip_qpcr_df <- chip_qpcr_df[!grepl(mockName, chip_qpcr_df$Var1), ]
    }
  }

  # make plot
  ggplot(data = chip_qpcr_df, aes(x = chip_qpcr_df[,1], y = chip_qpcr_df[,5], fill = chip_qpcr_df[,2],
                                  ymin = chip_qpcr_df[,6], ymax = chip_qpcr_df[,7])) +
    geom_bar(stat = "identity", position = "dodge") +
    geom_errorbar(position = position_dodge(width = 0.9), width = 0.5) +
    geom_hline(yintercept = 0.0, color = "black", linetype = "solid", size = 1) +
    scale_fill_brewer(palette = palette) +
    xlab(xlab) +
    ylab(ylab) +
    ggtitle(title) +
    labs(fill = legend.title) +
    scale_y_continuous(expand = c(0, 0)) +
    if (theme_classic){
      theme(plot.title = element_text(hjust=0.5, face = "bold", size = 20),
            panel.background = element_blank(), axis.line = element_line(color = "black", size = 2))
    } else {
      theme(plot.title = element_text(hjust=0.5, face = "bold", size = 20))
    }
}
sccallahan/WARPqpcr documentation built on March 1, 2020, 1:23 a.m.