R/boxplotCounts.R

Defines functions boxplotCounts

Documented in boxplotCounts

#' Box-plots representing counts distribution per sample
#'
#' Box-plots representing counts distribution per sample
#'
#' @param counts a numeric matrix of raw or normalized counts
#' @param group vector of the condition from which each sample belongs
#' @param type type of counts used (\code{"raw"} of \code{"norm"})
#' @param out \code{TRUE} to export the figure
#' @param col colors of the bars
#' @param versionName versionName of the project
#' @return The boxplots
#' @author Marie-Agnes Dillies and Hugo Varet

# created Feb 7th, 2012
# modified April 30th, 2012
# modified sept 27th, 2012 (cex.axis)
# modified Oct 30th, 2012 (png)
# modified Jan 16th, 2013 (pdf)
# modified Sept 20th, 2013 (legend)
# modified Sept 24th, 2013 (colors)
# modified Oct 14th, 2013 (dds en entr?e au lieu d'une matrice de comptages)
# modified Oct 25th, 2013 (modification for multiple factors)
# modified Nov 14th, 2013 (counts en param au lieu de dds)
# modified Mar 21st, 2014 (removed outputfile argument)
# modified Aug 5th, 2014 (removed graphDir argument)
# modified August 26th, 2019 (ggplot2)

boxplotCounts <- function(counts, group, type = c("raw", "norm"), out=TRUE,
                          col=c("lightblue","orange","MediumVioletRed","SpringGreen"), versionName="."){
  type <- type[1]
  if (out) pdf(file=paste0("figures/", versionName, "-", type, "-boxplot.pdf"), width=8)
  counts <- removeNul(counts)
  group <- data.frame(group=apply(group, 1, paste, collapse="-"))
  group$group <- factor(group$group, levels=unique(group$group))
  d <- stack(as.data.frame(counts))
  d$group <- rep(group$group, each=nrow(counts))
  print(ggplot(d) + 
          geom_boxplot(aes(x=.data$ind, y=.data$values+1, fill=.data$group), show.legend=TRUE) +
          labs(fill="") +
          scale_y_continuous(trans = log10_trans(),
                             breaks = trans_breaks("log10", function(x) 10^x),
                             labels = trans_format("log10", math_format(~10^.x))) +
          scale_fill_manual(values=col) +
          xlab("Samples") +
          ylab(ifelse(type=="raw", "Raw counts", "Normalized counts")) +
          ggtitle(paste0(versionName, " - ", type, " counts distribution")) +
          theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5)))
  if (out) dev.off()
}
biomics-pasteur-fr/RNADiff documentation built on Aug. 27, 2020, 12:44 a.m.