forked from YuLab-SMU/ggsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc-geom-point.R
290 lines (274 loc) · 10.2 KB
/
sc-geom-point.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
##' @title sc_geom_point
##' @rdname sc-geom-point
##' @param mapping aesthetic mapping
##' @param ... additional parameters pass to 'scattermore::geom_scattermore()'
##' @return layer of points
##' @importFrom scattermore geom_scattermore
##' @seealso [sc_dim()] and [sc_feature()]
##' @export
##' @examples
##' library(ggplot2)
##' ggplot(iris,
##' aes(x= Sepal.Length, y = Petal.Width, color=Species)
##' ) +
##' sc_geom_point()
sc_geom_point <- function(mapping=NULL, ...){
default_params <- list(mapping = mapping,
pointsize = 3,
pixels = c(700, 700)
)
params <- modifyList(default_params, list(...))
do.call(geom_scattermore2, params)
}
#' @title geom_bgpoint
#' @description
#' this add the background color for \code{\link[ggplot2]{geom_point}}
#' @eval rd_aesthetics("geom", "bgpoint")
#' @inheritParams ggplot2::layer
#' @param na.rm If \code{FALSE}, the default, missing values are removed
#' with a warning, if \code{TRUE}, missing values are silently removed.
#' @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{.1}.
#' @param pointsize numeric the size of point, default is NULL, will use the
#' internal size aesthetics of \code{geom_bgpoint}
#' @param ... Other arguments passed on to \code{\link[ggplot2]{layer}}.
#' @details
#' \itemize{
#' \item \code{colour} the colour of point, default is \code{black}.
#' \item \code{bg_colour} the colour of background point, default is \code{NA}.
#' \item \code{alpha} the transparency of colour, default is 1.
#' \item \code{subset} subset the data frame which meet conditions to display.
#' }
#' @importFrom rlang list2
#' @author Shuangbin Xu
#' @export
#' @examples
##' library(ggplot2)
##' ggplot(iris,
##' aes(x= Sepal.Length, y = Petal.Width, color=Species, bg_colour=Species)
##' ) +
##' geom_bgpoint(pointsize=4, gap_line_width = .1, bg_line_width = .3)
geom_bgpoint <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
gap_colour = 'white',
gap_alpha = 1,
bg_line_width = .3,
gap_line_width = .1,
pointsize = NULL
){
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomBgpoint,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
na.rm = na.rm,
gap_colour = gap_colour,
gap_alpha = gap_alpha,
bg_line_width = bg_line_width,
gap_line_width = gap_line_width,
pointsize = pointsize,
...
)
)
}
#' @title geom_scattermore2
#' @description
#' this add the background colour for the \code{\link[scattermore]{geom_scattermore}}
#' @eval rd_aesthetics("geom", "scattermore2")
#' @inheritParams ggplot2::layer
#' @param na.rm If `FALSE`, the default, missing values are removed with
#' a warning. If `TRUE`, missing values are silently removed.
#' @param interpolate A logical value indicating whether to linearly interpolate
#' the image (the alternative is to use nearest-neighbour interpolation,
#' which gives a more blocky result). Default \code{FALSE},
#' 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 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{.1}.
#' @param ... Other arguments passed on to \code{\link[ggplot2]{layer}}.
#' @details
#' \itemize{
#' \item \code{colour} the colour of point, default is \code{black}.
#' \item \code{bg_colour} the colour of background point, default is \code{NA}.
#' \item \code{alpha} the transparency of colour, default is 1.
#' \item \code{subset} subset the data frame which meet conditions to display.
#' }
#' @return polygonal point layer
#' @importFrom ggplot2 layer
#' @author Shuangbin Xu
#' @export
#' @examples
##' library(ggplot2)
##' ggplot(iris,
##' aes(x= Sepal.Length, y = Petal.Width, color=Species, bg_colour=Species)
##' ) +
##' geom_scattermore2(pointsize=4, gap_line_width = .1, bg_line_width = .3)
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', gap_alpha = 1, bg_line_width = .3, gap_line_width = .1){
ggplot2::layer(
data = data,
mapping = mapping,
stat = stat,
position = position,
geom = GeomScattermore2,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
na.rm = na.rm,
interpolate = interpolate,
pointsize = pointsize,
pixels = pixels,
gap_colour = gap_colour,
gap_alpha = gap_alpha,
bg_line_width = bg_line_width,
gap_line_width = gap_line_width,
...
)
)
}
#' @importFrom scattermore scattermore
GeomScattermore2 <- ggplot2::ggproto("GeomScattermore2", ggplot2::Geom,
required_aes = c("x", "y"),
non_missing_aes = c("alpha", "colour"),
optional_aes = c("subset"),
default_aes = ggplot2::aes(
shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = 1, stroke = 0.5, bg_colour = NA
),
setup_data = function(data, params){
if (is.null(data$subset))
return(data)
data[which(data$subset),]
},
draw_panel = function(data, pp, coord,
pointsize = 0,
interpolate = FALSE,
na.rm = FALSE,
pixels = c(512, 512),
gap_colour = 'white',
gap_alpha = 1,
bg_line_width = .3,
gap_line_width = .1){
coords <- coord$transform(data, pp)
upperimage <- scattermore(cbind(coords$x, coords$y),
rgba = grDevices::col2rgb(alpha = TRUE, scales::alpha(coords$colour, coords$alpha)),
cex = pointsize,
xlim = c(0, 1),
ylim = c(0, 1),
size = pixels)
if (!all(is.null(coords$bg_colour)) && !all(is.na(coords$bg_colour))){
tmpsize <- sqrt(pointsize)
gapsize <- (tmpsize + tmpsize * gap_line_width * 2)^2
bgsize <- gapsize + (sqrt(bg_line_width) + tmpsize * bg_line_width * 2) ^2
bgimage <- scattermore(cbind(coords$x, coords$y),
rgba = grDevices::col2rgb(alpha = TRUE, scales::alpha(coords$bg_colour, 1)),
cex = bgsize,
xlim = c(0, 1),
ylim = c(0, 1),
size = pixels)
gapimage <- scattermore(cbind(coords$x, coords$y),
rgba = grDevices::col2rgb(alpha = TRUE, scales::alpha(gap_colour, gap_alpha)),
cex = gapsize,
xlim = c(0, 1),
ylim = c(0, 1),
size = pixels)
}else{
bgimage <- gapimage <- NULL
}
ggplot2:::ggname(
"geom_scattermore2",
crasterGrob(
upperimage, bgimage, gapimage,
0, 0, 1, 1,
default.units = "native",
just = c("left", "bottom"),
interpolate = interpolate
)
)
},
draw_key = draw_key_bgpoint
)
#' @importFrom ggplot2 translate_shape_string draw_key_point
GeomBgpoint <- ggplot2::ggproto("GeomBgpoint", ggplot2::Geom,
required_aes = c("x", "y"),
non_missing_aes = c("alpha", "colour"),
optional_aes = c("subset"),
default_aes = ggplot2::aes(
shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = 1, stroke = 0.5, bg_colour = NA
),
setup_data = function(data, params){
if (is.null(data$subset))
return(data)
data[which(data$subset),]
},
draw_panel = function(self,
data,
panel_params,
coord,
na.rm = FALSE,
gap_colour = 'white',
gap_alpha = 1,
bg_line_width = .3,
gap_line_width = .1,
pointsize = NULL
){
if (is.character(data$shape)) {
data$shape <- translate_shape_string(data$shape)
}
coords <- coord$transform(data, panel_params)
stroke_size <- coords$stroke
stroke_size[is.na(stroke_size)] <- 0
if (is.null(pointsize)){
pointsize <- coords$size * .pt + stroke_size * .stroke / 2
}else{
pointsize <- rep(pointsize, nrow(coords)) * .pt + stroke_size * .stroke/2
}
ggname("geom_bgpoint",
cpointsGrob(
coords$x, coords$y,
pch = coords$shape,
bg_line_width = bg_line_width,
gap_line_width = gap_line_width,
bg_colour = coords$bg_colour,
gap_colour = alpha(gap_colour, gap_alpha),
gp = gpar(
col = alpha(coords$colour, coords$alpha),
fill = fill_alpha(coords$fill, coords$alpha),
fontsize = pointsize,
lwd = coords$stroke * .stroke / 2
)
)
)
},
draw_key = draw_key_bgpoint
)
#' @importFrom grid grobName
ggname <- function (prefix, grob){
grob$name <- grobName(grob, prefix)
grob
}