-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstat-dens1d-filter.r
594 lines (559 loc) · 23.8 KB
/
stat-dens1d-filter.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#' @title Filter observations by local 1D density
#'
#' @description \code{stat_dens1d_filter} Filters-out/filters-in observations in
#' regions of a plot panel with high density of observations, based on the
#' values mapped to one of \code{x} and \code{y} aesthetics.
#' \code{stat_dens1d_filter_g} does the same filtering by group instead of by
#' panel. This second stat is useful for highlighting observations, while the
#' first one tends to be most useful when the aim is to prevent clashes among
#' text labels. By default the data are handled all together, but it is also
#' possible to control labeling separately in each tail.
#'
#' @details The 1D density of observations of \emph{x} or \emph{y} is computed
#' with function \code{\link[stats]{density}} and used to select observations,
#' passing to the geom a subset of the rows in its \code{data} input. The
#' default is to select observations in sparse regions of the plot, but the
#' selection can be inverted so that only observations in the densest regions
#' are returned. Specific observations can be protected from being deselected
#' and "kept" by passing a suitable argument to \code{keep.these}. Logical and
#' integer vectors work as indexes to rows in \code{data}, while a values in a
#' character vector are compared to the character values mapped to the
#' \code{label} aesthetic. A function passed as argument to keep.these will
#' receive as argument the values in the variable mapped to \code{label} and
#' should return a character, logical or numeric vector as described above. If
#' no variable has been mapped to \code{label}, row names are used in its
#' place.
#'
#' How many rows are retained in addition to those in \code{keep.these} is
#' controlled with arguments passed to \code{keep.number} and
#' \code{keep.fraction}. \code{keep.number} sets the maximum number of
#' observations selected, whenever \code{keep.fraction} results in fewer
#' observations selected, it is obeyed. If `xintercept` is a finite value
#' within the \emph{x} range of the data and \code{pool.along}
#' is passed \code{"none"} the data as are split into two groups
#' and \code{keep.number} and \code{keep.fraction} are applied separately to
#' each tail with density still computed jointly from all observations. If the
#' length of \code{keep.number} and \code{keep.fraction} is one, this value
#' is used for both tails, if their length is two, the first value is use
#' for the left tail and the second value for the right tail.
#'
#' Computation of density and of the default bandwidth require at least
#' two observations with different values. If data do not fulfill this
#' condition, they are kept only if \code{keep.fraction = 1}. This is correct
#' behavior for a single observation, but can be surprising in the case of
#' multiple observations.
#'
#' Parameters \code{keep.these} and \code{exclude.these} make it possible to
#' force inclusion or exclusion of observations after the density is computed.
#' In case of conflict, \code{exclude.these} overrides \code{keep.these}.
#'
#' @note Which points are kept and which not depends on how dense and flexible
#' is the density curve estimate. This depends on the values passed as
#' arguments to parameters \code{n}, \code{bw} and \code{kernel}. It is
#' also important to be aware that both \code{geom_text()} and
#' \code{geom_text_repel()} can avoid over plotting by discarding labels at
#' the plot rendering stage, i.e., what is plotted may differ from what is
#' returned by this statistic.
#'
#' @param mapping The aesthetic mapping, usually constructed with
#' \code{\link[ggplot2]{aes}} or \code{\link[ggplot2]{aes_}}. Only needs to be
#' set at the layer level if you are overriding the plot defaults.
#' @param data A layer specific dataset - only needed if you want to override
#' the plot defaults.
#' @param geom The geometric object to use display the data.
#' @param keep.fraction numeric vector of length 1 or 2 [0..1]. The fraction of
#' the observations (or rows) in \code{data} to be retained.
#' @param keep.number integer vector of length 1 or 2. Set the maximum number of
#' observations to retain, effective only if obeying \code{keep.fraction}
#' would result in a larger number.
#' @param keep.sparse logical If \code{TRUE}, the default, observations from the
#' more sparse regions are retained, if \code{FALSE} those from the densest
#' regions.
#' @param keep.these,exclude.these character vector, integer vector, logical
#' vector or function that takes one or more variables in data selected by
#' \code{these.target}. Negative integers behave as in R's extraction methods.
#' The rows from \code{data} indicated by \code{keep.these} and
#' \code{exclude.these} are kept or excluded irrespective of the local
#' density.
#' @param these.target character, numeric or logical selecting one or more
#' column(s) of \code{data}. If \code{TRUE} the whole \code{data} object is
#' passed.
#' @param pool.along character, one of \code{"none"} or \code{"x"},
#' indicating if selection should be done pooling the observations along the
#' \emph{x} aesthetic, or separately on either side of \code{xintercept}.
#' @param xintercept numeric The split point for the data filtering. If
#' \code{NA} the data are not split.
#' @param invert.selection logical If \code{TRUE}, the complement of the
#' selected rows are returned.
#' @param bw numeric or character The smoothing bandwidth to be used. If
#' numeric, the standard deviation of the smoothing kernel. If character, a
#' rule to choose the bandwidth, as listed in \code{\link[stats]{bw.nrd}}.
#' @param adjust numeric A multiplicative bandwidth adjustment. This makes it
#' possible to adjust the bandwidth while still using the a bandwidth
#' estimator through an argument passed to \code{bw}. The larger the value
#' passed to \code{adjust} the stronger the smoothing, hence decreasing
#' sensitivity to local changes in density.
#' @param kernel character See \code{\link{density}} for details.
#' @param n numeric Number of equally spaced points at which the density is to
#' be estimated for applying the cut point. See \code{\link{density}} for
#' details.
#' @param return.density logical vector of lenght 1. If \code{TRUE} add columns
#' \code{"density"} and \code{"keep.obs"} to the returned data frame.
#' @param orientation character The aesthetic along which density is computed.
#' Given explicitly by setting orientation to either \code{"x"} or \code{"y"}.
#' @param position The position adjustment to use for overlapping points on this
#' layer
#' @param show.legend logical. Should this layer be included in the legends?
#' \code{NA}, the default, includes if any aesthetics are mapped. \code{FALSE}
#' never includes, and \code{TRUE} always includes.
#' @param inherit.aes If \code{FALSE}, overrides the default aesthetics, rather
#' than combining with them. This is most useful for helper functions that
#' define both data and aesthetics and shouldn't inherit behaviour from the
#' default plot specification, e.g. \code{\link[ggplot2]{borders}}.
#' @param ... other arguments passed on to \code{\link[ggplot2]{layer}}. This
#' can include aesthetics whose values you want to set, not map. See
#' \code{\link[ggplot2]{layer}} for more details.
#' @param na.rm a logical value indicating whether \code{NA} values should be
#' stripped before the computation proceeds.
#'
#' @return A plot layer instance. Using as output \code{data} a subset of the
#' rows in input \code{data} retained based on a 1D filtering criterion.
#'
#' @seealso \code{\link[stats]{density}} used internally.
#'
#' @family statistics returning a subset of data
#'
#' @examples
#'
#' random_string <-
#' function(len = 6) {
#' paste(sample(letters, len, replace = TRUE), collapse = "")
#' }
#'
#' # Make random data.
#' set.seed(1001)
#' d <- tibble::tibble(
#' x = rnorm(100),
#' y = rnorm(100),
#' group = rep(c("A", "B"), c(50, 50)),
#' lab = replicate(100, { random_string() })
#' )
#' d$xg <- d$x
#' d$xg[51:100] <- d$xg[51:100] + 1
#'
#' # highlight the 1/10 of observations in sparsest regions of the plot
#' ggplot(data = d, aes(x, y)) +
#' geom_point() +
#' geom_rug(sides = "b") +
#' stat_dens1d_filter(colour = "red") +
#' stat_dens1d_filter(geom = "rug", colour = "red", sides = "b")
#'
#' # highlight the 1/4 of observations in densest regions of the plot
#' ggplot(data = d, aes(x, y)) +
#' geom_point() +
#' geom_rug(sides = "b") +
#' stat_dens1d_filter(colour = "blue",
#' keep.fraction = 1/4, keep.sparse = FALSE) +
#' stat_dens1d_filter(geom = "rug", colour = "blue",
#' keep.fraction = 1/4, keep.sparse = FALSE,
#' sides = "b")
#'
#' # switching axes
#' ggplot(data = d, aes(x, y)) +
#' geom_point() +
#' geom_rug(sides = "l") +
#' stat_dens1d_filter(colour = "red", orientation = "y") +
#' stat_dens1d_filter(geom = "rug", colour = "red", orientation = "y",
#' sides = "l")
#'
#' # highlight 1/10 plus 1/10 observations in high and low density regions
#' ggplot(data = d, aes(x, y)) +
#' geom_point() +
#' geom_rug(sides = "b") +
#' stat_dens1d_filter(colour = "red") +
#' stat_dens1d_filter(geom = "rug", colour = "red", sides = "b") +
#' stat_dens1d_filter(colour = "blue", keep.sparse = FALSE) +
#' stat_dens1d_filter(geom = "rug",
#' colour = "blue", keep.sparse = FALSE, sides = "b")
#'
#' # selecting the 1/10 observations in sparsest regions and their complement
#' ggplot(data = d, aes(x, y)) +
#' stat_dens1d_filter(colour = "red") +
#' stat_dens1d_filter(geom = "rug", colour = "red", sides = "b") +
#' stat_dens1d_filter(colour = "blue", invert.selection = TRUE) +
#' stat_dens1d_filter(geom = "rug",
#' colour = "blue", invert.selection = TRUE, sides = "b")
#'
#' # density filtering done jointly across groups
#' ggplot(data = d, aes(xg, y, colour = group)) +
#' geom_point() +
#' geom_rug(sides = "b", colour = "black") +
#' stat_dens1d_filter(shape = 1, size = 3, keep.fraction = 1/4, adjust = 2)
#'
#' # density filtering done independently for each group
#' ggplot(data = d, aes(xg, y, colour = group)) +
#' geom_point() +
#' geom_rug(sides = "b") +
#' stat_dens1d_filter_g(shape = 1, size = 3, keep.fraction = 1/4, adjust = 2)
#'
#' # density filtering done jointly across groups by overriding grouping
#' ggplot(data = d, aes(xg, y, colour = group)) +
#' geom_point() +
#' geom_rug(sides = "b") +
#' stat_dens1d_filter_g(colour = "black",
#' shape = 1, size = 3, keep.fraction = 1/4, adjust = 2)
#'
#' # label observations
#' ggplot(data = d, aes(x, y, label = lab, colour = group)) +
#' geom_point() +
#' stat_dens1d_filter(geom = "text", hjust = "outward")
#'
#' # looking under the hood with gginnards::geom_debug()
#' gginnards.installed <- requireNamespace("ggrepel", quietly = TRUE)
#' if (gginnards.installed) {
#' library(gginnards)
#'
#' ggplot(data = d, aes(x, y, label = lab, colour = group)) +
#' stat_dens1d_filter(geom = "debug")
#'
#' ggplot(data = d, aes(x, y, label = lab, colour = group)) +
#' stat_dens1d_filter(geom = "debug", return.density = TRUE)
#'
#' }
#'
#' @export
#'
stat_dens1d_filter <-
function(mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
keep.fraction = 0.10,
keep.number = Inf,
keep.sparse = TRUE,
keep.these = FALSE,
exclude.these = FALSE,
these.target = "label",
pool.along = c("x", "none"),
xintercept = 0,
invert.selection = FALSE,
bw = "SJ",
kernel = "gaussian",
adjust = 1,
n = 512,
return.density = FALSE,
orientation = c("x", "y"),
na.rm = TRUE,
show.legend = FALSE,
inherit.aes = TRUE) {
pool.along <- rlang::arg_match(pool.along)
orientation <- rlang::arg_match(orientation)
if (any(is.na(keep.fraction) | keep.fraction < 0 | keep.fraction > 1)) {
stop("Out of range or missing value for 'keep.fraction': ", keep.fraction)
}
if (any(is.na(keep.number) | keep.number < 0)) {
stop("Out of range or missing value for 'keep.number': ", keep.number)
}
ggplot2::layer(
stat = StatDens1dFilter, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm,
keep.fraction = keep.fraction,
keep.number = keep.number,
keep.sparse = keep.sparse,
keep.these = keep.these,
exclude.these = exclude.these,
these.target = these.target,
pool.along = pool.along,
xintercept = xintercept,
invert.selection = invert.selection,
bw = bw,
adjust = adjust,
kernel = kernel,
n = n,
return.density = return.density,
orientation = orientation,
...)
)
}
#' @rdname stat_dens1d_filter
#'
#' @export
#'
stat_dens1d_filter_g <-
function(mapping = NULL, data = NULL,
geom = "point", position = "identity",
keep.fraction = 0.10,
keep.number = Inf,
keep.sparse = TRUE,
keep.these = FALSE,
exclude.these = FALSE,
these.target = "label",
pool.along = c("x", "none"),
xintercept = 0,
invert.selection = FALSE,
na.rm = TRUE, show.legend = FALSE,
inherit.aes = TRUE,
bw = "SJ",
adjust = 1,
kernel = "gaussian",
n = 512,
return.density = FALSE,
orientation = c("x", "y"),
...) {
pool.along <- rlang::arg_match(pool.along)
orientation <- rlang::arg_match(orientation)
if (any(is.na(keep.fraction) | keep.fraction < 0 | keep.fraction > 1)) {
stop("Out of range or missing value for 'keep.fraction': ", keep.fraction)
}
if (any(is.na(keep.number) | keep.number < 0)) {
stop("Out of range or missing value for 'keep.number': ", keep.number)
}
ggplot2::layer(
stat = StatDens1dFilterG, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm,
keep.fraction = keep.fraction,
keep.number = keep.number,
keep.sparse = keep.sparse,
keep.these = keep.these,
exclude.these = exclude.these,
these.target = these.target,
pool.along = pool.along,
xintercept = xintercept,
invert.selection = invert.selection,
bw = bw,
kernel = kernel,
adjust = adjust,
n = n,
return.density = return.density,
orientation = orientation,
...)
)
}
#' @rdname ggpp-ggproto
#' @format NULL
#' @usage NULL
#' @export
StatDens1dFilter <-
ggplot2::ggproto(
"StatDens1dFilter",
ggplot2::Stat,
compute_panel =
# dens1d_flt_compute_fun,
# code duplicated below to ensure it is all seen by 'covr'
function(data,
scales,
keep.fraction,
keep.number,
keep.sparse,
keep.these,
exclude.these,
these.target,
pool.along,
xintercept,
invert.selection,
bw,
kernel,
adjust,
n,
orientation,
return.density) {
force(data)
keep.these <- these2logical(these = keep.these,
data = data,
these.target = these.target)
exclude.these <- these2logical(these = exclude.these,
data = data,
these.target = these.target)
# discard redundant splits and make list of logical vectors
if (pool.along != "x" &&
xintercept < max(data[[orientation]]) &&
xintercept > min(data[[orientation]])) {
selectors <-list(low.tail = data[[orientation]] <= xintercept,
high.tail = data[[orientation]] > xintercept)
if (length(keep.fraction) != 2L) {
keep.fraction <- rep_len(keep.fraction, length.out = 2)
}
if (length(keep.number) != 2L) {
if (length(keep.number) == 1L) {
keep.number <- keep.number %/% 2
}
keep.number <- rep_len(keep.number, length.out = 2)
}
num.rows <- sapply(selectors, sum) # selectors are logical
} else {
keep.fraction <- keep.fraction[[1]] # can be a vector or a list
keep.number <- keep.number[[1]]
num.rows <- nrow(data)
selectors <- list(all = rep.int(TRUE, times = num.rows))
}
# vectorized
too.large.frac <- num.rows * keep.fraction > keep.number
keep.fraction[too.large.frac] <-
keep.number[too.large.frac] / num.rows[too.large.frac]
# density on a grid
# data with fewer than 2 rows is as a special case as density() fails
if (length(unique(data[[orientation]])) >= 2L) {
dens <-
stats::density(data[[orientation]],
bw = bw, kernel = kernel, adjust = adjust, n = n,
from = scales[[orientation]]$dimension()[1],
to = scales[[orientation]]$dimension()[2])
# estimate density at each observations coordinates
fdens <- stats::splinefun(dens$x, dens$y) # y contains estimate of density
dens <- fdens(data[[orientation]])
} else {
if (nrow(data) > 1L) {
message("Density not computed, too few distinct values in '", orientation, "'")
}
dens <- rep_len(1, nrow(data))
}
# we construct one logical vector by adding observations/label to be kept
# we may have a list of 1 or 2 logical vectors
keep <- logical(nrow(data))
for (i in seq_along(selectors)) {
if (keep.fraction[i] == 1 ||
(length(selectors[[i]]) < 2L && keep.fraction[i] >= 0.5)) {
keep[ selectors[[i]] ] <- TRUE
} else if (keep.fraction[i] != 0 && length(selectors[[i]]) >= 2L) {
if (keep.sparse) {
keep[ selectors[[i]] ] <-
dens[ selectors[[i]] ] < stats::quantile(dens[ selectors[[i]] ],
keep.fraction[i],
names = FALSE,
type = 8)
} else {
keep[ selectors[[i]] ] <-
dens[ selectors[[i]] ] >= stats::quantile(dens[ selectors[[i]] ],
1 - keep.fraction[i],
names = FALSE,
type = 8)
}
}
}
keep <- (keep | keep.these) & !exclude.these
if (invert.selection){
keep <- !keep
}
if (return.density) {
data[["keep.obs"]] <- keep
data[["density"]] <- dens
}
data[keep, ]
},
required_aes = "x|y"
)
#' @rdname ggpp-ggproto
#' @format NULL
#' @usage NULL
#' @export
StatDens1dFilterG <-
ggplot2::ggproto(
"StatDens1dFilterG",
ggplot2::Stat,
compute_group =
# dens1d_flt_compute_fun,
# code duplicated above to ensure it is all seen by 'covr'
function(data,
scales,
keep.fraction,
keep.number,
keep.sparse,
keep.these,
exclude.these,
these.target,
pool.along,
xintercept,
invert.selection,
bw,
kernel,
adjust,
n,
orientation,
return.density) {
force(data)
keep.these <- these2logical(these = keep.these,
data = data,
these.target = these.target)
exclude.these <- these2logical(these = exclude.these,
data = data,
these.target = these.target)
# discard redundant splits and make list of logical vectors
if (pool.along != "x" &&
xintercept < max(data[[orientation]]) &&
xintercept > min(data[[orientation]])) {
selectors <-list(low.tail = data[[orientation]] <= xintercept,
high.tail = data[[orientation]] > xintercept)
if (length(keep.fraction) != 2L) {
keep.fraction <- rep_len(keep.fraction, length.out = 2)
}
if (length(keep.number) != 2L) {
if (length(keep.number) == 1L) {
keep.number <- keep.number %/% 2
}
keep.number <- rep_len(keep.number, length.out = 2)
}
num.rows <- sapply(selectors, sum) # selectors are logical
} else {
keep.fraction <- keep.fraction[[1]] # can be a vector or a list
keep.number <- keep.number[[1]]
num.rows <- nrow(data)
selectors <- list(all = rep.int(TRUE, times = num.rows))
}
# vectorized
too.large.frac <- num.rows * keep.fraction > keep.number
keep.fraction[too.large.frac] <-
keep.number[too.large.frac] / num.rows[too.large.frac]
# density on a grid
# data with fewer than 2 rows is as a special case as density() fails
if (length(unique(data[[orientation]])) >= 2L) {
dens <-
stats::density(data[[orientation]],
bw = bw, kernel = kernel, adjust = adjust, n = n,
from = scales[[orientation]]$dimension()[1],
to = scales[[orientation]]$dimension()[2])
# estimate density at each observations coordinates
fdens <- stats::splinefun(dens$x, dens$y) # y contains estimate of density
dens <- fdens(data[[orientation]])
} else {
if (nrow(data) > 1L) {
message("Density not computed, too few distinct values in '", orientation, "'")
}
dens <- rep_len(1, nrow(data))
}
# we construct one logical vector by adding observations/label to be kept
# we may have a list of 1 or 2 logical vectors
keep <- logical(nrow(data))
for (i in seq_along(selectors)) {
if (keep.fraction[i] == 1) {
keep[ selectors[[i]] ] <- TRUE
} else if (keep.fraction[i] != 0 && length(selectors[[i]]) >= 2L) {
if (keep.sparse) {
keep[ selectors[[i]] ] <-
dens[ selectors[[i]] ] < stats::quantile(dens[ selectors[[i]] ],
keep.fraction[i],
names = FALSE,
type = 8)
} else {
keep[ selectors[[i]] ] <-
dens[ selectors[[i]] ] >= stats::quantile(dens[ selectors[[i]] ],
1 - keep.fraction[i],
names = FALSE,
type = 8)
}
}
}
keep <- (keep | keep.these) & !exclude.these
if (invert.selection){
keep <- !keep
}
if (return.density) {
data[["keep.obs"]] <- keep
data[["density"]] <- dens
}
data[keep, ]
},
required_aes = "x|y"
)