Skip to content

Commit c5a9c68

Browse files
authored
facet_wrap(drop = TRUE) can handle character variables with NAs (#5847)
* `ulevels()` only drops character NAs on request * add test * add news bullet
1 parent 09bcda6 commit c5a9c68

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

NEWS.md

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

3+
* Prevented `facet_wrap(..., drop = FALSE)` from throwing spurious errors when
4+
a character facetting variable contained `NA`s (@teunbrand, #5485).
5+
* When facets coerce the faceting variables to factors, the 'ordered' class
6+
is dropped (@teunbrand, #5666).
37
* `geom_curve()` now appropriately removes missing data instead of throwing
48
errors (@teunbrand, #5831).
59
* `update_geom_defaults()` and `update_stat_defaults()` have a reset mechanism

R/facet-grid-.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ FacetGrid <- ggproto("FacetGrid", Facet,
511511

512512
# Helpers -----------------------------------------------------------------
513513

514-
ulevels <- function(x) {
514+
ulevels <- function(x, na.last = TRUE) {
515515
if (is.factor(x)) {
516516
x <- addNA(x, TRUE)
517517
factor(levels(x), levels(x), exclude = NULL)
518518
} else {
519-
sort(unique0(x))
519+
sort(unique0(x), na.last = na.last)
520520
}
521521
}

R/stat-summary-2d.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ StatSummary2d <- ggproto("StatSummary2d", Stat,
126126

127127
# Adaptation of tapply that returns a data frame instead of a matrix
128128
tapply_df <- function(x, index, fun, ..., drop = TRUE) {
129-
labels <- lapply(index, ulevels)
129+
labels <- lapply(index, ulevels, na.last = NA) # drop NA
130130
out <- expand.grid(labels, KEEP.OUT.ATTRS = FALSE, stringsAsFactors = FALSE)
131131

132132
grps <- split(x, index)

tests/testthat/test-facet-layout.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ test_that("grid: as.table reverses rows", {
100100

101101
a2 <- data_frame(
102102
a = factor(1:3, levels = 1:4),
103-
b = factor(1:3, levels = 4:1)
103+
b = factor(1:3, levels = 4:1),
104+
c = as.character(c(1:2, NA))
104105
)
105106

106107
test_that("wrap: drop = FALSE preserves unused levels", {
@@ -111,6 +112,11 @@ test_that("wrap: drop = FALSE preserves unused levels", {
111112
wrap_b <- panel_layout(facet_wrap(~b, drop = FALSE), list(a2))
112113
expect_equal(nrow(wrap_b), 4)
113114
expect_equal(as.character(wrap_b$b), as.character(4:1))
115+
116+
# NA character should not be dropped or throw errors #5485
117+
wrap_c <- panel_layout(facet_wrap(~c, drop = FALSE), list(a2))
118+
expect_equal(nrow(wrap_c), 3)
119+
expect_equal(wrap_c$c, a2$c)
114120
})
115121

116122
test_that("grid: drop = FALSE preserves unused levels", {

0 commit comments

Comments
 (0)