position_jitterdodge() misaligns points when additional discrete aesthetics are mapped
Hello there!
looking for feedback / thoughts on some position_jitterdodge() - currently, when adding a discrete aesthetic like colour to the point layer users will observe silent misalignment when additional discrete aesthetics are mapped.
library(ggplot2)
# Works: fill only
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(am))) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitterdodge(seed = 1))
# Misaligned: adding colour = factor(vs) to the point layer
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(am))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(colour = factor(vs)),
position = position_jitterdodge(seed = 1))
# Workaround: explicitly set group to match the fill variable
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(am))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(colour = factor(vs), group = factor(am)),
position = position_jitterdodge(seed = 1))
In the second plot, points are distributed across incorrect horizontal positions because the implicit group aesthetic becomes cyl x am x vs (the interaction of all discrete aesthetics), while the boxplot dodges by cyl x am only. The dodge machinery then divides the available width across more groups than the boxplots have, causing misalignment.
The workaround is aes(group = factor(am)), which overrides the implicit grouping to match the boxplot's fill-based dodge. However, this requires understanding how ggplot2 computes implicit groups from discrete aesthetics, which is non-obvious even for experienced users. This pattern comes up frequently when users want to colour points by a secondary variable (e.g., a status flag or subgroup) while keeping them aligned with their respective boxplots.
This is not quite a bug perse, but I think there is perhaps an opportunity to either provide a more intelligent default jitterdodge or at least some slightly verbose docs / a warning about this behavior.
Note, the images below use ToothGrowth with 3 dose groups to make the misalignment more visually apparent than mtcars dataset as a demo.


See the following draft PRs for docs change and default change:
- docs changes, add tests for warn: https://github.com/tidyverse/ggplot2/pull/6825
- infer dodge grouping by default: https://github.com/tidyverse/ggplot2/pull/6826
Cheers, -Jess
Hi, I would like to work on this issue. I will try to reproduce the misalignment and investigate the cause.
Thank you!
Thanks for the report! I would suggest that your 'workaround' is not a workaround but actually the recommended path. It falls under case 3 where inferred groupings may be worse than manually curated groupings mentioned here: https://ggplot2.tidyverse.org/reference/aes_group_order.html.
I'm happy to consider linking up documentation or making a note for this in position_jitter()'s docs, but I would not want to give any special treatment to aesthetics (e.g. fill) on the fly.
I'm closing this issue because this shortcoming in the defaults is already documented and that documentation advertises the workaround as the correct solution to the problem.