diff --git a/NAMESPACE b/NAMESPACE index 5ae5e015..10c14899 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -146,6 +146,7 @@ export(plotArrow) export(plotDiablo) export(plotIndiv) export(plotLoadings) +export(plotSig) export(plotVar) export(pls) export(plsda) diff --git a/R/plotSig.R b/R/plotSig.R new file mode 100644 index 00000000..695b7c71 --- /dev/null +++ b/R/plotSig.R @@ -0,0 +1,99 @@ +#' Plot the values for signature variables +#' +#' Plots the standardaised 'expression' values for the variables for +#' a given block on a given component. +#' +#' @param object An object of class \code{block.splsda} or \code{block.spls} +#' @param block Name of the block to use +#' @param comp Integer, the component to use +#' @param group Factor, the grouping variable (not required for DA objects) +#' @param top_n Integer, only show this number of top features +#' @template arg/col.per.group +#' @param contrib The contribution type. one of c('max', 'min') +#' @param global Logical indicating whether to seggregate all features or show +#' the global plots. +#' +#' @return A ggplot object +#' @export +#' +#' @examples +plotSig <- + function(object, + block, + comp = 1, + group = NULL, + top_n = NULL, + col.per.group = NULL, + contrib = c('max', 'min'), + global = FALSE, + title = NULL) + { + + contrib <- match.arg(contrib) + + blocks <- names(object$X) + if (!block %in% blocks) + stop(message = sprintf("block must be one of: %s", paste0(blocks, collapse = ', '))) + df <- data.frame(object$X[[block]], check.names = FALSE) + + ## group factor + group <- .get.group(group, object, n_ind = nrow(df)) + + col.group <- .get.cols.and.group(col.per.group = col.per.group, + group = group) + group <- col.group$group + col.per.group <- col.group$col.per.group + vars <- selectVar(object, block=block, comp=comp)[[1]]$value + if (contrib == 'max') + { + vars <- vars[vars$value.var > 0, drop=FALSE, ] + } else + { + vars <- vars[vars$value.var < 0, drop=FALSE, ] + } + if (is.null(top_n)) + { + top_n <- nrow(vars) + } else { + top_n <- min(as.integer(top_n), nrow(vars)) + } + + + if (top_n < 3) + { + message("Too few variables match the provided criteria. Aborting the plot.") + return(NULL) + } + vars <- vars[seq_len(top_n),,drop=FALSE] + var.names <- rownames(vars) + if (!all(var.names %in% colnames(df))) + .stop("Unexpected error.\n") + df <- df[,var.names] + df$group <- group + df <- + melt(df, + id.vars = 'group', + variable.name = 'feature', + value.name = 'value') + df$feature <- factor(df$feature, levels = var.names, ordered = TRUE) + + if (global) + { + df$feature <- 'plotSig' + } + + p <- ggplot(df, aes(group, value, fill=group)) + + geom_violin(adjust=0.9) + + geom_boxplot(width=0.1) + + scale_fill_manual(values = col.per.group) + + theme_classic() + + labs(x='', + y='value (standardised)', + title = title) + + theme(legend.position = 'none', + axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + plot.title = element_text(hjust = 0.5), + strip.background = element_rect(colour="black", fill="grey80")) + + facet_wrap(.~feature) + p +} diff --git a/examples/block.spls-examples.R b/examples/block.spls-examples.R index 708d171d..032fdde1 100644 --- a/examples/block.spls-examples.R +++ b/examples/block.spls-examples.R @@ -22,6 +22,14 @@ plotIndiv(TCGA.block.spls, group = breast.TCGA$data.train$subtype, ind.names = # illustrates coefficient weights in each block plotLoadings(TCGA.block.spls, ncomp = 1) plotVar(TCGA.block.spls, style = 'graphics', legend = TRUE) + +## plot signature variables for mrna and mirna +group <- breast.TCGA$data.train$subtype +# min contribution for mrna +plotSig(object = TCGA.block.spls, comp = 1, block = 'mrna', contrib = 'min', group = group) +# max contribution for protein +plotSig(object = TCGA.block.spls, comp = 1, block = 'Y', contrib = 'max', group = group) + \dontrun{ network(TCGA.block.spls) } diff --git a/examples/block.splsda-examples.R b/examples/block.splsda-examples.R index 828cc7b0..442ad580 100644 --- a/examples/block.splsda-examples.R +++ b/examples/block.splsda-examples.R @@ -34,3 +34,11 @@ TCGA.block.splsda$design # illustrates coefficient weights in each block plotLoadings(TCGA.block.splsda, ncomp = 1, contrib = 'max') plotVar(TCGA.block.splsda, style = 'graphics', legend = TRUE) + +## plot signature variables for mrna and mirna +# min contribution for mrna +plotSig(object = TCGA.block.splsda, comp = 1, block = 'mrna', contrib = 'min') +## plot only the top 6 variables +plotSig(object = TCGA.block.splsda, comp = 1, block = 'mrna', contrib = 'min', top_n = 6) +# max contribution for protein +plotSig(object = TCGA.block.splsda, comp = 1, block = 'protein', contrib = 'max') diff --git a/man-roxygen/arg/col.per.group.R b/man-roxygen/arg/col.per.group.R new file mode 100644 index 00000000..4f846000 --- /dev/null +++ b/man-roxygen/arg/col.per.group.R @@ -0,0 +1,2 @@ +#' @param col.per.group character (or symbol) color to be used when 'group' is +#' defined. Vector of the same length as the number of groups. diff --git a/man/block.spls.Rd b/man/block.spls.Rd index bded0629..dc4a4766 100644 --- a/man/block.spls.Rd +++ b/man/block.spls.Rd @@ -159,6 +159,14 @@ plotIndiv(TCGA.block.spls, group = breast.TCGA$data.train$subtype, ind.names = # illustrates coefficient weights in each block plotLoadings(TCGA.block.spls, ncomp = 1) plotVar(TCGA.block.spls, style = 'graphics', legend = TRUE) + +## plot signature variables for mrna and mirna +group <- breast.TCGA$data.train$subtype +# min contribution for mrna +plotSig(object = TCGA.block.spls, comp = 1, block = 'mrna', contrib = 'min', group = group) +# max contribution for protein +plotSig(object = TCGA.block.spls, comp = 1, block = 'Y', contrib = 'max', group = group) + \dontrun{ network(TCGA.block.spls) } diff --git a/man/block.splsda.Rd b/man/block.splsda.Rd index 92b14d88..23a953cd 100644 --- a/man/block.splsda.Rd +++ b/man/block.splsda.Rd @@ -177,6 +177,14 @@ TCGA.block.splsda$design # illustrates coefficient weights in each block plotLoadings(TCGA.block.splsda, ncomp = 1, contrib = 'max') plotVar(TCGA.block.splsda, style = 'graphics', legend = TRUE) + +## plot signature variables for mrna and mirna +# min contribution for mrna +plotSig(object = TCGA.block.splsda, comp = 1, block = 'mrna', contrib = 'min') +## plot only the top 6 variables +plotSig(object = TCGA.block.splsda, comp = 1, block = 'mrna', contrib = 'min', top_n = 6) +# max contribution for protein +plotSig(object = TCGA.block.splsda, comp = 1, block = 'protein', contrib = 'max') } \references{ On multiple integration with sPLS-DA and 4 data blocks: diff --git a/man/plotSig.Rd b/man/plotSig.Rd new file mode 100644 index 00000000..42ff5415 --- /dev/null +++ b/man/plotSig.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotSig.R +\name{plotSig} +\alias{plotSig} +\title{Plot the values for signature variables} +\usage{ +plotSig( + object, + block, + comp = 1, + group = NULL, + top_n = NULL, + col.per.group = NULL, + contrib = c("max", "min"), + global = FALSE, + title = NULL +) +} +\arguments{ +\item{object}{An object of class \code{block.splsda} or \code{block.spls}} + +\item{block}{Name of the block to use} + +\item{comp}{Integer, the component to use} + +\item{group}{Factor, the grouping variable (not required for DA objects)} + +\item{top_n}{Integer, only show this number of top features} + +\item{col.per.group}{character (or symbol) color to be used when 'group' is +defined. Vector of the same length as the number of groups.} + +\item{contrib}{The contribution type. one of c('max', 'min')} + +\item{global}{Logical indicating whether to seggregate all features or show +the global plots.} +} +\value{ +A ggplot object +} +\description{ +Plots the standardaised 'expression' values for the variables for +a given block on a given component. +}