From 5779a676e955686bb4d005a53ad81fc6046c51d1 Mon Sep 17 00:00:00 2001 From: TuomasBorman Date: Mon, 3 Jun 2024 21:12:40 +0300 Subject: [PATCH] Support box plot in addition to violin plot --- DESCRIPTION | 9 ++++++--- R/plot_central.R | 19 ++++++++++++++++--- man/scater-plot-args.Rd | 1 + 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3b673f2..ede45f1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,10 +10,13 @@ Authors@R: c( person("Alan", "O'Callaghan", role=c("ctb", "cre"), email="alan.ocallaghan@outlook.com"), person("Yun", "Peng", role=c("ctb"), email="yunyunp96@gmail.com"), person("Leo", "Lahti", role=c("ctb"), email="leo.lahti@utu.fi", - comment = c(ORCID = "0000-0001-5537-637X")) + comment = c(ORCID = "0000-0001-5537-637X")), + person("Tuomas", "Borman", role = c("ctb"), + email = "tuomas.v.borman@utu.fi", + comment = c(ORCID = "0000-0002-8563-8884")) ) -Version: 1.31.2 -Date: 2024-01-28 +Version: 1.31.3 +Date: 2024-06-03 License: GPL-3 Title: Single-Cell Analysis Toolkit for Gene Expression Data in R Description: A collection of tools for doing various analyses of diff --git a/R/plot_central.R b/R/plot_central.R index 23b9001..60dab9f 100644 --- a/R/plot_central.R +++ b/R/plot_central.R @@ -18,6 +18,7 @@ #' \item{\code{jitter_type}:}{String to define how points are to be jittered in a violin plot. #' This is either with random jitter on the x-axis (\code{"jitter"}) or in a \dQuote{beeswarm} style (if \code{"swarm"}, default). #' The latter usually looks more attractive, but for datasets with a large number of cells, or for dense plots, the jitter option may work better.} +#' \item{\code{layout}:}{String to define layout of plot. Must be \code{"violin"} (violin plot, default) or \code{"box"} (box plot).} #' } #' #' @section Distributional calculations: @@ -63,7 +64,7 @@ NULL #' @importFrom ggbeeswarm geom_quasirandom #' @importFrom ggplot2 ggplot geom_violin xlab ylab stat_summary geom_jitter #' position_jitter coord_flip geom_point stat_smooth geom_tile theme_bw theme -#' geom_bin2d geom_hex stat_summary_2d stat_summary_hex +#' geom_bin2d geom_hex stat_summary_2d stat_summary_hex geom_boxplot .central_plotter <- function(object, xlab = NULL, ylab = NULL, colour_by = NULL, shape_by = NULL, size_by = NULL, fill_by = NULL, show_median = FALSE, show_violin = TRUE, show_smooth = FALSE, show_se = TRUE, @@ -71,7 +72,7 @@ NULL theme_size = 10, point_alpha = 0.6, point_size = NULL, point_shape = 19, add_legend = TRUE, point_FUN = NULL, jitter_type = "swarm", rasterise = FALSE, scattermore = FALSE, bins = NULL, - summary_fun = "sum", hex = FALSE) + summary_fun = "sum", hex = FALSE, layout = "violin") # Internal ggplot-creating function to plot anything that involves points. # Creates either a scatter plot, (horizontal) violin plots, or a rectangle plot. { @@ -90,7 +91,7 @@ NULL # Adding violins. plot_out <- ggplot(object, aes(x=.data$X, y=.data$Y)) + xlab(xlab) + ylab(ylab) - if (show_violin) { + if (show_violin && layout == "violin") { if (is.null(fill_by)) { viol_args <- list(fill="grey90") } else { @@ -101,6 +102,18 @@ NULL geom_violin, c(viol_args, list(colour = "gray60", alpha = 0.2, scale = "width", width = 0.8)) ) + } else if (layout == "box") { + # User can choose also to plot box plot instaed of violin plot + if (is.null(fill_by)) { + box_args <- list(fill="grey90") + } else { + box_args <- list(mapping=aes(fill=.data[[fill_by]])) + } + plot_out <- plot_out + + do.call( + geom_boxplot, + c(box_args, list(colour = "black", alpha = 0.2)) + ) } # Adding median, if requested. diff --git a/man/scater-plot-args.Rd b/man/scater-plot-args.Rd index a007d16..2414431 100644 --- a/man/scater-plot-args.Rd +++ b/man/scater-plot-args.Rd @@ -23,6 +23,7 @@ of the points. Details see \code{vignette("ggplot2-specs")}. Defaults to \item{\code{jitter_type}:}{String to define how points are to be jittered in a violin plot. This is either with random jitter on the x-axis (\code{"jitter"}) or in a \dQuote{beeswarm} style (if \code{"swarm"}, default). The latter usually looks more attractive, but for datasets with a large number of cells, or for dense plots, the jitter option may work better.} +\item{\code{layout}:}{String to define layout of plot. Must be \code{"violin"} (violin plot, default) or \code{"box"} (box plot).} } }