Skip to content

Commit

Permalink
Merge pull request #22 from xiangpin/master
Browse files Browse the repository at this point in the history
add background point layer in grid level to be compatable with the framework of ggsc
  • Loading branch information
GuangchuangYu authored Apr 12, 2024
2 parents 42894d0 + 030a98b commit b39eb82
Show file tree
Hide file tree
Showing 15 changed files with 730 additions and 24 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Imports:
tibble,
utils,
RColorBrewer,
yulab.utils
yulab.utils,
scales
Suggests:
aplot,
BiocParallel,
Expand Down
22 changes: 22 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ S3method(ggplot_add,sc_dim_geom_ellipse)
S3method(ggplot_add,sc_dim_geom_feature)
S3method(ggplot_add,sc_dim_geom_label)
export(aes)
export(draw_key_scattermore2)
export(geom_scattermore2)
export(sc_dim)
export(sc_dim_count)
export(sc_dim_geom_ellipse)
Expand All @@ -18,6 +20,9 @@ export(sc_feature)
export(sc_geom_point)
export(sc_spatial)
export(sc_violin)
export(scale_bg_color_manual)
export(scale_bg_colour_discrete)
export(scale_bg_colour_manual)
export(theme)
exportMethods(sc_dim)
exportMethods(sc_dot)
Expand All @@ -39,11 +44,14 @@ importFrom(SummarizedExperiment,colData)
importFrom(cli,cli_abort)
importFrom(ggfun,get_aes_var)
importFrom(ggplot2,"%+replace%")
importFrom(ggplot2,ScaleDiscreteIdentity)
importFrom(ggplot2,aes)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,annotation_custom)
importFrom(ggplot2,coord_fixed)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,discrete_scale)
importFrom(ggplot2,draw_key_point)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_text)
importFrom(ggplot2,facet_grid)
Expand All @@ -59,6 +67,7 @@ importFrom(ggplot2,guide_colorbar)
importFrom(ggplot2,guide_legend)
importFrom(ggplot2,guides)
importFrom(ggplot2,labs)
importFrom(ggplot2,layer)
importFrom(ggplot2,layer_data)
importFrom(ggplot2,rel)
importFrom(ggplot2,scale_color_distiller)
Expand All @@ -71,22 +80,35 @@ importFrom(ggplot2,stat_ellipse)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,theme_minimal)
importFrom(ggplot2,waiver)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(grDevices,as.raster)
importFrom(grDevices,colorRampPalette)
importFrom(grid,gList)
importFrom(grid,gTree)
importFrom(grid,gpar)
importFrom(grid,grid.draw)
importFrom(grid,grobName)
importFrom(grid,grobTree)
importFrom(grid,is.unit)
importFrom(grid,pointsGrob)
importFrom(grid,rasterGrob)
importFrom(grid,unit)
importFrom(methods,as)
importFrom(methods,setMethod)
importFrom(rlang,.data)
importFrom(scales,alpha)
importFrom(scales,pal_identity)
importFrom(scattermore,geom_scattermore)
importFrom(scattermore,scattermore)
importFrom(stats,dist)
importFrom(stats,hclust)
importFrom(stats,setNames)
importFrom(tibble,as_tibble)
importFrom(tidydr,theme_dr)
importFrom(tidyr,pivot_longer)
importFrom(utils,getFromNamespace)
importFrom(utils,modifyList)
importFrom(yulab.utils,get_fun_from_pkg)
useDynLib(ggsc)
40 changes: 40 additions & 0 deletions R/draw_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Key drawing functions
#'
#' Each Geom has an associated function that draws the key when the geom needs
#' to be displayed in a legend. These are the options built into ggplot2.
#'
#' @param data A single row data frame containing the scaled aesthetics to
#' display in this key
#' @param params A list of additional parameters supplied to the geom.
#' @param size Width and height of key in mm.
#' @return A grid grob.
#' @name draw_key_scattermore2
#' @export
#' @importFrom scales alpha
#' @importFrom ggplot2 draw_key_point
#' @importFrom grid grobTree pointsGrob
draw_key_scattermore2 <- function(data, params, size){
pointkey <- draw_key_point(data, params, size)
if (is.null(data$bg_colour)){
return (pointkey)
}

stroke_size <- data$stroke %||% 0.5
stroke_size[is.na(stroke_size)] <- 0

gp <- gpar(col = data$bg_colour,
fontsize = (data$size %||% 1.5) * .pt + stroke_size * .stroke / 2,
lwd = (data$stroke %||% 0.5) * 4)
grobTree(pointkey, pointsGrob(0.5, 0.5, pch = 21, gp = gp))
}

