Skip to content

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

Open
Jesssullivan wants to merge 1 commit intotidyverse:mainfrom
Jesssullivan:jitterdodge-docs-warning
Open

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

Conversation

@Jesssullivan
Copy link
Copy Markdown

@Jesssullivan Jesssullivan commented Mar 29, 2026

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

  • 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
Copy link
Copy Markdown
Author

Jesssullivan commented Apr 14, 2026

Following along from here #6826 (comment) from this feedback #6824 (comment) 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. #6824

When additional discrete aesthetics are mapped in a layer using position_jitterdodge(), the implicit group can become finer than the fill-based dodge used by paired layers, causing visual misalignment.

Add a targeted warning when dodge groups exceed fill levels, document the grouping interaction, and cover warning/no-warning cases in tests.
@Jesssullivan Jesssullivan force-pushed the jitterdodge-docs-warning branch from 90892bb to 8d6a98f Compare May 5, 2026 15:22
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.

position_jitterdodge() misaligns points when additional discrete aesthetics are mapped

1 participant