Skip to content

Commit a11e098

Browse files
Deprecate guides(... = FALSE) (#4109)
Fix #4097
1 parent 9c66494 commit a11e098

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
* Only drop groups in `stat_ydensity()` when there are fewer than two data points and throw a warning (@andrewwbutler, #4111).
99

10+
* It is now deprecated to specify `guides(<scale> = FALSE)` or
11+
`scale_*(guide = FALSE)` to remove a guide. Please use
12+
`guides(<scale> = "none")` or `scale_*(guide = "none")` instead
13+
(@yutannihilation, #4094).
14+
1015
# ggplot2 3.3.2
1116
This is a small release focusing on fixing regressions introduced in 3.3.1.
1217

R/guides-.r

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ guides <- function(...) {
6969
if (is.list(args[[1]]) && !inherits(args[[1]], "guide")) args <- args[[1]]
7070
args <- rename_aes(args)
7171
}
72+
73+
idx_false <- vapply(args, isFALSE, FUN.VALUE = logical(1L))
74+
if (isTRUE(any(idx_false))) {
75+
warn('`guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> = "none")` instead.')
76+
args[idx_false] <- "none"
77+
}
78+
7279
structure(args, class = "guides")
7380
}
7481

@@ -190,10 +197,12 @@ guides_train <- function(scales, theme, guides, labels) {
190197
# + guides(XXX) > + scale_ZZZ(guide=XXX) > default(i.e., legend)
191198
guide <- resolve_guide(output, scale, guides)
192199

193-
# this should be changed to testing guide == "none"
194-
# scale$legend is backward compatibility
195-
# if guides(XXX=FALSE), then scale_ZZZ(guides=XXX) is discarded.
196-
if (identical(guide, "none") || isFALSE(guide) || inherits(guide, "guide_none")) next
200+
if (identical(guide, "none") || inherits(guide, "guide_none")) next
201+
202+
if (isFALSE(guide)) {
203+
warn('It is deprecated to specify `guide = FALSE` to remove a guide. Please use `guide = "none"` instead.')
204+
next
205+
}
197206

198207
# check the validity of guide.
199208
# if guide is character, then find the guide object

tests/testthat/test-guides.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,17 @@ test_that("coloursteps guide can be styled correctly", {
524524
p + guides(colour = guide_coloursteps(ticks = TRUE))
525525
)
526526
})
527+
528+
test_that("a warning is generated when guides(<scale> = FALSE) is specified", {
529+
df <- data_frame(x = c(1, 2, 4),
530+
y = c(6, 5, 7))
531+
532+
# warn on guide(<scale> = FALSE)
533+
expect_warning(g <- guides(colour = FALSE), "`guides(<scale> = FALSE)` is deprecated.", fixed = TRUE)
534+
expect_equal(g[["colour"]], "none")
535+
536+
# warn on scale_*(guide = FALSE)
537+
p <- ggplot(df, aes(x, y, colour = x)) + scale_colour_continuous(guide = FALSE)
538+
built <- expect_silent(ggplot_build(p))
539+
expect_warning(ggplot_gtable(built), "It is deprecated to specify `guide = FALSE`")
540+
})

0 commit comments

Comments
 (0)