Skip to content

Commit 15ce4a9

Browse files
authored
Allow NA values in position_dodge2 (#4408)
1 parent aafcac3 commit 15ce4a9

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Fix a bug in `position_dodge2()` where `NA` values in thee data would cause an
4+
error (@thomasp85, #2905)
5+
36
* Fix a bug in `position_jitter()` where different jitters would be applied to
47
different position aesthetics of the same axis (@thomasp85, #2941)
58

R/position-dodge2.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ find_x_overlaps <- function(df) {
138138
overlaps[1] <- counter <- 1
139139

140140
for (i in seq_asc(2, nrow(df))) {
141-
if (df$xmin[i] >= df$xmax[i - 1]) {
141+
if (is.na(df$xmin[i]) || is.na(df$xmax[i - 1]) || df$xmin[i] >= df$xmax[i - 1]) {
142142
counter <- counter + 1
143143
}
144144
overlaps[i] <- counter

tests/testthat/test-position-dodge2.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ test_that("width of groups is computed per facet", {
107107

108108
expect_true(all(width == (0.9 / 3) * 0.9))
109109
})
110+
111+
test_that("NA values are given their own group", {
112+
df <- data.frame(
113+
xmin = c(1, 2, NA, NA),
114+
xmax = c(1, 2, NA, NA)
115+
)
116+
expect_equal(find_x_overlaps(df), seq_len(4))
117+
})

0 commit comments

Comments
 (0)