Skip to content

Commit

Permalink
Add min and max value (#197) (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan authored Feb 11, 2025
1 parent 3fc1ffa commit 94f0be6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Authors@R: c(
person("Leo", "Lahti", role=c("ctb"), email="[email protected]", comment = c(ORCID = "0000-0001-5537-637X")),
person("Tuomas", "Borman", role = c("ctb"), comment = c(ORCID = "0000-0002-8563-8884"))
)
Version: 1.35.1
Date: 2025-01-31
Version: 1.35.2
Date: 2025-02-11
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
3 changes: 2 additions & 1 deletion R/plotHighestExprs.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ plotHighestExprs <- function(object, n = 50, colour_cells_by = color_cells_by,
## Create the plot and annotations.
plot_most_expressed <- ggplot(df_exprs_by_cell_long, aes_to_use) + geom_point(alpha = 0.6, shape = 124)
plot_most_expressed <- plot_most_expressed + labs(x=assay.type, y="Feature") + theme_bw(8) +
theme(legend.position = c(1, 0), legend.justification = c(1, 0),
theme(legend.position.inside = c(1, 0),
legend.justification = c(1, 0),
axis.text.x = element_text(colour = "gray35"),
axis.text.y = element_text(colour = "gray35"),
axis.title.x = element_text(colour = "gray35"),
Expand Down
38 changes: 37 additions & 1 deletion R/plotReducedDim.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#' \code{options(ggrastr.default.dpi)},
#' for example \code{options(ggrastr.default.dpi=300)}.
#' @param by_exprs_values Alias for \code{by.assay.type}.
#' @param min.value,max.value Minimum and maximum values, beyond which \code{colour_by} values (if numeric) are truncated. Can be set to a numeric value to prevent outlying values from skewing the colour scale, or set to quantiles of the \code{colour_by} variable by setting to (e.g.) \code{"q10"} for the 10th quantile.
#' @param ... Additional arguments for visualization, see
#' \code{?"\link{scater-plot-args}"} for details.
#'
Expand Down Expand Up @@ -124,7 +125,9 @@ plotReducedDim <- function(
swap_rownames = NULL, point.padding = NA, force = 1,
rasterise = FALSE, scattermore = FALSE,
bins = NULL, summary_fun = "sum", hex = FALSE,
by.assay.type=by_exprs_values, ...
by.assay.type=by_exprs_values,
min.value=NULL, max.value=NULL,
...
) {

## Extract reduced dimension representation of cells
Expand Down Expand Up @@ -162,6 +165,11 @@ plotReducedDim <- function(
colour_by <- vis_out$colour_by
shape_by <- vis_out$shape_by
size_by <- vis_out$size_by

if (is.numeric(df_to_plot$colour_by)) {
df_to_plot$colour_by <- .truncate_values(df_to_plot$colour_by, min.value, max.value)

}

## Dispatching to the central plotter in the simple case of two dimensions.
if (length(to_plot) == 2L) {
Expand Down Expand Up @@ -298,3 +306,31 @@ paired_reddim_plot <- function(df_to_plot, to_plot, dimred, percentVar = NULL,
}
plot_out
}



.truncate_values <- function(values, min.value=NULL, max.value=NULL) {
if (!is.null(min.value)) {
min.value <- .handle_truncval(unlist(values), min.value)
values[values < min.value] <- min.value
}
if (!is.null(max.value)) {
max.value <- .handle_truncval(unlist(values), max.value)
values[values > max.value] <- max.value
}
values
}

.handle_truncval <- function(col, truncval) {
if (is.character(truncval) || is.factor(truncval)) {
if (!is.na(as.character(as.numeric(truncval)))) {
return (as.character(as.numeric(truncval)))
}
stopifnot(grepl("q\\d+", truncval))
return (quantile(col, as.numeric(sub("q", "", as.character(truncval))) / 100))
}
if (is.numeric(truncval)) {
# do nothing?
}
truncval
}
9 changes: 8 additions & 1 deletion inst/NEWS.Rd
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
\name{NEWS}
\title{News for Package \pkg{scater}}

\section{Changes in version 1.28.0, Bioconductor 3.17 Release}{
\section{Changes in version 1.36.0, Bioconductor 3.21 Release}{
\itemize{
\item Add \code{min.value,max.value} arguments to \code{plotReducedDim}
to enable truncating colour scales using a numeric value or a quantile (eg \code{"q10"}).
}
}

\section{Changes in version 1.28.0, Bioconductor 3.18 Release}{
\itemize{
\item Change \code{exprs_values} (and similar) to \code{assay.type}.
\item Tweak colouring of violin plots.
Expand Down
4 changes: 4 additions & 0 deletions man/plotReducedDim.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-plot-dimred.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ test_that("we can produce PCA scatterplots", {
expect_ggplot(plotPCA(example_sce, bins = 10, colour_by = "Gene_0001"))
expect_ggplot(plotPCA(example_sce, bins = 10, colour_by = "Gene_0001",
hex = TRUE))

# truncating color scale
expect_ggplot(plotPCA(example_sce, min.value = 1))
expect_ggplot(plotPCA(example_sce, min.value = "1"))
expect_ggplot(plotPCA(example_sce, min.value = factor(1)))
expect_ggplot(plotPCA(example_sce, min.value = factor("q1")))
expect_ggplot(plotPCA(example_sce, min.value = "q1"))

expect_ggplot(plotPCA(example_sce, max.value = 2))
expect_ggplot(plotPCA(example_sce, max.value = "2"))
expect_ggplot(plotPCA(example_sce, max.value = factor(2)))
expect_ggplot(plotPCA(example_sce, max.value = factor("q90")))
expect_ggplot(plotPCA(example_sce, max.value = "q90"))
})

test_that("we can produce PCA pairplots", {
Expand Down

0 comments on commit 94f0be6

Please sign in to comment.