Skip to content

Commit 9c66494

Browse files
Bring stat_ydensity behavior in line with stat_density (#4112)
Fix #4111 * stat_ydensity drop groups with fewer than 2 + warn This change is intended to bring behavior of stat_ydensity in line with stat_density. Previously dropped with fewer than 3, now drops with fewer than 2 and will display a warning message when dropping.
1 parent 97a5f6a commit 9c66494

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
* Updated style for example code (@rjake, #4092)
77

8+
* Only drop groups in `stat_ydensity()` when there are fewer than two data points and throw a warning (@andrewwbutler, #4111).
9+
810
# ggplot2 3.3.2
911
This is a small release focusing on fixing regressions introduced in 3.3.1.
1012

R/stat-ydensity.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ StatYdensity <- ggproto("StatYdensity", Stat,
7171

7272
compute_group = function(data, scales, width = NULL, bw = "nrd0", adjust = 1,
7373
kernel = "gaussian", trim = TRUE, na.rm = FALSE, flipped_aes = FALSE) {
74-
if (nrow(data) < 3) return(new_data_frame())
74+
if (nrow(data) < 2) {
75+
warn("Groups with fewer than two data points have been dropped.")
76+
return(new_data_frame())
77+
}
7578
range <- range(data$y, na.rm = TRUE)
7679
modifier <- if (trim) 0 else 3
7780
bw <- calc_bw(data$y, bw)

0 commit comments

Comments
 (0)