Warn when position_jitterdodge() groups are inflated beyond fill
Warn when position_jitterdodge() groups are inflated beyond fill
Closes #6824.
Depending on thoughts about this 👀 I also am opening a sister PR that instead modifies default behavior, which may or may not be preferable.
position_jitterdodge() silently misaligns points when additional discrete aesthetics (e.g., colour) are mapped in the point layer. The implicit group becomes finer than the fill-based dodge of the boxplot, but no diagnostic is emitted. The workaround (aes(group = <fill variable>)) requires understanding the implicit grouping mechanism, which is non-obvious.
# Points land in wrong dodge slots — no error, no warning
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(colour = factor(vs)),
position = position_jitterdodge(seed = 1))
Warning (R/position-jitterdodge.R): In setup_params(), when fill is present and discrete, compare the number of unique groups per x-position against the number of unique fill values. When groups exceed fills, emit a cli::cli_warn() with the fix. Uses the same vec_unique/vec_group_id/tabulate pattern already at lines 63-65 of the function.
Does not fire when: the user has already set group = <fill_var>, only fill is mapped, fill is absent, or fill is continuous.
Documentation (R/position-jitterdodge.R): New @section Interaction with grouping: explains the mechanism and the aes(group = ...) fix. New \donttest{} example demonstrates the pitfall and solution using a small data frame.
Tests (tests/testthat/test-position-jitterdodge.R): 4 new tests covering warning/no-warning scenarios; these may be overkill.
NEWS (NEWS.md): Entry added.
Checklist
- [x] Motivation described above and in NEWS entry (
@Jesssullivan, #6824) - [x] Only related changes (warning + docs for this single issue)
- [x] Tidyverse style — follows existing
cli::cli_warn()patterns in position code - [x] roxygen2 docs updated,
devtools::document()run - [x] Unit tests added (4 new, all passing)
- [ ] Visual tests — N/A (not a graphical output change)
- [x] Minimal example added to `@examples
Following along from here https://github.com/tidyverse/ggplot2/pull/6826#issuecomment-4243125683 from this feedback https://github.com/tidyverse/ggplot2/issues/6824#issuecomment-4236406928 from @teunbrand
This PR may ultimately be overkill as well, given the group order docs do fundamentally cover this behavior already to some degree.
ref. https://github.com/tidyverse/ggplot2/issues/6824