Skip to content

Commit

Permalink
add plotSig mixOmicsTeam#134
Browse files Browse the repository at this point in the history
  • Loading branch information
aljabadi committed Jun 22, 2021
1 parent 7d147b1 commit dbc7c4e
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export(plotArrow)
export(plotDiablo)
export(plotIndiv)
export(plotLoadings)
export(plotSig)
export(plotVar)
export(pls)
export(plsda)
Expand Down
100 changes: 100 additions & 0 deletions R/plotSig.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#' 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
#' see ?block.splsda and ?block.spls
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
}
8 changes: 8 additions & 0 deletions examples/block.spls-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions examples/block.splsda-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
4 changes: 2 additions & 2 deletions man/auroc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/block.spls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/block.splsda.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions man/plotSig.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbc7c4e

Please sign in to comment.