Skip to content

Commit e635262

Browse files
authored
'geom_rug()' prints a warning when 'na.rm = FALSE' (#5906)
* 'geom_rug()' prints a warning when 'na.rm = FALSE' Fixes issue #5905. When presented with missing values, 'geom_rug()' was not printing a warning message, contrary to the documentation. A warning message is now printed when 'na.rm = FALSE' ad suppressed when 'na.rm = TRUE', as expected. * Impement local changes in 'handle_na()' Make local changes in 'handle_na() instead of global changes to 'GeomRug' * Test for 'geom_rug()' warning about missing values Also added the issue number to NEWS * Handle rugs in orthogonal directions better When plotting rugs in both the 'x' and 'y' direction simultaneously, values of 'x' were being dropped when 'y' was missing, and vice versa. A warning will be given for each axis ('x' or 'y') that contains missing values, if 'na.rm = FALSE'. * Remove dependence on 'dplyr' Use 'vctrs::vec_set_union()' instead of 'dplyr::union()' * Update 'testthat' version to 3.1.5 * Add github handle to NEWS bullet
1 parent 78660a9 commit e635262

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Suggests:
6767
rpart,
6868
sf (>= 0.7-3),
6969
svglite (>= 2.1.2),
70-
testthat (>= 3.1.2),
70+
testthat (>= 3.1.5),
7171
vdiffr (>= 1.0.6),
7272
xml2
7373
Enhances:

NEWS.md

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

3+
* `geom_rug()` prints a warning when `na.rm = FALSE`, as per documentation (@pn317, #5905)
34
* `position_dodge(preserve = "single")` now handles multi-row geoms better,
45
such as `geom_violin()` (@teunbrand based on @clauswilke's work, #2801).
56
* `position_jitterdodge()` now dodges by `group` (@teunbrand, #3656)

R/geom-rug.R

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,44 @@ GeomRug <- ggproto("GeomRug", Geom,
157157

158158
draw_key = draw_key_path,
159159

160-
rename_size = TRUE
160+
rename_size = TRUE,
161+
162+
handle_na = function(self, data, params) {
163+
sides_aes <- character()
164+
165+
if (grepl("b|t", params$sides)) {
166+
sides_aes <- c(sides_aes, "x")
167+
}
168+
169+
if (grepl("l|r", params$sides)) {
170+
sides_aes <- c(sides_aes, "y")
171+
}
172+
173+
if (length(sides_aes) > 0) {
174+
df_list <- lapply(
175+
sides_aes,
176+
function(axis) {
177+
remove_missing(
178+
data, params$na.rm,
179+
c(axis, self$required_aes, self$non_missing_aes),
180+
snake_class(self)
181+
)
182+
}
183+
)
184+
data <- switch(
185+
paste0(sides_aes, collapse = ""),
186+
"x" = ,
187+
"y" = df_list[[1]],
188+
"xy" = vctrs::vec_set_union(df_list[[1]], df_list[[2]])
189+
)
190+
} else {
191+
data <- remove_missing(
192+
data, params$na.rm,
193+
c(self$required_aes, self$non_missing_aes),
194+
snake_class(self)
195+
)
196+
}
197+
198+
data
199+
}
161200
)

tests/testthat/test-geom-rug.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,21 @@ test_that("Rug lengths are correct", {
4040

4141
})
4242

43+
test_that(
44+
"geom_rug() warns about missing values when na.rm = FALSE",
45+
{
46+
df2 <- df
47+
n_missing <- 2
48+
df2$x[sample(nrow(df2), size = n_missing)] <- NA
49+
50+
p1 <- ggplot(df2, aes(x = x)) + geom_rug()
51+
p2 <- ggplot(df2, aes(x = x)) + geom_rug(na.rm = TRUE)
52+
53+
expect_warning(
54+
ggplotGrob(p1),
55+
paste0("Removed ", n_missing, " rows containing missing values or values outside the scale range")
56+
)
57+
58+
expect_no_warning(ggplotGrob(p2))
59+
}
60+
)

0 commit comments

Comments
 (0)