Skip to content

Commit

Permalink
rasterises plots
Browse files Browse the repository at this point in the history
  • Loading branch information
joseah committed Feb 11, 2023
1 parent 4c795d7 commit ac8c2e9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ URL: https://github.com/powellgenomicslab/Nebulosa
BugReports: https://github.com/powellgenomicslab/Nebulosa/issues
License: GPL-3
Encoding: UTF-8
LazyData: true
Depends:
R (>= 4.0),
ggplot2,
Expand All @@ -25,12 +24,12 @@ Imports:
Seurat,
SingleCellExperiment,
SummarizedExperiment,
SeuratObject,
ks,
Matrix,
stats,
methods
RoxygenNote: 7.1.1
methods,
ggrastr
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Suggests:
testthat, BiocStyle, knitr, rmarkdown, covr, scater, scran, DropletUtils,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ importFrom(ggplot2,scale_color_viridis_c)
importFrom(ggplot2,theme)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(ggrastr,rasterise)
importFrom(ks,hpi)
importFrom(ks,kde)
importFrom(methods,is)
Expand Down
6 changes: 4 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@

#' @importFrom patchwork wrap_plots
.plot_final_density <- function(vars, cell_embeddings, features, joint, method,
adjust, shape, size, pal, combine, ...) {
adjust, shape, size, pal, combine, raster, ...) {
dim_names <- colnames(cell_embeddings)
if (ncol(vars) > 1) {
res <- apply(vars, 2, calculate_density,
cell_embeddings, method, adjust)
p <- mapply(plot_density_, as.list(as.data.frame(res)), colnames(res),
MoreArgs = list(cell_embeddings, dim_names, shape, size,
"Density", pal = pal, ...),
"Density", pal = pal, raster, ...),
SIMPLIFY = FALSE)

if(joint){
Expand All @@ -58,6 +58,7 @@
size,
"Joint density",
pal = pal,
raster,
...
)

Expand Down Expand Up @@ -86,6 +87,7 @@
size,
"Density",
pal = pal,
raster,
...
)
}
Expand Down
9 changes: 6 additions & 3 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#' @param combine Create a single plot? If \code{FALSE}, a list with ggplot
#' objects is returned
#' @param pal String specifying the viridis color palette to use.
#' @param raster Rasterise plot
#' @param ... Further scale arguments passed to scale_color_viridis_c
#' Options:
#' \itemize{
Expand All @@ -48,6 +49,7 @@ setGeneric("plot_density", function(object, features, slot = NULL,
method = c("ks", "wkde"),
adjust = 1, size = 1, shape = 16,
combine = TRUE, pal = "viridis",
raster = TRUE,
...)
standardGeneric("plot_density"))

Expand All @@ -59,7 +61,7 @@ setMethod("plot_density", signature("Seurat"),
features, slot = NULL, joint = FALSE, reduction = NULL,
dims = c(1, 2), method = c("ks", "wkde"), adjust = 1,
size = 1, shape = 16, combine = TRUE, pal = "viridis",
...) {
raster = TRUE, ...) {

# Validate dimensions -----
.validate_dimensions(dims)
Expand Down Expand Up @@ -102,7 +104,7 @@ setMethod("plot_density", signature("Seurat"),
}
.plot_final_density(vars, cell_embeddings, features, joint,
method, adjust, shape, size, pal, combine,
...)
raster, ...)
})


Expand All @@ -117,7 +119,7 @@ setMethod("plot_density", signature("SingleCellExperiment"),
features, slot = NULL, joint = FALSE, reduction = NULL,
dims = c(1, 2), method = c("ks", "wkde"), adjust = 1,
size = 1, shape = 16, combine = TRUE, pal = "viridis",
...) {
raster = TRUE, ...) {

# Validate dimensions -----
.validate_dimensions(dims)
Expand Down Expand Up @@ -167,5 +169,6 @@ setMethod("plot_density", signature("SingleCellExperiment"),
}
.plot_final_density(vars, cell_embeddings, features, joint,
method, adjust, shape, size, pal, combine,
raster,
...)
})
14 changes: 12 additions & 2 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
#' @param size Geom size
#' @param legend_title String used as legend title
#' @param pal String specifying the viridis color palette to use
#' @param raster Rasterise plot
#' @param ... Further scale arguments passed to scale_color_viridis_c
#' @return A ggplot object
#' @importFrom ggplot2 ggplot aes_string geom_point xlab ylab ggtitle labs
#' guide_legend theme element_text element_line element_rect element_blank
#' scale_color_viridis_c scale_color_gradientn
#' @importFrom ggrastr rasterise
plot_density_ <- function(z, feature, cell_embeddings, dim_names, shape, size,
legend_title,
pal = c(
"viridis", "magma", "cividis",
"inferno", "plasma"
), ...) {
),
raster,
...) {
p <- ggplot(data.frame(cell_embeddings, feature = z)) +
aes_string(dim_names[1], dim_names[2], color = "feature") +
geom_point(shape = shape, size = size) +
Expand All @@ -36,6 +40,12 @@ plot_density_ <- function(z, feature, cell_embeddings, dim_names, shape, size,
)

pal <- match.arg(pal)
p + scale_color_viridis_c(option = pal, ...)
p <- p + scale_color_viridis_c(option = pal, ...)

if(raster){
rasterise(p, dpi = 300)
}else{
p
}

}
11 changes: 8 additions & 3 deletions man/plot_density.Rd

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

3 changes: 3 additions & 0 deletions man/plot_density_.Rd

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

0 comments on commit ac8c2e9

Please sign in to comment.