Skip to content

Convert size = NA to 0 before rendering guide_legend #4672

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 1 commit into from
Nov 16, 2021
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Setting `size = NA` will no longer cause `guide_legend()` to error
(@thomasp85, #4559)

* Setting `stroke` to `NA` in `geom_point()` will no longer impair the sizing of
the points (@thomasp85, #4624)

Expand Down
5 changes: 5 additions & 0 deletions R/guide-legend.r
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
# override.aes in guide_legend manually changes the geom
data <- modify_list(data, guide$override.aes)

if (!is.null(data$size)) {
Copy link
Member

Choose a reason for hiding this comment

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

I forget if data is a data.frame here, but if it is, I wonder of "size" %in% names(data) is a bit better (lest data ever become a tibble)

Copy link
Member Author

Choose a reason for hiding this comment

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

We have this pattern all over the place so once we begin to accept tibbles inside the code we will have a lot of places to fix up

data$size[is.na(data$size)] <- 0
}

list(
draw_key = layer$geom$draw_key,
data = data,
Expand Down Expand Up @@ -383,6 +387,7 @@ guide_gengrob.legend <- function(guide, theme) {
)

key_size_mat <- do.call("cbind", lapply(guide$geoms, function(g) g$data$size / 10))

if (nrow(key_size_mat) == 0 || ncol(key_size_mat) == 0) {
key_size_mat <- matrix(0, ncol = 1, nrow = nbreak)
}
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ test_that("guide merging for guide_legend() works as expected", {
expect_equal(repeated_identical_labels[[1]]$key$.label, c("label1", "label1", "label2"))
})

test_that("size = NA doesn't throw rendering errors", {
df = data.frame(
x = c(1, 2),
group = c("a","b")
)
p <- ggplot(df, aes(x = x, y = 0, colour = group)) +
geom_point(size = NA, na.rm = TRUE)

expect_silent(plot(p))
})

# Visual tests ------------------------------------------------------------

test_that("axis guides are drawn correctly", {
Expand Down