Skip to content

'geom_rug()' prints a warning when 'na.rm = FALSE' #5906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Suggests:
rpart,
sf (>= 0.7-3),
svglite (>= 2.1.2),
testthat (>= 3.1.2),
testthat (>= 3.1.5),
vdiffr (>= 1.0.6),
xml2
Enhances:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* `geom_rug()` prints a warning when `na.rm = FALSE`, as per documentation (@pn317, #5905)
* `position_dodge(preserve = "single")` now handles multi-row geoms better,
such as `geom_violin()` (@teunbrand based on @clauswilke's work, #2801).
* `position_jitterdodge()` now dodges by `group` (@teunbrand, #3656)
Expand Down
41 changes: 40 additions & 1 deletion R/geom-rug.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,44 @@ GeomRug <- ggproto("GeomRug", Geom,

draw_key = draw_key_path,

rename_size = TRUE
rename_size = TRUE,

handle_na = function(self, data, params) {
sides_aes <- character()

if (grepl("b|t", params$sides)) {
sides_aes <- c(sides_aes, "x")
}

if (grepl("l|r", params$sides)) {
sides_aes <- c(sides_aes, "y")
}

if (length(sides_aes) > 0) {
df_list <- lapply(
sides_aes,
function(axis) {
remove_missing(
data, params$na.rm,
c(axis, self$required_aes, self$non_missing_aes),
snake_class(self)
)
}
)
data <- switch(
paste0(sides_aes, collapse = ""),
"x" = ,
"y" = df_list[[1]],
"xy" = vctrs::vec_set_union(df_list[[1]], df_list[[2]])
)
} else {
data <- remove_missing(
data, params$na.rm,
c(self$required_aes, self$non_missing_aes),
snake_class(self)
)
}

data
}
)
18 changes: 18 additions & 0 deletions tests/testthat/test-geom-rug.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ test_that("Rug lengths are correct", {

})

test_that(
"geom_rug() warns about missing values when na.rm = FALSE",
{
df2 <- df
n_missing <- 2
df2$x[sample(nrow(df2), size = n_missing)] <- NA

p1 <- ggplot(df2, aes(x = x)) + geom_rug()
p2 <- ggplot(df2, aes(x = x)) + geom_rug(na.rm = TRUE)

expect_warning(
ggplotGrob(p1),
paste0("Removed ", n_missing, " rows containing missing values or values outside the scale range")
)

expect_no_warning(ggplotGrob(p2))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be accompanied by a bump of the suggested {testthat} version to 3.1.5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}
)
Loading