Skip to content

Commit

Permalink
Deprecate guides(... = FALSE) (#4109)
Browse files Browse the repository at this point in the history
Fix #4097
  • Loading branch information
yutannihilation committed Jul 6, 2020
1 parent 9c66494 commit a11e098
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

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

* It is now deprecated to specify `guides(<scale> = FALSE)` or
`scale_*(guide = FALSE)` to remove a guide. Please use
`guides(<scale> = "none")` or `scale_*(guide = "none")` instead
(@yutannihilation, #4094).

# ggplot2 3.3.2
This is a small release focusing on fixing regressions introduced in 3.3.1.

Expand Down
17 changes: 13 additions & 4 deletions R/guides-.r
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ guides <- function(...) {
if (is.list(args[[1]]) && !inherits(args[[1]], "guide")) args <- args[[1]]
args <- rename_aes(args)
}

idx_false <- vapply(args, isFALSE, FUN.VALUE = logical(1L))
if (isTRUE(any(idx_false))) {
warn('`guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> = "none")` instead.')
args[idx_false] <- "none"
}

structure(args, class = "guides")
}

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

# this should be changed to testing guide == "none"
# scale$legend is backward compatibility
# if guides(XXX=FALSE), then scale_ZZZ(guides=XXX) is discarded.
if (identical(guide, "none") || isFALSE(guide) || inherits(guide, "guide_none")) next
if (identical(guide, "none") || inherits(guide, "guide_none")) next

if (isFALSE(guide)) {
warn('It is deprecated to specify `guide = FALSE` to remove a guide. Please use `guide = "none"` instead.')
next
}

# check the validity of guide.
# if guide is character, then find the guide object
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,17 @@ test_that("coloursteps guide can be styled correctly", {
p + guides(colour = guide_coloursteps(ticks = TRUE))
)
})

test_that("a warning is generated when guides(<scale> = FALSE) is specified", {
df <- data_frame(x = c(1, 2, 4),
y = c(6, 5, 7))

# warn on guide(<scale> = FALSE)
expect_warning(g <- guides(colour = FALSE), "`guides(<scale> = FALSE)` is deprecated.", fixed = TRUE)
expect_equal(g[["colour"]], "none")

# warn on scale_*(guide = FALSE)
p <- ggplot(df, aes(x, y, colour = x)) + scale_colour_continuous(guide = FALSE)
built <- expect_silent(ggplot_build(p))
expect_warning(ggplot_gtable(built), "It is deprecated to specify `guide = FALSE`")
})

0 comments on commit a11e098

Please sign in to comment.