Skip to content

Commit ba5c090

Browse files
authored
respect show.legend in colorbar (#1782)
Fixes #1568 * Add guide_geom.colorbar method and refactored
1 parent 77343bd commit ba5c090

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
* `scale_*_datetime` now has support for timezones. If time data has been
4343
encoded with a timezone this will be used, but it can be overridden with the
4444
`timezone` argument in the scale constructor.
45+
46+
* `geom_*(show.legend = FALSE)` now works for `guide_colorbar`
4547

4648
* The documentation for theme elements has been improved (#1743).
4749

R/guide-colorbar.r

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,22 @@ guide_merge.colorbar <- function(guide, new_guide) {
211211

212212
# this guide is not geom-based.
213213
#' @export
214-
guide_geom.colorbar <- function(guide, ...) {
214+
guide_geom.colorbar <- function(guide, layers, default_mapping) {
215+
# Layers that use this guide
216+
guide_layers <- plyr::llply(layers, function(layer) {
217+
matched <- matched_aes(layer, guide, default_mapping)
218+
219+
if (length(matched) && ((is.na(layer$show.legend) || layer$show.legend))) {
220+
layer
221+
} else {
222+
# This layer does not use this guide
223+
NULL
224+
}
225+
})
226+
227+
# Remove this guide if no layer uses it
228+
if (length(compact(guide_layers)) == 0) guide <- NULL
229+
215230
guide
216231
}
217232

R/guide-legend.r

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,7 @@ guide_merge.legend <- function(guide, new_guide) {
252252
guide_geom.legend <- function(guide, layers, default_mapping) {
253253
# arrange common data for vertical and horizontal guide
254254
guide$geoms <- plyr::llply(layers, function(layer) {
255-
all <- names(c(layer$mapping, if (layer$inherit.aes) default_mapping, layer$stat$default_aes))
256-
geom <- c(layer$geom$required_aes, names(layer$geom$default_aes))
257-
matched <- intersect(intersect(all, geom), names(guide$key))
258-
matched <- setdiff(matched, names(layer$geom_params))
259-
matched <- setdiff(matched, names(layer$aes_params))
255+
matched <- matched_aes(layer, guide, default_mapping)
260256

261257
if (length(matched) > 0) {
262258
# This layer contributes to the legend

R/guides-.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,12 @@ guide_merge <- function(...) UseMethod("guide_merge")
297297
guide_geom <- function(...) UseMethod("guide_geom")
298298

299299
guide_gengrob <- function(...) UseMethod("guide_gengrob")
300+
301+
# Helpers
302+
matched_aes <- function(layer, guide, defaults) {
303+
all <- names(c(layer$mapping, if (layer$inherit.aes) defaults, layer$stat$default_aes))
304+
geom <- c(layer$geom$required_aes, names(layer$geom$default_aes))
305+
matched <- intersect(intersect(all, geom), names(guide$key))
306+
matched <- setdiff(matched, names(layer$geom_params))
307+
setdiff(matched, names(layer$aes_params))
308+
}

tests/testthat/test-guides.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ test_that("colourbar trains without labels", {
88
expect_equal(names(out$key), c("colour", ".value"))
99
})
1010

11+
test_that("Colorbar respects show.legend in layer", {
12+
df <- data.frame(x = 1:3, y = 1)
13+
p <- ggplot(df, aes(x = x, y = y, color = x)) +
14+
geom_point(size = 20, shape = 21, show.legend = FALSE)
15+
expect_false("guide-box" %in% ggplotGrob(p)$layout$name)
16+
p <- ggplot(df, aes(x = x, y = y, color = x)) +
17+
geom_point(size = 20, shape = 21, show.legend = TRUE)
18+
expect_true("guide-box" %in% ggplotGrob(p)$layout$name)
19+
})

0 commit comments

Comments
 (0)