From 4ccdede8f281406c21f067cd898d316edb0e965f Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Tue, 7 Nov 2023 16:02:03 -0500 Subject: [PATCH 1/7] add schex to suggest --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 534687a..0db1a91 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,5 +54,6 @@ Suggests: scran, scater, scuttle, - Seurat + Seurat, + schex VignetteBuilder: knitr From b913877d5a40408493c4e15036fbbb0394f0a678 Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 14:38:02 -0500 Subject: [PATCH 2/7] add wrapper function to create binned plot as alterative implmentation for schex. --- R/add_fill_bin.R | 24 ++++++++++++++++++++++++ R/add_group_bin.R | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 R/add_fill_bin.R create mode 100644 R/add_group_bin.R diff --git a/R/add_fill_bin.R b/R/add_fill_bin.R new file mode 100644 index 0000000..27d6d34 --- /dev/null +++ b/R/add_fill_bin.R @@ -0,0 +1,24 @@ +#' @param bins numeric vector giving number of bins in both vertical and +#' horizontal directions. Set to 30 by default. +#' +#' @rdname add_fill +#' @export +#' @importFrom ggplot2 stat_summary_hex scale_fill_gradient after_stat +add_fill_bin <- function( + p, + var, + bins = 30, + point_size = 2.8, + ...){ + p + + stat_summary_hex( + aes(z = !!sym(var), fill = after_stat(value)), + color = "transparent", + geom = "point", + shape = 21, + size = 2.8, + fun = sum, + bins = bins + ) + + scale_fill_gradient(name = var) +} diff --git a/R/add_group_bin.R b/R/add_group_bin.R new file mode 100644 index 0000000..63fb3d1 --- /dev/null +++ b/R/add_group_bin.R @@ -0,0 +1,35 @@ +#' @param bins numeric vector giving number of bins in both vertical and +#' horizontal directions. Set to 30 by default. +#' @param ... +#' +#' @rdname add_ground +#' @export +#' @importFrom ggplot2 scale_color_discrete +add_ground_bin <- function( + p, + var, + bins = 30, + stroke = 1, + point_size = 3 + ,...){ + p + + stat_summary_hex( + aes( + z = !!sym(var), + group = -1, + color = after_stat(value), + fill = after_stat(rep(NA_integer_, length(value))) + ), + geom = "point", + shape = 21, + size = point_size, + stroke = stroke, + # Majority Voting + fun = function(x){ + ll <- data.frame(table(x)) + as.character(ll[which.max(ll$Freq),"x"]) + }, + bins = bins + ) + + scale_color_discrete(name = var) +} From 5ebf66172b63425dd0982145f9cef90ea743da5e Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 14:38:39 -0500 Subject: [PATCH 3/7] Remove suggestion to schex --- DESCRIPTION | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0db1a91..534687a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,6 +54,5 @@ Suggests: scran, scater, scuttle, - Seurat, - schex + Seurat VignetteBuilder: knitr From 5e622e944c090ee0407655bb7b2ca4d6d683da47 Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 14:39:13 -0500 Subject: [PATCH 4/7] add import for newly added add_fill/ground_bin function --- NAMESPACE | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index c763bc2..930e59e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,9 @@ S3method(make_escheR,SingleCellExperiment) S3method(make_escheR,SpatialExperiment) S3method(make_escheR,data.frame) export(add_fill) +export(add_fill_bin) export(add_ground) +export(add_ground_bin) export(add_symbol) export(make_escheR) importFrom(SingleCellExperiment,reducedDim) @@ -14,14 +16,18 @@ importFrom(SpatialExperiment,scaleFactors) importFrom(SpatialExperiment,spatialCoords) importFrom(SummarizedExperiment,colData) importFrom(ggplot2,aes) +importFrom(ggplot2,after_stat) importFrom(ggplot2,element_blank) importFrom(ggplot2,element_text) importFrom(ggplot2,geom_point) importFrom(ggplot2,ggplot) +importFrom(ggplot2,scale_color_discrete) +importFrom(ggplot2,scale_fill_gradient) importFrom(ggplot2,scale_fill_viridis_c) importFrom(ggplot2,scale_fill_viridis_d) importFrom(ggplot2,scale_shape) importFrom(ggplot2,scale_y_reverse) +importFrom(ggplot2,stat_summary_hex) importFrom(ggplot2,theme) importFrom(ggplot2,theme_bw) importFrom(ggplot2,theme_set) From afb6b2ac05223067ba31b924b4b69e10eb1d77d1 Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 15:03:18 -0500 Subject: [PATCH 5/7] fix documentation and var importing --- NAMESPACE | 2 ++ R/add_fill_bin.R | 2 ++ R/add_group_bin.R | 2 +- R/make_escheR.R | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 930e59e..4fd59de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,7 @@ importFrom(SpatialExperiment,spatialCoords) importFrom(SummarizedExperiment,colData) importFrom(ggplot2,aes) importFrom(ggplot2,after_stat) +importFrom(ggplot2,coord_fixed) importFrom(ggplot2,element_blank) importFrom(ggplot2,element_text) importFrom(ggplot2,geom_point) @@ -31,6 +32,7 @@ importFrom(ggplot2,stat_summary_hex) importFrom(ggplot2,theme) importFrom(ggplot2,theme_bw) importFrom(ggplot2,theme_set) +importFrom(ggplot2,theme_void) importFrom(ggplot2,unit) importFrom(ggplot2,xlab) importFrom(ggplot2,ylab) diff --git a/R/add_fill_bin.R b/R/add_fill_bin.R index 27d6d34..1430b66 100644 --- a/R/add_fill_bin.R +++ b/R/add_fill_bin.R @@ -22,3 +22,5 @@ add_fill_bin <- function( ) + scale_fill_gradient(name = var) } + +utils::globalVariables("value") diff --git a/R/add_group_bin.R b/R/add_group_bin.R index 63fb3d1..308ae01 100644 --- a/R/add_group_bin.R +++ b/R/add_group_bin.R @@ -1,6 +1,6 @@ #' @param bins numeric vector giving number of bins in both vertical and #' horizontal directions. Set to 30 by default. -#' @param ... +#' @param ... Reserved for future arguments. #' #' @rdname add_ground #' @export diff --git a/R/make_escheR.R b/R/make_escheR.R index bec8581..0941269 100644 --- a/R/make_escheR.R +++ b/R/make_escheR.R @@ -140,7 +140,8 @@ make_escheR.SingleCellExperiment <- function( #' @rdname make_escheR #' @importFrom rlang .data #' @importFrom ggplot2 aes element_blank element_text geom_point ggplot -#' @importFrom ggplot2 scale_shape theme theme_bw theme_set unit xlab ylab scale_y_reverse +#' @importFrom ggplot2 scale_shape theme theme_bw theme_set unit xlab ylab +#' @importFrom ggplot2 scale_y_reverse coord_fixed theme_void #' @importFrom SpatialExperiment imgRaster spatialCoords scaleFactors #' @export #' From 031d15e042245a9a508bb785fc30e382141abb5d Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 15:04:03 -0500 Subject: [PATCH 6/7] update documentation. --- man/add_fill.Rd | 8 +++++++- man/add_ground.Rd | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/man/add_fill.Rd b/man/add_fill.Rd index a0b0ced..167aedb 100644 --- a/man/add_fill.Rd +++ b/man/add_fill.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/add_fill.R +% Please edit documentation in R/add_fill.R, R/add_fill_bin.R \name{add_fill} \alias{add_fill} +\alias{add_fill_bin} \title{Adding fill to highlight the figure in the spatial map} \usage{ add_fill(p, var, point_size = 2, ...) + +add_fill_bin(p, var, bins = 30, point_size = 2.8, ...) } \arguments{ \item{p}{a spatial map created by \code{make_escheR()}, with or without @@ -17,6 +20,9 @@ has the values to be used as the background.} ggplot. Defaults to 2.} \item{...}{Reserved for future arguments.} + +\item{bins}{numeric vector giving number of bins in both vertical and +horizontal directions. Set to 30 by default.} } \value{ an ggplot object. diff --git a/man/add_ground.Rd b/man/add_ground.Rd index 503587b..c076432 100644 --- a/man/add_ground.Rd +++ b/man/add_ground.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/add_ground.R +% Please edit documentation in R/add_ground.R, R/add_group_bin.R \name{add_ground} \alias{add_ground} +\alias{add_ground_bin} \title{Adding border to highlight the ground in the spatial map} \usage{ add_ground(p, var, stroke = 0.5, point_size = 2, ...) + +add_ground_bin(p, var, bins = 30, stroke = 1, point_size = 3, ...) } \arguments{ \item{p}{a spatial map created by \code{make_escheR()}, with or without other layers of aesthetics.} @@ -16,6 +19,9 @@ add_ground(p, var, stroke = 0.5, point_size = 2, ...) \item{point_size}{A numeric(1) specifying the size of the spot in the ggplot. Defaults to 2.} \item{...}{Reserved for future arguments.} + +\item{bins}{numeric vector giving number of bins in both vertical and +horizontal directions. Set to 30 by default.} } \value{ an ggplot object. From 8ece4fc4c6387f452663fda1582c05cfaef84d9d Mon Sep 17 00:00:00 2001 From: Boyi Guo Date: Wed, 8 Nov 2023 15:10:04 -0500 Subject: [PATCH 7/7] add binning example. --- vignettes/more_than_visium.Rmd | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/vignettes/more_than_visium.Rmd b/vignettes/more_than_visium.Rmd index 56d4822..598874e 100644 --- a/vignettes/more_than_visium.Rmd +++ b/vignettes/more_than_visium.Rmd @@ -33,6 +33,8 @@ library(scran) spe <- Visium_humanDLPFC() |> logNormCounts() +spe <- spe[, spe$in_tissue == 1] +spe <- spe[, !is.na(spe$ground_truth)] top.gene <- getTopHVGs(spe, n=500) set.seed(100) # See below. @@ -46,6 +48,74 @@ make_escheR( theme_minimal() ``` +# Hex Binning +```{r} +spe$counts_MOBP <- counts(spe)[which(rowData(spe)$gene_name=="MOBP"),] +spe$ground_truth <- factor(spe$ground_truth) +# Hex Binning version +# make_escheR( +# spe, +# dimred = "PCA" +# ) + +# # Create hex binning +# stat_summary_hex( +# aes(z = ground_truth, group = -1, color = after_stat(value), fill = after_stat(rep(NA_integer_, length(value))) +# ), +# linewidth = 1.2, +# fun = function(x){ +# # browser() +# ll <- data.frame(table(x)) +# as.character(ll[which.max(ll$Freq),"x"]) +# }, bins = 30 +# ) + +# # Create hex binning +# stat_summary_hex( +# aes(z = counts_MOBP, fill = after_stat(value)), +# color = "transparent", +# fun = sum, bins = 30) + +# scale_size_area + + +# Point Binning version +make_escheR( + spe, + dimred = "PCA" +) |> + add_ground_bin( + var = "ground_truth") |> + add_fill_bin( + var = "counts_MOBP" + ) + + scale_fill_gradient(low = "white", high = "black", name = "MOBP Count")+ + scale_color_discrete(name = "Spatial Domains") + + theme_minimal() + +# +# stat_summary_hex( +# aes(z = ground_truth, group = -1, color = after_stat(value), fill = after_stat(rep(NA_integer_, length(value))) +# ), +# geom = "point", +# shape = 21, +# size = 3, +# stroke = 1, +# # linewidth = 1.2, +# fun = function(x){ +# # browser() +# ll <- data.frame(table(x)) +# as.character(ll[which.max(ll$Freq),"x"]) +# }, bins = 30 +# ) + +# stat_summary_hex( +# aes(z = counts_MOBP, fill = after_stat(value)), +# color = "transparent", +# geom = "point", +# shape = 21, +# size = 2.8, +# fun = sum, bins = 30) +``` +> Note 1: The strategy of binning to avoid overplotting is previously proposed in [`schex`](https://www.bioconductor.org/packages/release/bioc/html/schex.html). While we provide an impelmentation in `escheR`, we would caution our users that the binning strategy could lead to intermixing of cluster memberships. In our impelmentation, the majority membership of the datapoints belonging to a bin is selected as the label. Users should use the binning strategy under their own discretion, and interpret the visualization carefully. +> Note 2: `add_fill_bin()` shoudl be applied after `add_ground_bin()` for the better visualization outcome. + # Image-based `SpatialExperiment` Object To demonstrate the principle that `escheR` can be used to visualize image-based spatially-resolved data pending optimization, we include two image-based spatially resolved transcriptomics data generated via seqFish platform and Slide-seq V2 platform respectively. The two datasets have been previously curated in the [`STexampleData`](https://bioconductor.org/packages/release/data/experiment/vignettes/STexampleData/inst/doc/STexampleData_overview.html) package