Skip to content

Commit 9bd2d18

Browse files
authored
Warn on unsupported geoms in annotate() (#4721)
Fixes #4719
1 parent cf3d141 commit 9bd2d18

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `annotate()` now documents unsupported geoms (`geom_abline()`, `geom_hline()`
4+
and `geom_vline()`), and warns when they are requested (@mikmart, #4719)
5+
36
* `presidential` dataset now includes Trump's presidency (@bkmgit, #4703).
47

58
* referring to `x` in backquoted expressions with `label_bquote()` is no longer

R/annotation.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#' set. This means that layers created with this function will never
1212
#' affect the legend.
1313
#'
14+
#' @section Unsupported geoms:
15+
#' Due to their special nature, reference line geoms [geom_abline()],
16+
#' [geom_hline()], and [geom_vline()] can't be used with [annotate()].
17+
#' You can use these geoms directory for annotations.
1418
#' @param geom name of geom to use for annotation
1519
#' @param x,y,xmin,ymin,xmax,ymax,xend,yend positioning aesthetics -
1620
#' you must specify at least one of these.
@@ -38,6 +42,13 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
3842
ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ...,
3943
na.rm = FALSE) {
4044

45+
if (geom %in% c("abline", "hline", "vline")) {
46+
warn(c(
47+
glue("`annotate()` does not support `geom = \"{geom}\"`."),
48+
i = glue("Please use `geom_{geom}()` directly instead.")
49+
))
50+
}
51+
4152
position <- compact(list(
4253
x = x, xmin = xmin, xmax = xmax, xend = xend,
4354
y = y, ymin = ymin, ymax = ymax, yend = yend

man/annotate.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/annotate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# unsupported geoms signal a warning (#4719)
2+
3+
`annotate()` does not support `geom = "hline"`.
4+
i Please use `geom_hline()` directly instead.
5+

tests/testthat/test-annotate.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ test_that("annotation_* has dummy data assigned and don't inherit aes", {
4848
expect_false(map$inherit.aes)
4949
expect_false(raster$inherit.aes)
5050
})
51+
52+
test_that("unsupported geoms signal a warning (#4719)", {
53+
expect_snapshot_warning(annotate("hline", yintercept = 0))
54+
})

0 commit comments

Comments
 (0)