Skip to content

Warn when position_jitterdodge() groups are inflated beyond fill#6825

Draft
Jesssullivan wants to merge 2 commits intotidyverse:mainfrom
Jesssullivan:jitterdodge-docs-warning
Draft

Warn when position_jitterdodge() groups are inflated beyond fill#6825
Jesssullivan wants to merge 2 commits intotidyverse:mainfrom
Jesssullivan:jitterdodge-docs-warning

Conversation

@Jesssullivan
Copy link
Copy Markdown

Warn when position_jitterdodge() groups are inflated beyond fill

Relates to #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

  • Motivation described above and in NEWS entry (@Jesssullivan, #6824)
  • Only related changes (warning + docs for this single issue)
  • Tidyverse style — follows existing cli::cli_warn() patterns in position code
  • roxygen2 docs updated, devtools::document() run
  • Unit tests added (4 new, all passing)
  • Visual tests — N/A (not a graphical output change)
  • Minimal example added to `@examples

Jesssullivan and others added 2 commits March 27, 2026 17:31
When additional discrete aesthetics (e.g., colour) are mapped in a
point layer using position_jitterdodge(), the implicit group becomes
finer than the fill-based dodging of boxplots, causing misalignment.

This adds a targeted warning in setup_params() that detects when
dodge groups exceed fill levels, guiding users to set
aes(group = <fill variable>). Also adds a @section to the
documentation explaining the grouping interaction, with examples
showing the pitfall and fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant