R/FunctionalityScoreLinePlot.R

Defines functions fs.line.plot

Documented in fs.line.plot

#' Line plot of Functionality Score vs. some other axis
#'
#' This function creates a line plot of Functionality Score vs. some other axis,
#' like those generated by the GenericCOMPASSWrapper.R script.
#' Some formatting of the input data table is required, so it is not very "generic".
#'
#' @param fsData data table to plot
#' @param xaxis column in fsData for stratifying x-axis
#' @param yaxis column in fsData for the y-axis (probably functionality score)
#' @param groupName column name which identifies groups (e.g. individuals, for connecting line plots)
#' @param addBox (Optional) A boolean. Adds boxplots for each x-axis value if set to True.
#' @param ylimits (Optional) numeric vector specifying upper and lower y-axis limits
#' @param outdir (Optional) will save plot to a file in this directory, if provided
#' @param fileSuffix (Optional) What to add to end of filename
#' @return Line plot of FS scores, unless outdir is specified
#' @export
#' @keywords Line Plot Functionality
#' @examples
#' \dontrun{
#' # Create the fsData table from the COMPASS output FS text file.
#' fsFile <- "/home/path/to/CD8/8+_CMV/FS_8+_CMV.txt"
#' ylims <- c(0, 0.15)
#' fsTable <- read.table(fsFile, header=TRUE, sep="\t", check.names=FALSE)
#' cellSubset <- as.character(fsTable[,"CellSubset"])
#' condition <- as.character(fsTable[,conditioncol])
#' fsData1 <- t(fsTable[,3:length(colnames(fsTable))])
#' fsData1 <- cbind(fsData1, ldply(rownames(fsData1), .fun = function(x) {strsplit(x, ", Time ")[[1]]}))
#' colnames(fsData1) <- c("FunctionalityScore", "PTID", "Time")
#' # Draw the line plot
#' fs.line.plot(fsData=fsData1, xaxis="Time", yaxis="FunctionalityScore", groupName="PTID", addBox=TRUE, ylimits=ylims)
#' }
fs.line.plot <- function(fsData,
                         outdir=NULL,
                         xaxis,
                         yaxis,
                         groupName,
                         addBox=FALSE,
                         ylimits=NULL,
                         fileSuffix=NULL,
                         condition=NULL) {
  p1 <- ggplot2::ggplot(data=fsData, ggplot2::aes_string(x=xaxis, y=yaxis, group=groupName))
  if (addBox) {
    p1 <- p1 + ggplot2::geom_boxplot(inherit.aes=FALSE, ggplot2::aes_string(x=xaxis, y=yaxis),
                            alpha=0.5, colour = "#3366FF")
  }
  p1 <- p1 +
    ggplot2::geom_point() + ggplot2::geom_line() +
    ggplot2::labs(title=paste(c(cellSubset, ", ", condition), collapse=""),
         x=xaxis, y=yaxis) +
    ggplot2::theme_set(ggplot2::theme_gray(base_size = 15)) +
    ggplot2::coord_cartesian(ylim=ylimits)
  if (is.null(outdir)) {
    p1
  } else {
    pngName <- file.path(outdir, paste(c("LinePlot_", yaxis, "_v_", xaxis, fileSuffix, ".png"), collapse=""))
    cat(paste(c("Saving png to ", pngName, "\n"), collapse=""))
    ggplot2::ggsave(filename=pngName,
           plot=p1,
           width=6.66, height=7.85)
  }
}
seshadrilab/COMPASSHelpers documentation built on May 6, 2019, 6:04 p.m.