Skip to content

Commit c3dd767

Browse files
authored
Fix for position_dodge(preserve = "single") with location based data (#6000)
* fallback for location based data * Warn when x/xmin are absent
1 parent 6d48584 commit c3dd767

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

R/position-dodge.R

+9-2
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(
@@ -117,8 +122,10 @@ PositionDodge <- ggproto("PositionDodge", Position,
117122
if (identical(self$preserve, "total")) {
118123
n <- NULL
119124
} else {
120-
n <- vec_unique(data[c("group", "PANEL", "xmin")])
121-
n <- vec_group_id(n[c("PANEL", "xmin")])
125+
data$xmin <- data$xmin %||% data$x
126+
cols <- intersect(colnames(data), c("group", "PANEL", "xmin"))
127+
n <- vec_unique(data[cols])
128+
n <- vec_group_id(n[setdiff(cols, "group")])
122129
n <- max(tabulate(n, attr(n, "n")))
123130
}
124131

tests/testthat/test-position_dodge.R

+16
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)