Skip to content
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

Smarter keys (attempt 2) #5502

Merged
merged 9 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* By default, `guide_legend()` now only draws a key glyph for a layer when
the value is is the layer's data. To revert to the old behaviour, you
can still set `show.legend = c({aesthetic} = TRUE)` (@teunbrand, #3648).

* `ggsave()` no longer sometimes creates new directories, which is now
controlled by the new `create.dir` argument (#5489).

Expand Down
47 changes: 44 additions & 3 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ GuideLegend <- ggproto(

get_layer_key = function(params, layers, data) {

decor <- lapply(layers, function(layer) {
decor <- Map(layer = layers, df = data, f = function(layer, df) {

matched_aes <- matched_aes(layer, params)

Expand All @@ -324,9 +324,10 @@ GuideLegend <- ggproto(
"Failed to apply {.fn after_scale} modifications to legend",
parent = cnd
)
layer$geom$use_defaults(params$key[matched], layer_params, list())
layer$geom$use_defaults(params$key[matched_aes], layer_params, list())
}
)
data$.draw <- keep_key_data(params$key, df, matched_aes, layer$show.legend)
} else {
reps <- rep(1, nrow(params$key))
data <- layer$geom$use_defaults(NULL, layer$aes_params)[reps, ]
Expand Down Expand Up @@ -469,7 +470,12 @@ GuideLegend <- ggproto(
draw <- function(i) {
bg <- elements$key
keys <- lapply(decor, function(g) {
g$draw_key(vec_slice(g$data, i), g$params, key_size)
data <- vec_slice(g$data, i)
if (data$.draw %||% TRUE) {
g$draw_key(data, g$params, key_size)
} else {
zeroGrob()
}
})
c(list(bg), keys)
}
Expand Down Expand Up @@ -762,3 +768,38 @@ measure_legend_keys <- function(decor, n, dim, byrow = FALSE,
heights = pmax(default_height, apply(size, 1, max))
)
}

# For legend keys, check if the guide key's `.value` also occurs in the layer
# data when `show.legend = NA` and data is discrete. Note that `show.legend`
# besides TRUE (always show), FALSE (never show) and NA (show in relevant legend),
# can also take *named* logical vector to set this behaviour per aesthetic.
keep_key_data <- function(key, data, aes, show) {
# First, can we exclude based on anything else than actually checking the
# data that we should include or drop the key?
if (!is.discrete(key$.value)) {
return(TRUE)
}
if (is_named(show)) {
aes <- intersect(aes, names(show))
show <- show[aes]
} else {
show <- show[rep(1L, length(aes))]
}
if (isTRUE(any(show)) || length(show) == 0) {
return(TRUE)
}
if (isTRUE(all(!show))) {
return(FALSE)
}
# Second, we go find if the value is actually present in the data.
aes <- aes[is.na(show)]
match <- which(names(data) %in% aes)
if (length(match) == 0) {
return(TRUE)
}
keep <- rep(FALSE, nrow(key))
for (column in match) {
keep <- keep | vec_in(key$.value, data[[column]])
}
keep
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/testthat/test-draw-key.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,42 @@ test_that("horizontal key glyphs work", {
guides(color = guide_legend(order = 1))
)
})

test_that("keep_draw_key", {

key <- data_frame0(.value = c("A", "C"))
data <- data_frame0(foo = c("A", "B"), bar = c("B", "C"))

expect_true( keep_key_data(key, data, "foo", show = TRUE))
expect_false(keep_key_data(key, data, "foo", show = FALSE))
expect_equal(keep_key_data(key, data, "foo", show = NA), c(TRUE, FALSE))
expect_equal(keep_key_data(key, data, "bar", show = NA), c(FALSE, TRUE))
expect_equal(keep_key_data(key, data, c("foo", "bar"), show = NA), c(TRUE, TRUE))

# Named show
expect_true(
keep_key_data(key, data, c("foo", "bar"), show = c(foo = TRUE, bar = FALSE))
)
expect_equal(
keep_key_data(key, data, c("foo", "bar"), show = c(foo = NA, bar = FALSE)),
c(TRUE, FALSE)
)
expect_equal(
keep_key_data(key, data, c("foo", "bar"), show = c(foo = FALSE, bar = NA)),
c(FALSE, TRUE)
)

p <- ggplot(data.frame(x = 1:2), aes(x, x)) +
geom_point(
aes(colour = "point", alpha = "point"),
show.legend = c("colour" = NA, alpha = FALSE)
) +
geom_line(
aes(colour = "line", alpha = "line"),
show.legend = c("colour" = NA, alpha = TRUE)
) +
suppressWarnings(scale_alpha_discrete())

expect_doppelganger("appropriate colour key with alpha key as lines", p)

})