Skip to content

Warn on unsupported geoms in annotate() #4721

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
merged 10 commits into from
Mar 15, 2022
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `annotate()` now documents unsupported geoms (`geom_abline()`, `geom_hline()`
and `geom_vline()`), and warns when they are requested (@mikmart, #4719)

* `presidential` dataset now includes Trump's presidency (@bkmgit, #4703).

* referring to `x` in backquoted expressions with `label_bquote()` is no longer
Expand Down
11 changes: 11 additions & 0 deletions R/annotation.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#' set. This means that layers created with this function will never
#' affect the legend.
#'
#' @section Unsupported geoms:
#' Due to their special nature, reference line geoms [geom_abline()],
#' [geom_hline()], and [geom_vline()] can't be used with [annotate()].
#' You can use these geoms directory for annotations.
#' @param geom name of geom to use for annotation
#' @param x,y,xmin,ymin,xmax,ymax,xend,yend positioning aesthetics -
#' you must specify at least one of these.
Expand Down Expand Up @@ -38,6 +42,13 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ...,
na.rm = FALSE) {

if (geom %in% c("abline", "hline", "vline")) {
warn(c(
glue("`annotate()` does not support `geom = \"{geom}\"`."),
i = glue("Please use `geom_{geom}()` directly instead.")
))
}

position <- compact(list(
x = x, xmin = xmin, xmax = xmax, xend = xend,
y = y, ymin = ymin, ymax = ymax, yend = yend
Expand Down
10 changes: 10 additions & 0 deletions man/annotate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/_snaps/annotate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# unsupported geoms signal a warning (#4719)

`annotate()` does not support `geom = "hline"`.
i Please use `geom_hline()` directly instead.

4 changes: 4 additions & 0 deletions tests/testthat/test-annotate.r
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ test_that("annotation_* has dummy data assigned and don't inherit aes", {
expect_false(map$inherit.aes)
expect_false(raster$inherit.aes)
})

test_that("unsupported geoms signal a warning (#4719)", {
expect_snapshot_warning(annotate("hline", yintercept = 0))
})