From a23efcddec247bf5bae2116bdcee07893160ebb6 Mon Sep 17 00:00:00 2001 From: xushuangbin Date: Thu, 29 Aug 2024 17:18:26 +0800 Subject: [PATCH] colour in aes mapping of sc_dim_geom_label sc_dim_geom_ellipse work with sc_features by adding new_scale_colour of ggnewscale --- .github/workflows/rworkflows.yml | 4 ++-- R/sc-dim-utilities.R | 10 +++++++--- R/utils.R | 15 +++++++++++++++ man/sc-dim-geom-label.Rd | 4 +++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.github/workflows/rworkflows.yml b/.github/workflows/rworkflows.yml index 8e6bc3f..4037ea8 100644 --- a/.github/workflows/rworkflows.yml +++ b/.github/workflows/rworkflows.yml @@ -30,12 +30,12 @@ jobs: - os: macOS-latest bioc: release r: auto - cont: ghcr.io/bioconductor/bioconductor_docker:RELEASE_3_19 + cont: ~ rspm: ~ - os: windows-latest bioc: release r: auto - cont: ghcr.io/bioconductor/bioconductor_docker:RELEASE_3_19 + cont: ~ rspm: ~ steps: - uses: neurogenomics/rworkflows@master diff --git a/R/sc-dim-utilities.R b/R/sc-dim-utilities.R index a10fed0..b5875ab 100644 --- a/R/sc-dim-utilities.R +++ b/R/sc-dim-utilities.R @@ -158,6 +158,7 @@ ggplot_add.sc_dim_geom_feature <- function(object, plot, object_name){ ##' @title sc_dim_geom_label ##' @rdname sc-dim-geom-label ##' @param geom geometric layer (default: geom_text) to display the lables +##' @param mapping aesthetic mapping ##' @param ... additional parameters pass to the geom ##' @return layer of labels ##' @export @@ -175,8 +176,8 @@ ggplot_add.sc_dim_geom_feature <- function(object, plot, object_name){ ##' p1 <- sc_dim(sce, reduction = 'UMAP', mapping = aes(colour = Cell_Cycle)) ##' p2 <- sc_dim(sce, reduction = 'UMAP') ##' f1 <- p1 + sc_dim_geom_label() -sc_dim_geom_label <- function(geom = ggplot2::geom_text, ...) { - structure(list(geom = geom, ...), +sc_dim_geom_label <- function(geom = ggplot2::geom_text, mapping=NULL, ...) { + structure(list(geom = geom, mapping = mapping, ...), class = "sc_dim_geom_label") } @@ -198,6 +199,7 @@ ggplot_add.sc_dim_geom_label <- function(object, plot, object_name) { lapply(function(x).calculate_ellipse(x, vars = dims[c(2, 3)], level=object$level)) |> dplyr::bind_rows(.id=lab.text) object$level <- NULL + object$data <- .set_label_levels(object$data, plot, lab.text) }else{ cli::cli_abort("The `label` in mapping should be specified, and the data should not be numeric type!") } @@ -215,6 +217,8 @@ ggplot_add.sc_dim_geom_label <- function(object, plot, object_name) { if (flag2){ object$colour <- 'black' } + + object <- .set_inherit.aes(object) ly <- do.call(geom, object) ggplot_add(ly, plot, object_name) @@ -283,7 +287,7 @@ ggplot_add.sc_dim_geom_ellipse <- function(object, plot, object_name) { if (flag2){ object$colour <- 'black' } - + object <- .set_inherit.aes(object) geomfun <- object$geom object$geom <- NULL diff --git a/R/utils.R b/R/utils.R index 60efd91..3798fd7 100644 --- a/R/utils.R +++ b/R/utils.R @@ -50,3 +50,18 @@ class(x) <- c(unique(clsnm), unique(old)) return(x) } + +.set_label_levels <- function(data, plot, lab.text){ + lab.levels <- levels(plot$data[[lab.text]]) + if (!is.null(lab.levels)){ + data[[lab.text]] <- factor(data[[lab.text]], levels=lab.levels) + } + return(data) +} + +.set_inherit.aes <- function(x){ + if (!'inherit.aes' %in% names(x)){ + x$inherit.aes <- FALSE + } + return(x) +} diff --git a/man/sc-dim-geom-label.Rd b/man/sc-dim-geom-label.Rd index daccd89..883f033 100644 --- a/man/sc-dim-geom-label.Rd +++ b/man/sc-dim-geom-label.Rd @@ -4,11 +4,13 @@ \alias{sc_dim_geom_label} \title{sc_dim_geom_label} \usage{ -sc_dim_geom_label(geom = ggplot2::geom_text, ...) +sc_dim_geom_label(geom = ggplot2::geom_text, mapping = NULL, ...) } \arguments{ \item{geom}{geometric layer (default: geom_text) to display the lables} +\item{mapping}{aesthetic mapping} + \item{...}{additional parameters pass to the geom} } \value{