From fb0ac1bfe2752697d4b9f52557984418c0c184b1 Mon Sep 17 00:00:00 2001 From: vertesy Date: Fri, 21 Jun 2024 10:45:00 +0200 Subject: [PATCH] nf qSeuViolin colors --- ...-test-048c268b-6bdf-4f06-8911-f5e858e496e6 | 1 + R/Seurat.Utils.Visualization.R | 36 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 .write-test-048c268b-6bdf-4f06-8911-f5e858e496e6 diff --git a/.write-test-048c268b-6bdf-4f06-8911-f5e858e496e6 b/.write-test-048c268b-6bdf-4f06-8911-f5e858e496e6 new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/.write-test-048c268b-6bdf-4f06-8911-f5e858e496e6 @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/R/Seurat.Utils.Visualization.R b/R/Seurat.Utils.Visualization.R index 4ca0fe9..8074c08 100644 --- a/R/Seurat.Utils.Visualization.R +++ b/R/Seurat.Utils.Visualization.R @@ -1726,6 +1726,8 @@ qFeatureScatter <- function( #' @param caption A character string or logical for the plot caption. If FALSE, no caption is displayed. #' @param ylab Y-axis label. Default is "Expression". #' @param ylimit A numeric vector specifying the limits of the y-axis. +#' @param legend Show legend; Default: opposite of `label`. +#' @param legend.pos Position of legend; Default: 'NULL'. #' @param show_plot A logical indicating whether to display the plot. #' @param w Width of the plot. #' @param h Height of the plot. @@ -1743,6 +1745,7 @@ qSeuViolin <- function( feature = "nFeature_RNA", ident = GetNamedClusteringRuns(obj)[1], split.by = NULL, + colors = NULL, replace.na = FALSE, pt.size = 0.5, sub = NULL, @@ -1753,6 +1756,8 @@ qSeuViolin <- function( hline = FALSE, ylab = "Expression", ylimit = NULL, + legend = TRUE, + legend.pos = NULL, # c("top", "bottom", "left", "right", "none")[2], show_plot = TRUE, w = 9, h = 5, ...) { @@ -1787,22 +1792,37 @@ qSeuViolin <- function( obj@meta.data[[feature]] <- na.replace(x = obj@meta.data[[feature]], replace = 0) } - p <- VlnPlot(object = obj, features = feature, split.by = split.by, group.by = ident, + if (!is.null(colors)) { + stopifnot(colors %in% colnames(obj@meta.data)) + col_long <- as.factor(unlist(obj[[colors]])) + split_col <- unlist(obj[[ident]]) + colors <- as.factor.numeric(sapply(split(col_long, split_col), unique)) + stopifnot("colors cannot be uniquely split by ident. Set colors = NULL!" = length(colors) == length(unique(split_col))) + } + + p.obj <- Seurat::VlnPlot(object = obj, + features = feature, group.by = ident, + cols = colors, split.by = split.by, pt.size = pt.size, ...) + theme(axis.title.x = element_blank()) + labs(y = ylab) + - ggtitle(label = ttl, subtitle = subt ) + ggtitle(label = ttl, subtitle = subt ) + + if (!legend) NoLegend() else NULL + # Add additional customization, if needed.. - if (!is.null(ylimit)) p <- p + ylim(ylimit[1], ylimit[2]) - if (logY) p <- p + ggplot2::scale_y_log10() - if (hline) p <- p + ggplot2::geom_hline(yintercept = hline) - if (!isFALSE(caption)) p <- p + ggplot2::labs(caption = caption) + if (!is.null(ylimit)) p.obj <- p.obj + ylim(ylimit[1], ylimit[2]) + if (logY) p.obj <- p.obj + ggplot2::scale_y_log10() + if (hline) p.obj <- p.obj + ggplot2::geom_hline(yintercept = hline) + if (!isFALSE(caption)) p.obj <- p.obj + ggplot2::labs(caption = caption) + if (!is.null(legend.pos)) p.obj <- p.obj + theme(legend.position = legend.pos) + + # Save the plot. TTL <- ppp(as.character(feature), suffix, flag.nameiftrue(logY)) - qqSave(p, title = TTL, suffix = ppp("by.", ident), w = w, h = h) - if (show_plot) p + qqSave(p.obj, title = TTL, suffix = ppp("by.", ident), w = w, h = h) + if (show_plot) p.obj }