Skip to content

Commit

Permalink
update the rd with the new point layer
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangpin committed Apr 12, 2024
1 parent 26dd2ac commit 030a98b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 31 deletions.
14 changes: 13 additions & 1 deletion R/sc-dim.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
##' \code{alpha} or (\code{shape} when geom = geom_point) can be set manually.
##' @param geom the function of geometric layer, default is sc_geom_point,
##' other geometric layer, such as geom_point also works.
##' @param ... additional parameters pass to 'scattermore::geom_scattermore()'
##' @param ... additional parameters pass to \code{geom_scattermore2()}.
##' \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 dimension reduction plot
##' @seealso
##' [geom_scattermore][scattermore::geom_scattermore];
Expand Down
12 changes: 12 additions & 0 deletions R/sc-feature.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
##' @param common.legend whether to use \code{facet_wrap} to display the multiple
##' \code{features}, default is TRUE.
##' @param ... additional parameters pass to '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 dimension reduction plot colored by selected features
##' @importFrom ggplot2 theme
##' @importFrom ggplot2 theme_bw
Expand Down
17 changes: 10 additions & 7 deletions R/sc-geom-point.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
##' sc_geom_point()
sc_geom_point <- function(mapping=NULL, ...){
default_params <- list(mapping = mapping,
pointsize = 4,
pointsize = 3,
pixels = c(700, 700)
)
params <- modifyList(default_params, list(...))
Expand All @@ -34,12 +34,13 @@ sc_geom_point <- function(mapping=NULL, ...){
#' passed to \code{\link[grid]{rasterGrob}}.
#' @param pointsize Radius of rasterized point. Use ‘0’ for single pixels (fastest).
#' @param pixels Vector with X and Y resolution of the raster, default \code{c(512,512)}.
#' @param gap_colour colour between the background and top point point layer,
#' default is \code{white}.
#' @param gap_colour colour of gap background between the bottom background
#' and top point point layer, default is \code{white}.
#' @param gap_alpha numeric the transparency of gap background colour, default is 1.
#' @param bg_line_width numeric the line width of background point layer,
#' default is \code{0.3}.
#' @param gap_line_width numeric the line width of gap between the background and
#' top point point layer, default is \code{.05}.
#' top point point layer, default is \code{.1}.
#' @param ... Other arguments passed on to \code{\link[ggplot2]{layer}}.
#' @details
#' \itemize{
Expand All @@ -61,7 +62,7 @@ sc_geom_point <- function(mapping=NULL, ...){
geom_scattermore2 <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity", ...,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
interpolate = FALSE, pointsize = 0, pixels = c(512, 512),
gap_colour = 'white', bg_line_width = .3, gap_line_width = .05){
gap_colour = 'white', gap_alpha = 1, bg_line_width = .3, gap_line_width = .1){
ggplot2::layer(
data = data,
mapping = mapping,
Expand All @@ -76,6 +77,7 @@ geom_scattermore2 <- function(mapping = NULL, data = NULL, stat = "identity", po
pointsize = pointsize,
pixels = pixels,
gap_colour = gap_colour,
gap_alpha = gap_alpha,
bg_line_width = bg_line_width,
gap_line_width = gap_line_width,
...
Expand Down Expand Up @@ -104,8 +106,9 @@ GeomScattermore2 <- ggplot2::ggproto("GeomScattermore2", ggplot2::Geom,
na.rm = FALSE,
pixels = c(512, 512),
gap_colour = 'white',
gap_alpha = 1,
bg_line_width = .3,
gap_line_width = .05){
gap_line_width = .1){
coords <- coord$transform(data, pp)

upperimage <- scattermore(cbind(coords$x, coords$y),
Expand All @@ -126,7 +129,7 @@ GeomScattermore2 <- ggplot2::ggproto("GeomScattermore2", ggplot2::Geom,
ylim = c(0, 1),
size = pixels)
gapimage <- scattermore(cbind(coords$x, coords$y),
rgba = grDevices::col2rgb(alpha = TRUE, scales::alpha(gap_colour, 1)),
rgba = grDevices::col2rgb(alpha = TRUE, scales::alpha(gap_colour, gap_alpha)),
cex = gapsize,
xlim = c(0, 1),
ylim = c(0, 1),
Expand Down
31 changes: 20 additions & 11 deletions R/sc-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@
##' default is prod.
##' @param common.legend whether to use \code{facet_wrap} to display the multiple
##' \code{features}, default is TRUE.
##' @param point.size the size of point, default is 5.
##' @param ... additional parameters.
##' @param pointsize the size of point, default is 5.
##' @param ... additional parameters, see also \code{geom_scattermore2()}.
##' \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 grid rasterGrob unit
##' @importFrom ggplot2 facet_grid annotation_custom
Expand Down Expand Up @@ -74,7 +86,7 @@ setGeneric('sc_spatial', function(object,
joint = FALSE,
joint.fun = prod,
common.legend = TRUE,
point.size = 5,
pointsize = 5,
...)
standardGeneric('sc_spatial')
)
Expand All @@ -90,7 +102,7 @@ setMethod("sc_spatial", 'Seurat',
image.rotate.degree = NULL, image.mirror.axis = 'v',
remove.point = FALSE, mapping = NULL, ncol = 6,
density=FALSE, grid.n = 100, joint = FALSE,
joint.fun = prod, common.legend = TRUE, point.size = 5, ...) {
joint.fun = prod, common.legend = TRUE, pointsize = 5, ...) {
images <- SeuratObject::Images(object = object,
assay = Seurat::DefaultAssay(object = object)
)
Expand Down Expand Up @@ -145,7 +157,6 @@ setMethod("sc_spatial", 'Seurat',
mapping <- default_mapping
}

ratio <- .cal_ratio(d, mapping)

if (!plot.pie){
p <- ggplot(d, mapping)
Expand All @@ -162,7 +173,7 @@ setMethod("sc_spatial", 'Seurat',
}

if ((!remove.point && (!is.null(features) || (any(names(mapping) %in% c('color', 'colour')) && is.null(features))) && !plot.pie)){
p <- p + sc_geom_point(pointsize = point.size, ...)
p <- p + sc_geom_point(pointsize = pointsize, ...)
}else if (!remove.point && plot.pie){
rlang::check_installed('scatterpie', 'is required when `plot.pie=TRUE`')
p <- p + scatterpie::geom_scatterpie(data=d, mapping=mapping, cols='features', long_format=TRUE, pie_scale=pie.radius.scale, ...)
Expand All @@ -174,7 +185,7 @@ setMethod("sc_spatial", 'Seurat',
.feature_setting(features, ncol, plot.pie) +
ylab(NULL) +
xlab(NULL) +
coord_fixed(ratio=ratio) +
coord_fixed() +
theme_bw2()

color.aes <- .check_aes_exits(p$mapping, c('color', 'colour'))
Expand Down Expand Up @@ -282,8 +293,6 @@ setMethod('sc_spatial', 'SingleCellExperiment', function(object,
mapping <- default_mapping
}

ratio <- .cal_ratio(d, mapping)

if (!plot.pie){
p <- ggplot(d, mapping)
}else{
Expand All @@ -299,7 +308,7 @@ setMethod('sc_spatial', 'SingleCellExperiment', function(object,
}

if ((!remove.point && (!is.null(features) || (any(names(mapping) %in% c('color', 'colour')) && is.null(features))) && !plot.pie)){
p <- p + sc_geom_point(pointsize = point.size, ...)
p <- p + sc_geom_point(pointsize = pointsize, ...)
}else if (!remove.point && plot.pie){
rlang::check_installed('scatterpie', 'is required when `plot.pie=TRUE`')
p <- p + scatterpie::geom_scatterpie(data=d, mapping=mapping, cols='features', long_format=TRUE, pie_scale = pie.radius.scale, ...)
Expand All @@ -311,7 +320,7 @@ setMethod('sc_spatial', 'SingleCellExperiment', function(object,
.feature_setting(features, ncol, plot.pie) +
ylab(NULL) +
xlab(NULL) +
coord_fixed(ratio = ratio) +
coord_fixed() +
theme_bw2()

color.aes <- .check_aes_exits(p$mapping, c('color', 'colour'))
Expand Down
11 changes: 7 additions & 4 deletions man/geom_scattermore2.Rd

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

14 changes: 13 additions & 1 deletion man/sc-dim-methods.Rd

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

14 changes: 13 additions & 1 deletion man/sc-feature-methods.Rd

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

24 changes: 18 additions & 6 deletions man/sc-spatial-methods.Rd

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

0 comments on commit 030a98b

Please sign in to comment.