08f1390e |
#' Make a simple 2d plot using two variables with ggplot2
#'
#' @param df a data.frame with at least two columns
|
904cd2ee |
#' @param xCol the name of the column in df with the "x" data. See Note
#' @param yCol the name of the column in df with the "y" data. See Note
#' @param xLabel the x plot label
#' @param yLabel the y plot label
#' @param title the plot title, if null the correlation will appear (DEFAULT: NULL)
#' @param classCol the name of the column with the classes. Values in column of df must be a factor (DEFAULT: NULL)
#' @param colorPalette a named vector with the names classes and value colors (DEFAULT: NULL)
#' @param showLegend boolean, whether to show the legend (DEFAULT: FALSE)
|
08f1390e |
#' @param showTrendLine boolean, whether to show the trendline
|
904cd2ee |
#' @param showTitle boolean, whether to show the title
#' @param alpha value from 0-1, where 0 indicates transparent points
|
2e575382 |
#' @param numberColPrefix a prefix to add to column names that start with a number that causes issues with ggplot (DEFAULT: X)
|
08f1390e |
#'
#' @return a ggplot object
#'
#' @examples
#' \dontrun{
#' # Load data
#' nci60DrugActZ <- exprs(getAct(rcellminerData::drugData))
#' nci60GeneExpZ <- getAllFeatureData(rcellminerData::molData)[["exp"]]
#' # Load colors
#' colorTab <- loadNciColorSet(returnDf=TRUE)
#' tissueColorTab <- unique(colorTab[, c("tissues", "colors")])
#' # Merge data
#' df <- as.data.frame(t(rbind(nci60DrugActZ["94600",], nci60GeneExpZ["SLFN11",])))
#' colnames(df) <- c("y", "x")
#' df <- cbind(df, colorTab)
#' # Plot data
|
904cd2ee |
#' plotCellMiner2D(df, xCol="x", yCol="y", xLabel="SLFN11", yLabel="94600")
|
08f1390e |
#' plotCellMiner2D(df, xCol="x", yCol="y", showTrendLine = FALSE, showTitle = FALSE)
#' plotCellMiner2D(df, xCol="x", yCol="y", showTrendLine = FALSE, showLegend = FALSE)
#' }
#'
#' @author Augustin Luna <augustin AT mail.nih.gov>
#'
|
904cd2ee |
#' @note Uses ggplot aes_string() which uses parse() to turn your text expression into a proper R symbol that can be resolved within the data.frame. Avoid numbers and spaces in
#'
#' @importFrom ggplot2 ggplot geom_point theme_bw scale_colour_manual xlab ylab geom_smooth ggtitle theme aes_string
|
08f1390e |
#'
#' @concept rcellminer
#' @export
|
904cd2ee |
plotCellMiner2D <- function(df, xCol="x", yCol="y", xLabel=xCol, yLabel=yCol,
title=NULL, colorPalette=NULL, classCol=NULL, tooltipCol=NULL,
showLegend=FALSE, showTrendLine=TRUE, showTitle=TRUE,
|
2e575382 |
alpha=1, numberColPrefix="X") {
|
904cd2ee |
# nci60DrugActZ <- exprs(getAct(rcellminerData::drugData))
# nci60GeneExpZ <- getAllFeatureData(rcellminerData::molData)[["exp"]]
# # Load colors
# colorTab <- loadNciColorSet(returnDf=TRUE)
# tissueColorTab <- unique(colorTab[, c("tissues", "colors")])
# # Merge data
# xCol <- "SLFN11"
# yCol <- "94600"
# classCol <- "tissues"
# xLabel <- xCol
# yLabel <- yCol
# title <- NULL
# df <- data.frame(y=nci60DrugActZ[yCol,], x=nci60GeneExpZ[xCol,])
# yCol <- "X94600" # MUST NOT BE A NUMBER
# colnames(df) <- c(yCol, xCol)
# df <- cbind(df, colorTab)
# df[, classCol] <- as.factor(df[, classCol])
# colorPalette <- tissueColorTab[, "colors"]
# names(colorPalette) <- tissueColorTab[, classCol]
# colors <- rep("blue", nrow(df))
# colors[1:10, "colors"] <- "red"
# showLegend <- FALSE
# showTrendLine <- TRUE
# showTitle <- TRUE
# alpha <- 1
|
2e575382 |
# Fix column names if they start with a number
if (grepl("^[0-9]", xCol)) {
tmpXCol <- paste0(numberColPrefix, xCol)
df[, tmpXCol] <- df[, xCol]
xCol <- tmpXCol
}
if (grepl("^[0-9]", yCol)) {
tmpYCol <- paste0(numberColPrefix, yCol)
df[, tmpYCol] <- df[, yCol]
yCol <- tmpYCol
}
# Create title
|
08f1390e |
if(is.null(title)) {
corResults <- cor.test(df[,xCol], df[,yCol], use="pairwise.complete.obs")
|
904cd2ee |
title <- paste0(paste(yLabel, '~', xLabel),
|
08f1390e |
', r=', round(corResults$estimate, 2),
' p=', signif(corResults$p.value, 2))
}
# Plot image
p1 <- ggplot(data=df, aes_string(x=xCol, y=yCol))
p1 <- p1 + theme_bw()
|
904cd2ee |
if(!is.null(colorPalette) && !is.null(classCol)) {
p1 <- p1 + geom_point(aes_string(color=classCol, text=tooltipCol), alpha=alpha)
p1 <- p1 + scale_colour_manual(name="", values=colorPalette)
} else {
p1 <- p1 + geom_point(aes_string(text=tooltipCol), color="#0000FF", alpha=alpha)
|
08f1390e |
}
|
904cd2ee |
if(!is.null(xLabel)) {
p1 <- p1 + xlab(xLabel)
|
08f1390e |
} else {
p1 <- p1 + xlab(xCol)
}
|
904cd2ee |
if(!is.null(yLabel)) {
p1 <- p1 + ylab(yLabel)
|
08f1390e |
} else {
p1 <- p1 + ylab(yCol)
}
if(showTrendLine) {
p1 <- p1 + geom_smooth(method = "lm", se = FALSE, color = "red")
}
if(showTitle) {
p1 <- p1 + ggtitle(title)
}
if(!showLegend) {
p1 <- p1 + theme(legend.position="none")
}
return(p1)
|
904cd2ee |
}
|