Skip to content

Commit

Permalink
Support box plot in addition to violin plot (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
TuomasBorman authored Jun 4, 2024
1 parent c6f5a13 commit 096e367
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ Authors@R: c(
person("Alan", "O'Callaghan", role=c("ctb", "cre"), email="[email protected]"),
person("Yun", "Peng", role=c("ctb"), email="[email protected]"),
person("Leo", "Lahti", role=c("ctb"), email="[email protected]",
comment = c(ORCID = "0000-0001-5537-637X"))
comment = c(ORCID = "0000-0001-5537-637X")),
person("Tuomas", "Borman", role = c("ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-8563-8884"))
)
Version: 1.31.2
Date: 2024-01-28
Version: 1.31.3
Date: 2024-06-04
License: GPL-3
Title: Single-Cell Analysis Toolkit for Gene Expression Data in R
Description: A collection of tools for doing various analyses of
Expand Down
4 changes: 4 additions & 0 deletions R/plotExpression.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
#' plotExpression(example_sce, rownames(example_sce)[1:6],
#' colour_by = "Mutation_Status", shape_by = "Treatment",
#' size_by = "Gene_0010")
#'
#' ## use boxplot as well as violin plot
#' plotExpression(example_sce, rownames(example_sce)[1:6],
#' show_boxplot = TRUE, show_violin = FALSE)
#'
#' ## plot expression against expression values for Gene_0004
#' plotExpression(example_sce, rownames(example_sce)[1:4],
Expand Down
27 changes: 25 additions & 2 deletions R/plot_central.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#' Defaults to \code{FALSE}.}
#' \item{\code{show_se}:}{Logical, should standard errors for the fitted line be shown on a scatter plot when \code{show_smooth=TRUE}?
#' Defaults to \code{TRUE}.}
#' \item{\code{show_boxplot}:}{Logical, should a box plot be shown? Defaults to \code{FALSE}.}
#' }
#'
#' @section Miscellaneous fields: Addititional fields can be added to the
Expand Down Expand Up @@ -63,15 +64,15 @@ 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,
# show_points = TRUE,
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, show_boxplot = FALSE)
# Internal ggplot-creating function to plot anything that involves points.
# Creates either a scatter plot, (horizontal) violin plots, or a rectangle plot.
{
Expand Down Expand Up @@ -102,6 +103,28 @@ NULL
c(viol_args, list(colour = "gray60", alpha = 0.2, scale = "width", width = 0.8))
)
}
# Adding box plot
if (show_boxplot) {
if (is.null(fill_by)) {
box_args <- list(fill="grey90")
} else {
box_args <- list(mapping=aes(fill=.data[[fill_by]]))
}
# If violin plot is plotted, make the width of box plot smaller to
# improve readability.
if (show_violin) {
box_args[["width"]] <- 0.25
}
box_args <- c(box_args, list(colour = "black", alpha = 0.2))
# If user wants that jitter plot is not added, add outliers.
# Otherwise remove outliers since then they would be plotted twice;
# once as outlier and once as part of jitter plot.
if (!is.na(point_shape)) {
box_args[["outlier.shape"]] <- NA
}
plot_out <- plot_out +
do.call(geom_boxplot, box_args)
}

# Adding median, if requested.
if (show_median) {
Expand Down
4 changes: 4 additions & 0 deletions man/plotExpression.Rd

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

1 change: 1 addition & 0 deletions man/scater-plot-args.Rd

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

0 comments on commit 096e367

Please sign in to comment.