Skip to content
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
* The `dir` argument of `facet_wrap()` is now documented with all
eight two-letter codes, clarifying their starting corner and fill
order (@CuiweiG, #6837).
* Fixed bug in `width` computation when `position_dodge(preserve = "single")`
had duplicated `order` aesthetic values (@teunbrand, #6775).

# ggplot2 4.0.3

Expand Down
5 changes: 3 additions & 2 deletions R/position-dodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ PositionDodge <- ggproto("PositionDodge", Position,
n <- NULL
} else {
data$xmin <- data$xmin %||% data$x
cols <- intersect(colnames(data), c("group", "PANEL", "xmin"))
cols <- intersect(colnames(data), c("PANEL", "xmin"))
cols <- c(cols, if ("order" %in% names(data)) "order" else "group")
n <- vec_unique(data[cols])
n <- vec_group_id(n[setdiff(cols, "group")])
n <- vec_group_id(n[setdiff(cols, c("group", "order"))])
n <- max(tabulate(n, attr(n, "n")))
}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-position-dodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ test_that("position_dodge() can use the order aesthetic", {
expect_equal(ld$x, major + c(-0.2, 0, 0.2)[minor], ignore_attr = TRUE)
})

test_that("position_dodge() with duplicated order computes the correct width", {
df <- data_frame0(
group = c("A", "B", "C"),
order = c(1, 2, 2)
)
ld <- layer_data(
ggplot(df, aes("X", 1, group = group, order = order)) +
geom_col(position = position_dodge(preserve = "single", width = 1), width = 1)
)
expect_all_equal(ld$xmax - ld$xmin, 0.5)
})

test_that("position_dodge warns about missing required aesthetics", {

# Bit of a contrived geom to not have a required 'x' aesthetic
Expand Down
Loading