Skip to content

Commit 7712c8d

Browse files
committed
fallback for 0 IQR data
1 parent 418d84d commit 7712c8d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

R/stat-density-2d.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,17 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
181181

182182
compute_group = function(data, scales, na.rm = FALSE, h = NULL, adjust = c(1, 1),
183183
n = 100, ...) {
184+
184185
if (is.null(h)) {
186+
# Note: MASS::bandwidth.nrd is equivalent to stats::bw.nrd * 4
185187
h <- c(MASS::bandwidth.nrd(data$x), MASS::bandwidth.nrd(data$y))
188+
# Handle case when when IQR == 0 and thus regular nrd bandwidth fails
189+
if (h[1] == 0) {
190+
h[1] <- bw.nrd0(data$x) * 4
191+
}
192+
if (h[2] == 0) {
193+
h[2] <- bw.nrd0(data$y) * 4
194+
}
186195
h <- h * adjust
187196
}
188197
if (any(is.na(h) | h <= 0)) {

0 commit comments

Comments
 (0)