.pt <- 2.845276
.stroke <- 3.779528

`%||%` <- function (a, b){
if (!is.null(a))
a
else b
}

rd_aesthetics <- getFromNamespace("rd_aesthetics", "ggplot2")
102 changes: 102 additions & 0 deletions R/pointsGrob2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
##' @importFrom grid unit
##' @importFrom grid gpar
##' @importFrom grid gList
##' @importFrom grid gTree
##' @importFrom grid is.unit
##' @importFrom grid rasterGrob
crasterGrob <- function(image, bg.image = NULL, gap.image = NULL,
x = unit(0.5, "npc"), y = unit(0.5, "npc"),
width = NULL, height = NULL, just = "centre",
hjust = NULL, vjust = NULL, interpolate = TRUE,
default.units = "npc", name = NULL, gp = gpar(),
vp = NULL){

upperGrob <- rasterGrob(image, x = x, y = y, width = width, height = height,
just = just, hjust = hjust, vjust =vjust, interpolate = interpolate,
default.units = default.units, name = name, gp = gp, vp = vp)

if (is.null(bg.image)){
return(upperGrob)
}

bgGrob <- rasterGrob(bg.image, x = x, y = y, width = width, height = height,
just = just, hjust = hjust, vjust =vjust,
interpolate = interpolate, default.units = default.units,
name = name, gp = gp, vp = vp)

gapGrob <- rasterGrob(gap.image, x = x, y = y, width = width, height = height,
just = just, hjust = hjust, vjust =vjust,
interpolate = interpolate, default.units = default.units,
name = name, gp = gp, vp = vp)

grobs <- gList(bgGrob, gapGrob, upperGrob)
gTree(children = grobs)
}

grid.craster <- function (image, bg.image, gap.image, x = unit(0.5, "npc"),
y = unit(0.5, "npc"), width = NULL,
height = NULL, just = "centre", hjust = NULL,
vjust = NULL, interpolate = TRUE, default.units = "npc",
name = NULL, gp = gpar(), vp = NULL){

rg <- crasterGrob(image, bg.image, gap.image, x = x, y = y,
width = width, height = height, just = just,
hjust = hjust, vjust = vjust, interpolate = interpolate,
default.units = default.units, name = name, gp = gp,
vp = vp)

grid.draw(rg)
}


##' @importFrom grid pointsGrob
cpointsGrob <- function(x = stats::runif(10), y = stats::runif(10), pch = 1,
size = unit(1, "char"), bg_line_width = .3, gap_line_width = .1,
bg_colour = "black", gap.colour = 'white', default.units = "native",
name = NULL, gp = gpar(), vp = NULL){

upperPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size,
default.units = default.units, name = name,
gp = gp, vp = vp)

if (is.null(bg_colour)){
return(upperPointGrob)
}

gp.bg <- gp
gp.gap <- gp

gp.bg$col <- bg_colour
gp.gap$col <- gap.colour

gp$fontsize

tmpsize <- sqrt(gp$fontsize)
gp.gap$fontsize <- (tmpsize + tmpsize * gap_line_width * 2)^2
gp.bg$fontsize <- gp.gap$fontsize + (sqrt(bg_line_width) + tmpsize * bg_line_width * 2) ^2

gapPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size,
default.units = default.units, name = name,
gp = gp.gap, vp = vp)

bgPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size,
default.units = default.units, name = name,
gp = gp.bg, vp = vp)

grobs <- gList(bgPointGrob, gapPointGrob, upperPointGrob)
gTree(children = grobs)
}

##' @importFrom grid grid.draw
grid.cpoints <- function(x = stats::runif(10), y = stats::runif(10), pch = 1,
size = unit(1, "char"), bg_line_width = .3, gap_line_width = .1,
bg_colour = "black", gap.colour = 'white', default.units = "native",
name = NULL, gp = gpar(), draw = TRUE, vp = NULL){
pg <- cpointsGrob(x = x, y = y, pch = pch, size = size, bg_line_width = bg_line_width,
gap_line_width = gap_line_width, bg_colour = bg_colour,
gap.colour = gap.colour, default.units = default.units, name = name,
gp = gp, vp = vp)
if (draw) grid.draw(pg)
invisible(pg)
}

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
Loading

0 comments on commit b39eb82

Please sign in to comment.