Skip to content

Handle fine-grained show.legend properly #3490

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
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: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

* `geom_sf()` now removes rows that contain missing `shape`/`size`/`colour` (#3483, @yutannihilation)

* Fix a bug when `show.legend` is a named logical vector (#3461, @yutannihilation).

# ggplot2 3.2.1

This is a patch release fixing a few regressions introduced in 3.2.0 as well as
Expand Down
22 changes: 20 additions & 2 deletions R/guide-colorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,28 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) {
guide_layers <- lapply(layers, function(layer) {
matched <- matched_aes(layer, guide, default_mapping)

if (length(matched) > 0 && (isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend))) {
if (length(matched) == 0) {
# This layer does not use this guide
return(NULL)
}

# check if this layer should be included, different behaviour depending on
# if show.legend is a logical or a named logical vector
if (is_named(layer$show.legend)) {
layer$show.legend <- rename_aes(layer$show.legend)
show_legend <- layer$show.legend[matched]
# we cannot use `isTRUE(is.na(show_legend))` here because
# 1. show_legend can be multiple NAs
# 2. isTRUE() was not tolerant for a named TRUE
show_legend <- show_legend[!is.na(show_legend)]
include <- length(show_legend) == 0 || any(show_legend)
Copy link
Member

Choose a reason for hiding this comment

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

Why does an empty show_legend lead to inclusion here?

Copy link
Member Author

Choose a reason for hiding this comment

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

iiuc, guide_geom() shows a legend if

  1. the layer's mapping contains any of the corresponding aes and
  2. the user does not explicitly choose not to show the legend

So, the absence of the corresponding show.legend should be translated as inclusion.

Does this answer your question? Sorry, I might not get your point....

Copy link
Member

Choose a reason for hiding this comment

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

You are right - I'm probably just tired

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Sorry for making you busy with reviewing pull requests :)

} else {
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
}

if (include) {
layer
} else {
# This layer does not use this guide
NULL
}
})
Expand Down
10 changes: 7 additions & 3 deletions R/guide-legend.r
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,14 @@ guide_geom.legend <- function(guide, layers, default_mapping) {

# check if this layer should be included, different behaviour depending on
# if show.legend is a logical or a named logical vector
if (!is.null(names(layer$show.legend))) {
if (is_named(layer$show.legend)) {
layer$show.legend <- rename_aes(layer$show.legend)
include <- is.na(layer$show.legend[matched]) ||
layer$show.legend[matched]
show_legend <- layer$show.legend[matched]
# we cannot use `isTRUE(is.na(show_legend))` here because
# 1. show_legend can be multiple NAs
# 2. isTRUE() was not tolerant for a named TRUE
show_legend <- show_legend[!is.na(show_legend)]
include <- length(show_legend) == 0 || any(show_legend)
} else {
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
}
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ test_that("show.legend handles named vectors", {
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20, show.legend = c(color = FALSE, shape = FALSE))
expect_equal(n_legends(p), 0)

# c.f.https://github.com/tidyverse/ggplot2/issues/3461
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20, show.legend = c(shape = FALSE, color = TRUE))
expect_equal(n_legends(p), 1)
})

test_that("axis_label_overlap_priority always returns the correct number of elements", {
Expand Down