Skip to content

annotate() gives warning when passing position or stat #5746

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 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Patterns and gradients are now also enabled in `geom_sf()`
(@teunbrand, #5716).
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
* `annotate()` now warns about `stat` or `position` arguments (@teunbrand, #5151)

# ggplot2 3.5.0

Expand Down
15 changes: 11 additions & 4 deletions R/annotation.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
}

data <- data_frame0(!!!position, .size = n)

params <- list2(na.rm = na.rm, ...)
reject <- intersect(names(params), c("position", "stat"))
if (length(reject) > 0) {
cli::cli_warn(
"{.fn annotate} can't accept {.or {.arg {reject}}} argument{?s}."
)
params <- params[setdiff(names(params), reject)]
}

layer(
geom = geom,
params = list(
na.rm = na.rm,
...
),
params = params,
stat = StatIdentity,
position = PositionIdentity,
data = data,
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/annotate.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@
Using the `size` aesthetic in this geom was deprecated in ggplot2 3.5.0.
i Please use `linewidth` instead.

# annotate() warns about `stat` or `position` arguments

`annotate()` can't accept `stat` or `position` arguments.

6 changes: 6 additions & 0 deletions tests/testthat/test-annotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ test_that("annotate() checks aesthetic lengths match", {
test_that("annotation_logticks warns about deprecated `size` argument", {
expect_snapshot_warning(annotation_logticks(size = 5))
})

test_that("annotate() warns about `stat` or `position` arguments", {
expect_snapshot_warning(
annotate("point", 1:3, 1:3, stat = "density", position = "dodge")
)
})