Skip to content

Commit 87a76e8

Browse files
authored
annotate() gives warning when passing position or stat (#5746)
* `annotate()` warns about `position` and `stat` args * add tests * add news bullet
1 parent 1fd0c7b commit 87a76e8

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Patterns and gradients are now also enabled in `geom_sf()`
1111
(@teunbrand, #5716).
1212
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
13+
* `annotate()` now warns about `stat` or `position` arguments (@teunbrand, #5151)
1314

1415
# ggplot2 3.5.0
1516

R/annotation.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,19 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
7474
}
7575

7676
data <- data_frame0(!!!position, .size = n)
77+
78+
params <- list2(na.rm = na.rm, ...)
79+
reject <- intersect(names(params), c("position", "stat"))
80+
if (length(reject) > 0) {
81+
cli::cli_warn(
82+
"{.fn annotate} can't accept {.or {.arg {reject}}} argument{?s}."
83+
)
84+
params <- params[setdiff(names(params), reject)]
85+
}
86+
7787
layer(
7888
geom = geom,
79-
params = list(
80-
na.rm = na.rm,
81-
...
82-
),
89+
params = params,
8390
stat = StatIdentity,
8491
position = PositionIdentity,
8592
data = data,

tests/testthat/_snaps/annotate.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@
3434
Using the `size` aesthetic in this geom was deprecated in ggplot2 3.5.0.
3535
i Please use `linewidth` instead.
3636

37+
# annotate() warns about `stat` or `position` arguments
38+
39+
`annotate()` can't accept `stat` or `position` arguments.
40+

tests/testthat/test-annotate.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ test_that("annotate() checks aesthetic lengths match", {
8080
test_that("annotation_logticks warns about deprecated `size` argument", {
8181
expect_snapshot_warning(annotation_logticks(size = 5))
8282
})
83+
84+
test_that("annotate() warns about `stat` or `position` arguments", {
85+
expect_snapshot_warning(
86+
annotate("point", 1:3, 1:3, stat = "density", position = "dodge")
87+
)
88+
})

0 commit comments

Comments
 (0)