Skip to content
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)

* `position_stack()` now works fully with `geom_text()` (@thomasp85, #4367)

* `geom_tile()` now correctly recognises missing data in `xmin`, `xmax`, `ymin`,
Expand Down
10 changes: 10 additions & 0 deletions R/annotation.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#' 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()].
Comment thread
mikmart marked this conversation as resolved.
Outdated
#' For annotating reference lines, they should be used directly, instead.
#'
#' @seealso [geom_abline()] for annotating reference lines.
Comment thread
mikmart marked this conversation as resolved.
Outdated
#' @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 +44,10 @@ 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(glue("`annotate()` does not support `geom = \"{geom}\"`. Please use `geom_{geom}()` directly, instead."))
Comment thread
mikmart marked this conversation as resolved.
Outdated
}

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.

6 changes: 6 additions & 0 deletions tests/testthat/test-annotate.r
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ 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_warning(annotate("abline", slope = 1), "not support")
Comment thread
mikmart marked this conversation as resolved.
Outdated
expect_warning(annotate("hline", yintercept = 0), "not support")
expect_warning(annotate("vline", xintercept = 0), "not support")
})