Skip to content

Commit 7c59ba6

Browse files
authored
Improve error message when plot.title.position is not correctly mentioned. (#5296)
* Remove extra " * Update snapshot tests * Require the hexbin package for this test. * Remove the `call` arg I was trying. * Updating the error message with `arg_match0()` and update snapshot
1 parent f89212d commit 7c59ba6

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

R/plot-build.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,19 @@ ggplot_gtable.ggplot_built <- function(data) {
276276
# positioning of caption is governed by plot.caption.position
277277
# "panel" means align to the panel(s)
278278
# "plot" means align to the entire plot (except margins and tag)
279-
title_pos <- theme$plot.title.position %||% "panel"
280-
if (!(title_pos %in% c("panel", "plot"))) {
281-
cli::cli_abort('{.var plot.title.position} should be either {.val "panel"} or {.val "plot"}.')
282-
}
283-
caption_pos <- theme$plot.caption.position %||% "panel"
284-
if (!(caption_pos %in% c("panel", "plot"))) {
285-
cli::cli_abort('{.var plot.caption.position} should be either {.val "panel"} or {.val "plot"}.')
286-
}
279+
title_pos <- arg_match0(
280+
theme$plot.title.position %||% "panel",
281+
c("panel", "plot"),
282+
arg_nm = "plot.title.position",
283+
error_call = expr(theme())
284+
)
285+
286+
caption_pos <- arg_match0(
287+
theme$plot.caption.position %||% "panel",
288+
values = c("panel", "plot"),
289+
arg_nm = "plot.caption.position",
290+
error_call = expr(theme())
291+
)
287292

288293
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), , drop = FALSE]
289294
if (title_pos == "panel") {

tests/testthat/_snaps/theme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
# Theme elements are checked during build
4444

45-
`plot.title.position` should be either "\"panel\"" or "\"plot\"".
45+
`plot.title.position` must be one of "panel" or "plot", not "test".
4646

4747
---
4848

49-
`plot.caption.position` should be either "\"panel\"" or "\"plot\"".
49+
`plot.caption.position` must be one of "panel" or "plot", not "test".
5050

5151
---
5252

tests/testthat/test-stat-summary.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ test_that("stat_summary_(2d|hex) work with lambda expressions", {
6868

6969
# stat_summary_hex
7070
# this plot is a bit funky, but easy to reason through
71+
skip_if_not_installed("hexbin")
7172
p1 <- ggplot(dat, aes(x, y, z = z)) +
7273
stat_summary_hex(fun = function(x) mean(x))
7374

0 commit comments

Comments
 (0)