Skip to content

Commit

Permalink
Merge pull request #32 from xiangpin/master
Browse files Browse the repository at this point in the history
colour mapping of sc_dim_geom_label and sc_dim_geom_ellipse work with sc_feature
  • Loading branch information
GuangchuangYu authored Aug 30, 2024
2 parents 760ece2 + a23efcd commit 17fe232
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rworkflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions R/sc-dim-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}

Expand All @@ -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!")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion man/sc-dim-geom-label.Rd

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

0 comments on commit 17fe232

Please sign in to comment.