From 089460c6716faa3443747e209770e8fdb2de4c9f Mon Sep 17 00:00:00 2001 From: jiaxingli Date: Sun, 1 Sep 2024 23:34:03 +0800 Subject: [PATCH 1/4] Add plot-lisa-feature.R --- R/plot-lisa-feature.R | 165 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 R/plot-lisa-feature.R diff --git a/R/plot-lisa-feature.R b/R/plot-lisa-feature.R new file mode 100644 index 0000000..71e2c4f --- /dev/null +++ b/R/plot-lisa-feature.R @@ -0,0 +1,165 @@ +##' @title plot_lisa_feature +##' @rdname plot-lisa-feature +##' @param spe SpatialExperiment object. +##' @param feature selected features to be visualized. +##' @param lisa.res the result returned by \code{SVP::runLISA()}. +##' @param assay.type the assay name where data will be used from +##' (e.g., 'data', 'counts'), default is \code{'logcounts'}. +##' @param geom the function of geometric layer, default is \code{geom_bgpoint}, +##' other option is \code{sc_geom_point}. +##' @param pointsize numeric the size of point, default is \code{10}. +##' @param hlpointsize numeric the size of point which contains corresbonding +##' spatially variable gene(i.e., SVG), default is \code{8}. +##' @param clustertype cell type which is from the result of \code{lisa.res}, +##' default is \code{'High'}. +##' @param hlcolor the color of circular line which enfolds the point +##' that contains SVG, default is \code{'black'}. +##' @param gap_line_width numeric the line width of gap between the background and +##' top point point layer, default is \code{.1}. +##' @param bg_line_width numeric the line width of background point layer, +##' default is \code{0.3}. +##' @param facet_name the name of facet used in \code{ggh4x::facet_nested_wrap()}, +##' default is \code{NULL}. +##' @param type the type of the name of gene's prefix that will be substituted +##' with \code{""}, default is \code{'TFT'}, other options are \code{'GO.BP'} and \code{'Reactome'}. +##' @param reduction reduction method, default is \code{NULL} and will +##' use the default setting store in the object +##' @param ... additional parameters pass to \code{scattermore::geom_scattermore()} +##' \itemize{ +##' \item \code{bg_colour} the colour of background point, default is \code{NA}. +##' this character also can be set in \code{mappint}. +##' \item \code{gap_colour} the colour of gap background, default is \code{'white'}. +##' \item \code{bg_line_width} the line width of background point, +##' default is \code{.3}. +##' \item \code{gap_line_width} the gap line width of background point, +##' default is \code{.1}. +##' \item \code{alpha} the transparency of colour, default is 1. +##' \item \code{subset} subset the data frame which meet conditions to display. +##' this should be set in \code{mapping}. +##' } +##' @return ggplot object +##' @importFrom ggplot theme element_rect +##' @importFrom ggh4x facet_nested_wrap +##' @importFrom magrittr %>% +##' @export +##' @examples +##' \dontrun{ +##' library(ggplot2) +##' library(ggh4x) +##' library(SingleCellExperiment) |> suppressPackageStartupMessages() +##' library(SpatialExperiment) |> suppressPackageStartupMessages() +##' spe <- readRDS("./Visium_humanDLPFC.spe.rds") +##' spe <-scater::logNormCounts(spe) +##' genes <- c('MOBP', 'PCP4', 'SNAP25', 'HBB', 'IGKC', 'NPY') +##' target.features <- rownames(spe)[match(genes, rowData(spe)$gene_name)] +##' lisa.res1 <- runLISA(spe, +##' assay.type='logcounts', +##' features=target.features[seq(2)], +##' weight.method='knn', +##' k=50) +##' plot_lisa_feature(spe, lisa.res=lisa.res1, feature=target.features[seq(2)], +##' pointsize=2, hlpointsize=2, gap_line_width=.1) +##' } +plot_lisa_feature <- function(spe, + feature, + lisa.res, + assay.type = 'logcounts', + geom = geom_bgpoint, + pointsize = 10, + hlpointsize = 8, + clustertype = 'High', + hlcolor = c('black'), + gap_line_width = .1, + bg_line_width = .3, + facet_name = NULL, + type = 'TFT', + reduction = NULL, + ... + ){ + prefix.gsub <- switch(type, TFT="_TARGET_GENES|_UNKNOWN", GO.BP="GOBP_", Reactome="REACTOME_") + rownames(spe) <- gsub(prefix.gsub, "", rownames(spe)) + feature <- gsub(prefix.gsub, "", feature) + if (is.null(reduction)){ + cnm <- spatialCoordsNames(spe) + p <- sc_spatial(spe, + feature, + mapping = aes(x=!!rlang::sym(cnm[1]), y = !!rlang::sym(cnm[2])), + pointsize = pointsize, + slot = assay.type, + gap_colour = NA, + image.plot = FALSE, + geom = geom, + ... + ) + }else{ + p <- sc_feature( + spe, + features = feature, + reduction = reduction, + geom = geom, + pointsize = pointsize, + slot = assay.type, + ... + ) + } + if (missing(lisa.res) || is.null(lisa.res)){ + message("The lisa result is not provided") + return(p) + } + + if (inherits(lisa.res, 'SimpleList') || inherits(lisa.res, "list")){ + names(lisa.res) <- gsub(prefix.gsub, "", names(lisa.res)) + lisa.res <- lisa.res |> + lapply(function(x)x|>tibble::rownames_to_column(var='.BarcodeID')) |> + dplyr::bind_rows(.id='features') + } + if (inherits(p, 'patchwork')){ + `%add+%` <- `&` + }else{ + `%add+%` <- `+` + } + p1 <- p %add+% sc_geom_annot( + data = lisa.res, + mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% clustertype), + pointsize = hlpointsize, + gap_line_width = gap_line_width, + bg_line_width = bg_line_width + ) %add+% + scale_bg_colour_manual( + values = hlcolor, + guide = guide_legend( + theme = theme( + legend.title = element_text(size = 8), + legend.text = element_text(size = 6), + legend.key.width = grid::unit(.3, "cm"), + legend.key.height = grid::unit(.3, "cm") + ), + order = 1 + ) + ) %add+% + guides(colour = guide_colorbar( + theme = theme( + legend.title = element_text(size=8), + legend.text = element_text(size=6), + legend.key.width = grid::unit(.4, "cm"), + legend.key.height = grid::unit(1.5, "cm") + ) + ) + ) %add+% + theme( + strip.background.x=element_rect(color="white") + ) + spe$sample_id |> unique() |> length() -> len + if (len > 1 && !is.null(facet_name)){ + if (length(facet_name) > 1){ + tmpf <- paste0(facet_name, collapse="~") |> as.formula() + }else{ + tmpf <- as.formula("~sample_id") + } + p1 <- p1 %add+% + ggh4x::facet_nested_wrap(tmpf) %add+% + theme(strip.background.x=element_rect(color="white")) + } + return(p1) +} + From d3bb584230eef687d6fa796c439ca8ce4ccf6807 Mon Sep 17 00:00:00 2001 From: jiaxingli Date: Mon, 2 Sep 2024 23:20:10 +0800 Subject: [PATCH 2/4] modify plot-lisa-feature.R --- DESCRIPTION | 1 + NAMESPACE | 3 ++ R/plot-lisa-feature.R | 9 ++-- man/plot-lisa-feature.Rd | 102 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 man/plot-lisa-feature.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 379f4f5..92ccfb8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,6 +63,7 @@ Suggests: SpatialExperiment, STexampleData, testthat (>= 3.0.0), + ggh4x, MASS BugReports: https://github.com/YuLab-SMU/ggsc/issues URL: https://github.com/YuLab-SMU/ggsc (devel), https://yulab-smu.top/ggsc/ (docs) diff --git a/NAMESPACE b/NAMESPACE index c51c490..886f8c8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(aes) export(draw_key_bgpoint) export(geom_bgpoint) export(geom_scattermore2) +export(plot_lisa_feature) export(sc_dim) export(sc_dim_count) export(sc_dim_geom_ellipse) @@ -60,6 +61,7 @@ importFrom(ggplot2,coord_flip) importFrom(ggplot2,discrete_scale) importFrom(ggplot2,draw_key_point) importFrom(ggplot2,element_blank) +importFrom(ggplot2,element_rect) importFrom(ggplot2,element_text) importFrom(ggplot2,facet_grid) importFrom(ggplot2,facet_wrap) @@ -112,6 +114,7 @@ importFrom(scales,alpha) importFrom(scales,pal_identity) importFrom(scattermore,geom_scattermore) importFrom(scattermore,scattermore) +importFrom(stats,as.formula) importFrom(stats,dist) importFrom(stats,hclust) importFrom(stats,mad) diff --git a/R/plot-lisa-feature.R b/R/plot-lisa-feature.R index 71e2c4f..43f9bce 100644 --- a/R/plot-lisa-feature.R +++ b/R/plot-lisa-feature.R @@ -38,9 +38,8 @@ ##' this should be set in \code{mapping}. ##' } ##' @return ggplot object -##' @importFrom ggplot theme element_rect -##' @importFrom ggh4x facet_nested_wrap -##' @importFrom magrittr %>% +##' @importFrom ggplot2 theme element_rect +##' @importFrom stats as.formula ##' @export ##' @examples ##' \dontrun{ @@ -80,7 +79,7 @@ plot_lisa_feature <- function(spe, rownames(spe) <- gsub(prefix.gsub, "", rownames(spe)) feature <- gsub(prefix.gsub, "", feature) if (is.null(reduction)){ - cnm <- spatialCoordsNames(spe) + cnm <- SpatialExperiment::spatialCoordsNames(spe) p <- sc_spatial(spe, feature, mapping = aes(x=!!rlang::sym(cnm[1]), y = !!rlang::sym(cnm[2])), @@ -120,7 +119,7 @@ plot_lisa_feature <- function(spe, } p1 <- p %add+% sc_geom_annot( data = lisa.res, - mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% clustertype), + mapping = aes(bg_colour = lisa.res$cluster.test, subset = lisa.res$cluster.test %in% clustertype), pointsize = hlpointsize, gap_line_width = gap_line_width, bg_line_width = bg_line_width diff --git a/man/plot-lisa-feature.Rd b/man/plot-lisa-feature.Rd new file mode 100644 index 0000000..e762aa3 --- /dev/null +++ b/man/plot-lisa-feature.Rd @@ -0,0 +1,102 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot-lisa-feature.R +\name{plot_lisa_feature} +\alias{plot_lisa_feature} +\title{plot_lisa_feature} +\usage{ +plot_lisa_feature( + spe, + feature, + lisa.res, + assay.type = "logcounts", + geom = geom_bgpoint, + pointsize = 10, + hlpointsize = 8, + clustertype = "High", + hlcolor = c("black"), + gap_line_width = 0.1, + bg_line_width = 0.3, + facet_name = NULL, + type = "TFT", + reduction = NULL, + ... +) +} +\arguments{ +\item{spe}{SpatialExperiment object.} + +\item{feature}{selected features to be visualized.} + +\item{lisa.res}{the result returned by \code{SVP::runLISA()}.} + +\item{assay.type}{the assay name where data will be used from +(e.g., 'data', 'counts'), default is \code{'logcounts'}.} + +\item{geom}{the function of geometric layer, default is \code{geom_bgpoint}, +other option is \code{sc_geom_point}.} + +\item{pointsize}{numeric the size of point, default is \code{10}.} + +\item{hlpointsize}{numeric the size of point which contains corresbonding +spatially variable gene(i.e., SVG), default is \code{8}.} + +\item{clustertype}{cell type which is from the result of \code{lisa.res}, +default is \code{'High'}.} + +\item{hlcolor}{the color of circular line which enfolds the point +that contains SVG, default is \code{'black'}.} + +\item{gap_line_width}{numeric the line width of gap between the background and +top point point layer, default is \code{.1}.} + +\item{bg_line_width}{numeric the line width of background point layer, +default is \code{0.3}.} + +\item{facet_name}{the name of facet used in \code{ggh4x::facet_nested_wrap()}, +default is \code{NULL}.} + +\item{type}{the type of the name of gene's prefix that will be substituted +with \code{""}, default is \code{'TFT'}, other options are \code{'GO.BP'} and \code{'Reactome'}.} + +\item{reduction}{reduction method, default is \code{NULL} and will +use the default setting store in the object} + +\item{...}{additional parameters pass to \code{scattermore::geom_scattermore()} +\itemize{ +\item \code{bg_colour} the colour of background point, default is \code{NA}. +this character also can be set in \code{mappint}. +\item \code{gap_colour} the colour of gap background, default is \code{'white'}. +\item \code{bg_line_width} the line width of background point, +default is \code{.3}. +\item \code{gap_line_width} the gap line width of background point, +default is \code{.1}. +\item \code{alpha} the transparency of colour, default is 1. +\item \code{subset} subset the data frame which meet conditions to display. +this should be set in \code{mapping}. +}} +} +\value{ +ggplot object +} +\description{ +plot_lisa_feature +} +\examples{ +\dontrun{ +library(ggplot2) +library(ggh4x) +library(SingleCellExperiment) |> suppressPackageStartupMessages() +library(SpatialExperiment) |> suppressPackageStartupMessages() +spe <- readRDS("./Visium_humanDLPFC.spe.rds") +spe <-scater::logNormCounts(spe) +genes <- c('MOBP', 'PCP4', 'SNAP25', 'HBB', 'IGKC', 'NPY') +target.features <- rownames(spe)[match(genes, rowData(spe)$gene_name)] +lisa.res1 <- runLISA(spe, + assay.type='logcounts', + features=target.features[seq(2)], + weight.method='knn', + k=50) +plot_lisa_feature(spe, lisa.res=lisa.res1, feature=target.features[seq(2)], + pointsize=2, hlpointsize=2, gap_line_width=.1) +} +} From 58cf42a2c420cbeee17e3532bf08285353c37496 Mon Sep 17 00:00:00 2001 From: xushuangbin Date: Tue, 3 Sep 2024 16:40:10 +0800 Subject: [PATCH 3/4] update plot_lisa_feature --- DESCRIPTION | 2 + NAMESPACE | 1 + R/{plot-lisa-feature.R => plot-methods.R} | 69 +++++++++++++---------- R/sc-geom-annot.R | 2 +- man/plot-lisa-feature.Rd | 51 +++++++++-------- 5 files changed, 71 insertions(+), 54 deletions(-) rename R/{plot-lisa-feature.R => plot-methods.R} (73%) diff --git a/DESCRIPTION b/DESCRIPTION index 92ccfb8..240478b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -81,3 +81,5 @@ LinkingTo: Rcpp, RcppArmadillo, RcppParallel +Remotes: + YuLab-SMU/SVP diff --git a/NAMESPACE b/NAMESPACE index 886f8c8..286194e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ importFrom(ggplot2,ggtitle) importFrom(ggplot2,guide_colorbar) importFrom(ggplot2,guide_legend) importFrom(ggplot2,guides) +importFrom(ggplot2,label_wrap_gen) importFrom(ggplot2,labs) importFrom(ggplot2,layer) importFrom(ggplot2,layer_data) diff --git a/R/plot-lisa-feature.R b/R/plot-methods.R similarity index 73% rename from R/plot-lisa-feature.R rename to R/plot-methods.R index 43f9bce..5fd36bb 100644 --- a/R/plot-lisa-feature.R +++ b/R/plot-methods.R @@ -1,15 +1,15 @@ ##' @title plot_lisa_feature ##' @rdname plot-lisa-feature -##' @param spe SpatialExperiment object. -##' @param feature selected features to be visualized. +##' @param spe SpatialExperiment or SingleCellExperiment object. ##' @param lisa.res the result returned by \code{SVP::runLISA()}. +##' @param features selected features to be visualized, default is NULL. ##' @param assay.type the assay name where data will be used from ##' (e.g., 'data', 'counts'), default is \code{'logcounts'}. ##' @param geom the function of geometric layer, default is \code{geom_bgpoint}, ##' other option is \code{sc_geom_point}. -##' @param pointsize numeric the size of point, default is \code{10}. +##' @param pointsize numeric the size of point, default is \code{2}. ##' @param hlpointsize numeric the size of point which contains corresbonding -##' spatially variable gene(i.e., SVG), default is \code{8}. +##' spatially variable gene(i.e., SVG), default is \code{1.8}. ##' @param clustertype cell type which is from the result of \code{lisa.res}, ##' default is \code{'High'}. ##' @param hlcolor the color of circular line which enfolds the point @@ -20,25 +20,20 @@ ##' default is \code{0.3}. ##' @param facet_name the name of facet used in \code{ggh4x::facet_nested_wrap()}, ##' default is \code{NULL}. -##' @param type the type of the name of gene's prefix that will be substituted -##' with \code{""}, default is \code{'TFT'}, other options are \code{'GO.BP'} and \code{'Reactome'}. ##' @param reduction reduction method, default is \code{NULL} and will ##' use the default setting store in the object +##' @param image.plot logical whether display the image of spatial experiment, default +##' is FALSE. +##' @param label_wrap_width numeric maximum number of characters before wrapping the strip. +##' default is \code{30}. ##' @param ... additional parameters pass to \code{scattermore::geom_scattermore()} ##' \itemize{ ##' \item \code{bg_colour} the colour of background point, default is \code{NA}. ##' this character also can be set in \code{mappint}. -##' \item \code{gap_colour} the colour of gap background, default is \code{'white'}. -##' \item \code{bg_line_width} the line width of background point, -##' default is \code{.3}. -##' \item \code{gap_line_width} the gap line width of background point, -##' default is \code{.1}. ##' \item \code{alpha} the transparency of colour, default is 1. -##' \item \code{subset} subset the data frame which meet conditions to display. -##' this should be set in \code{mapping}. ##' } ##' @return ggplot object -##' @importFrom ggplot2 theme element_rect +##' @importFrom ggplot2 theme element_rect label_wrap_gen ##' @importFrom stats as.formula ##' @export ##' @examples @@ -47,53 +42,68 @@ ##' library(ggh4x) ##' library(SingleCellExperiment) |> suppressPackageStartupMessages() ##' library(SpatialExperiment) |> suppressPackageStartupMessages() -##' spe <- readRDS("./Visium_humanDLPFC.spe.rds") +##' library(STexampleData) +##' # create ExperimentHub instance +##' eh <- ExperimentHub() +##' # query STexampleData datasets +##' myfiles <- query(eh, "STexampleData") +##' ah_id <- myfiles$ah_id[myfiles$title == 'Visium_humanDLPFC'] +##' spe <- myfiles[[ah_id]] +##' spe <- spe[, colData(spe)$in_tissue == 1] ##' spe <-scater::logNormCounts(spe) ##' genes <- c('MOBP', 'PCP4', 'SNAP25', 'HBB', 'IGKC', 'NPY') ##' target.features <- rownames(spe)[match(genes, rowData(spe)$gene_name)] +##' library(SVP) ##' lisa.res1 <- runLISA(spe, ##' assay.type='logcounts', ##' features=target.features[seq(2)], ##' weight.method='knn', -##' k=50) -##' plot_lisa_feature(spe, lisa.res=lisa.res1, feature=target.features[seq(2)], +##' k=50) +##' plot_lisa_feature(spe, lisa.res=lisa.res1, features=target.features[seq(2)], ##' pointsize=2, hlpointsize=2, gap_line_width=.1) ##' } plot_lisa_feature <- function(spe, - feature, lisa.res, + features = NULL, assay.type = 'logcounts', geom = geom_bgpoint, - pointsize = 10, - hlpointsize = 8, + pointsize = 2, + hlpointsize = 1.8, clustertype = 'High', hlcolor = c('black'), gap_line_width = .1, bg_line_width = .3, facet_name = NULL, - type = 'TFT', reduction = NULL, + image.plot = FALSE, + label_wrap_width = 30, ... ){ - prefix.gsub <- switch(type, TFT="_TARGET_GENES|_UNKNOWN", GO.BP="GOBP_", Reactome="REACTOME_") - rownames(spe) <- gsub(prefix.gsub, "", rownames(spe)) - feature <- gsub(prefix.gsub, "", feature) + if (missing(lisa.res) || is.null(lisa.res)){ + if (is.null(features)){ + cli::cli_abort("The {.var features} should not be `NULL`, when {.var lisa.res} is missing or NULL.") + } + }else if(inherits(lisa.res, 'SimpleList') || inherits(lisa.res, "list")){ + names(lisa.res) <- gsub("_", " ", names(lisa.res)) + features <- names(lisa.res) + } + rownames(spe) <- gsub("_", " ", rownames(spe)) if (is.null(reduction)){ cnm <- SpatialExperiment::spatialCoordsNames(spe) p <- sc_spatial(spe, - feature, + features, mapping = aes(x=!!rlang::sym(cnm[1]), y = !!rlang::sym(cnm[2])), pointsize = pointsize, slot = assay.type, gap_colour = NA, - image.plot = FALSE, + image.plot = image.plot, geom = geom, ... ) }else{ p <- sc_feature( spe, - features = feature, + features = features, reduction = reduction, geom = geom, pointsize = pointsize, @@ -107,7 +117,6 @@ plot_lisa_feature <- function(spe, } if (inherits(lisa.res, 'SimpleList') || inherits(lisa.res, "list")){ - names(lisa.res) <- gsub(prefix.gsub, "", names(lisa.res)) lisa.res <- lisa.res |> lapply(function(x)x|>tibble::rownames_to_column(var='.BarcodeID')) |> dplyr::bind_rows(.id='features') @@ -119,7 +128,7 @@ plot_lisa_feature <- function(spe, } p1 <- p %add+% sc_geom_annot( data = lisa.res, - mapping = aes(bg_colour = lisa.res$cluster.test, subset = lisa.res$cluster.test %in% clustertype), + mapping = aes(bg_colour = !!rlang::sym("cluster.test"), subset = !!rlang::sym("cluster.test") %in% clustertype), pointsize = hlpointsize, gap_line_width = gap_line_width, bg_line_width = bg_line_width @@ -156,7 +165,7 @@ plot_lisa_feature <- function(spe, tmpf <- as.formula("~sample_id") } p1 <- p1 %add+% - ggh4x::facet_nested_wrap(tmpf) %add+% + ggh4x::facet_nested_wrap(tmpf, labeller = label_wrap_gen(label_wrap_width)) %add+% theme(strip.background.x=element_rect(color="white")) } return(p1) diff --git a/R/sc-geom-annot.R b/R/sc-geom-annot.R index 527d368..41acaf3 100644 --- a/R/sc-geom-annot.R +++ b/R/sc-geom-annot.R @@ -57,7 +57,7 @@ ggplot_add.sc_geom_annot <- function(object, plot, object_name){ if (geomfun == 'geom_scattermore2'){ geomfun <- "sc_geom_point" }else{ - params$pixels <- NULL + object$pixels <- NULL } ly <- do.call(geomfun, c(object, params)) ggplot_add(ly, plot, object_name) diff --git a/man/plot-lisa-feature.Rd b/man/plot-lisa-feature.Rd index e762aa3..a111fe6 100644 --- a/man/plot-lisa-feature.Rd +++ b/man/plot-lisa-feature.Rd @@ -1,44 +1,45 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plot-lisa-feature.R +% Please edit documentation in R/plot-methods.R \name{plot_lisa_feature} \alias{plot_lisa_feature} \title{plot_lisa_feature} \usage{ plot_lisa_feature( spe, - feature, lisa.res, + features = NULL, assay.type = "logcounts", geom = geom_bgpoint, - pointsize = 10, - hlpointsize = 8, + pointsize = 2, + hlpointsize = 1.8, clustertype = "High", hlcolor = c("black"), gap_line_width = 0.1, bg_line_width = 0.3, facet_name = NULL, - type = "TFT", reduction = NULL, + image.plot = FALSE, + label_wrap_width = 30, ... ) } \arguments{ -\item{spe}{SpatialExperiment object.} - -\item{feature}{selected features to be visualized.} +\item{spe}{SpatialExperiment or SingleCellExperiment object.} \item{lisa.res}{the result returned by \code{SVP::runLISA()}.} +\item{features}{selected features to be visualized, default is NULL.} + \item{assay.type}{the assay name where data will be used from (e.g., 'data', 'counts'), default is \code{'logcounts'}.} \item{geom}{the function of geometric layer, default is \code{geom_bgpoint}, other option is \code{sc_geom_point}.} -\item{pointsize}{numeric the size of point, default is \code{10}.} +\item{pointsize}{numeric the size of point, default is \code{2}.} \item{hlpointsize}{numeric the size of point which contains corresbonding -spatially variable gene(i.e., SVG), default is \code{8}.} +spatially variable gene(i.e., SVG), default is \code{1.8}.} \item{clustertype}{cell type which is from the result of \code{lisa.res}, default is \code{'High'}.} @@ -55,24 +56,20 @@ default is \code{0.3}.} \item{facet_name}{the name of facet used in \code{ggh4x::facet_nested_wrap()}, default is \code{NULL}.} -\item{type}{the type of the name of gene's prefix that will be substituted -with \code{""}, default is \code{'TFT'}, other options are \code{'GO.BP'} and \code{'Reactome'}.} - \item{reduction}{reduction method, default is \code{NULL} and will use the default setting store in the object} +\item{image.plot}{logical whether display the image of spatial experiment, default +is FALSE.} + +\item{label_wrap_width}{numeric maximum number of characters before wrapping the strip. +default is \code{30}.} + \item{...}{additional parameters pass to \code{scattermore::geom_scattermore()} \itemize{ \item \code{bg_colour} the colour of background point, default is \code{NA}. this character also can be set in \code{mappint}. -\item \code{gap_colour} the colour of gap background, default is \code{'white'}. -\item \code{bg_line_width} the line width of background point, -default is \code{.3}. -\item \code{gap_line_width} the gap line width of background point, -default is \code{.1}. \item \code{alpha} the transparency of colour, default is 1. -\item \code{subset} subset the data frame which meet conditions to display. -this should be set in \code{mapping}. }} } \value{ @@ -87,16 +84,24 @@ library(ggplot2) library(ggh4x) library(SingleCellExperiment) |> suppressPackageStartupMessages() library(SpatialExperiment) |> suppressPackageStartupMessages() -spe <- readRDS("./Visium_humanDLPFC.spe.rds") +library(STexampleData) +# create ExperimentHub instance +eh <- ExperimentHub() +# query STexampleData datasets +myfiles <- query(eh, "STexampleData") +ah_id <- myfiles$ah_id[myfiles$title == 'Visium_humanDLPFC'] +spe <- myfiles[[ah_id]] +spe <- spe[, colData(spe)$in_tissue == 1] spe <-scater::logNormCounts(spe) genes <- c('MOBP', 'PCP4', 'SNAP25', 'HBB', 'IGKC', 'NPY') target.features <- rownames(spe)[match(genes, rowData(spe)$gene_name)] +library(SVP) lisa.res1 <- runLISA(spe, assay.type='logcounts', features=target.features[seq(2)], weight.method='knn', - k=50) -plot_lisa_feature(spe, lisa.res=lisa.res1, feature=target.features[seq(2)], + k=50) +plot_lisa_feature(spe, lisa.res=lisa.res1, features=target.features[seq(2)], pointsize=2, hlpointsize=2, gap_line_width=.1) } } From 1aec045be94f15a6ca6688d1fb7754231c66f90c Mon Sep 17 00:00:00 2001 From: xushuangbin Date: Wed, 4 Sep 2024 14:58:06 +0800 Subject: [PATCH 4/4] remove ggh4x --- DESCRIPTION | 1 - R/plot-methods.R | 5 ++--- man/plot-lisa-feature.Rd | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 240478b..8c0659b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,7 +63,6 @@ Suggests: SpatialExperiment, STexampleData, testthat (>= 3.0.0), - ggh4x, MASS BugReports: https://github.com/YuLab-SMU/ggsc/issues URL: https://github.com/YuLab-SMU/ggsc (devel), https://yulab-smu.top/ggsc/ (docs) diff --git a/R/plot-methods.R b/R/plot-methods.R index 5fd36bb..9914e4a 100644 --- a/R/plot-methods.R +++ b/R/plot-methods.R @@ -18,7 +18,7 @@ ##' top point point layer, default is \code{.1}. ##' @param bg_line_width numeric the line width of background point layer, ##' default is \code{0.3}. -##' @param facet_name the name of facet used in \code{ggh4x::facet_nested_wrap()}, +##' @param facet_name the name of facet used in \code{facet_wrap()}, ##' default is \code{NULL}. ##' @param reduction reduction method, default is \code{NULL} and will ##' use the default setting store in the object @@ -39,7 +39,6 @@ ##' @examples ##' \dontrun{ ##' library(ggplot2) -##' library(ggh4x) ##' library(SingleCellExperiment) |> suppressPackageStartupMessages() ##' library(SpatialExperiment) |> suppressPackageStartupMessages() ##' library(STexampleData) @@ -165,7 +164,7 @@ plot_lisa_feature <- function(spe, tmpf <- as.formula("~sample_id") } p1 <- p1 %add+% - ggh4x::facet_nested_wrap(tmpf, labeller = label_wrap_gen(label_wrap_width)) %add+% + facet_wrap(tmpf, labeller = label_wrap_gen(label_wrap_width)) %add+% theme(strip.background.x=element_rect(color="white")) } return(p1) diff --git a/man/plot-lisa-feature.Rd b/man/plot-lisa-feature.Rd index a111fe6..3f98887 100644 --- a/man/plot-lisa-feature.Rd +++ b/man/plot-lisa-feature.Rd @@ -53,7 +53,7 @@ top point point layer, default is \code{.1}.} \item{bg_line_width}{numeric the line width of background point layer, default is \code{0.3}.} -\item{facet_name}{the name of facet used in \code{ggh4x::facet_nested_wrap()}, +\item{facet_name}{the name of facet used in \code{facet_wrap()}, default is \code{NULL}.} \item{reduction}{reduction method, default is \code{NULL} and will @@ -81,7 +81,6 @@ plot_lisa_feature \examples{ \dontrun{ library(ggplot2) -library(ggh4x) library(SingleCellExperiment) |> suppressPackageStartupMessages() library(SpatialExperiment) |> suppressPackageStartupMessages() library(STexampleData)