Skip to content

Commit 1a39648

Browse files
committed
Warn when x/xmin are absent
1 parent d8bd6d7 commit 1a39648

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

R/position-dodge.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ PositionDodge <- ggproto("PositionDodge", Position,
106106
reverse = NULL,
107107
setup_params = function(self, data) {
108108
flipped_aes <- has_flipped_aes(data, default = self$orientation == "y")
109+
check_required_aesthetics(
110+
if (flipped_aes) "y|ymin" else "x|xmin",
111+
names(data), snake_class(self)
112+
)
113+
109114
data <- flip_data(data, flipped_aes)
110115
if (is.null(data$xmin) && is.null(data$xmax) && is.null(self$width)) {
111116
cli::cli_warn(c(

tests/testthat/test-position_dodge.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ test_that("position_dodge() can reverse the dodge order", {
3737
ld <- get_layer_data(p + geom_col(position = position_dodge(reverse = FALSE)))
3838
expect_equal(ld$label[order(ld$x)], c("A", "A", "B", "B", "C"))
3939
})
40+
41+
test_that("position_dodge warns about missing required aesthetics", {
42+
43+
# Bit of a contrived geom to not have a required 'x' aesthetic
44+
GeomDummy <- ggproto(NULL, GeomPoint, required_aes = NULL, optional_aes = "x")
45+
46+
p <- ggplot(mtcars, aes(cyl, disp, colour = factor(vs))) +
47+
layer(
48+
geom = GeomDummy,
49+
stat = "identity",
50+
position = position_dodge(width = 0.5),
51+
mapping = aes(x = NULL)
52+
)
53+
54+
expect_error(ggplot_build(p), "requires the following missing aesthetics")
55+
})

0 commit comments

Comments
 (0)