Skip to content

Commit 99e2f87

Browse files
committed
Add drop argument as switch
1 parent 19d9b2e commit 99e2f87

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

R/stat-ydensity.r

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#' @param scale if "area" (default), all violins have the same area (before trimming
55
#' the tails). If "count", areas are scaled proportionally to the number of
66
#' observations. If "width", all violins have the same maximum width.
7+
#' @param drop Whether to discard groups with less than 2 observations
8+
#' (`TRUE`, default) or keep such groups for position adjustment purposes
9+
#' (`FALSE`).
710
#' @section Computed variables:
811
#' \describe{
912
#' \item{density}{density estimate}
@@ -26,6 +29,7 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
2629
kernel = "gaussian",
2730
trim = TRUE,
2831
scale = "area",
32+
drop = TRUE,
2933
na.rm = FALSE,
3034
orientation = NA,
3135
show.legend = NA,
@@ -46,6 +50,7 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
4650
kernel = kernel,
4751
trim = trim,
4852
scale = scale,
53+
drop = drop,
4954
na.rm = na.rm,
5055
...
5156
)
@@ -70,8 +75,18 @@ StatYdensity <- ggproto("StatYdensity", Stat,
7075
extra_params = c("na.rm", "orientation"),
7176

7277
compute_group = function(self, data, scales, width = NULL, bw = "nrd0", adjust = 1,
73-
kernel = "gaussian", trim = TRUE, na.rm = FALSE, flipped_aes = FALSE) {
78+
kernel = "gaussian", trim = TRUE, na.rm = FALSE,
79+
drop = TRUE, flipped_aes = FALSE) {
7480
if (nrow(data) < 2) {
81+
if (isTRUE(drop)) {
82+
cli::cli_warn(c(
83+
"Groups with fewer than two datapoints have been dropped.",
84+
i = paste0(
85+
"Set {.code drop = FALSE} to consider such groups for position ",
86+
"adjustment purposes."
87+
)))
88+
return(data_frame0())
89+
}
7590
ans <- data_frame0(x = data$x, n = nrow(data))
7691
return(ans)
7792
}
@@ -95,15 +110,15 @@ StatYdensity <- ggproto("StatYdensity", Stat,
95110

96111
compute_panel = function(self, data, scales, width = NULL, bw = "nrd0", adjust = 1,
97112
kernel = "gaussian", trim = TRUE, na.rm = FALSE,
98-
scale = "area", flipped_aes = FALSE) {
113+
scale = "area", flipped_aes = FALSE, drop = TRUE) {
99114
data <- flip_data(data, flipped_aes)
100115
data <- ggproto_parent(Stat, self)$compute_panel(
101116
data, scales, width = width, bw = bw, adjust = adjust, kernel = kernel,
102-
trim = trim, na.rm = na.rm
117+
trim = trim, na.rm = na.rm, drop = drop
103118
)
104-
if (any(data$n < 2)) {
119+
if (!drop && any(data$n < 2)) {
105120
cli::cli_warn(
106-
"Cannot compute density for groups with fewer than two data points."
121+
"Cannot compute density for groups with fewer than two datapoints."
107122
)
108123
}
109124

man/geom_violin.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)