-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Hi,
I'm trying to connect vertically dodged points using geom_line(), but the lines don't follow the dodged positions correctly.
I'm using the orientation argument introduced in v4.0, but I think the problem is rather caused by position and geom_line() both using the same group aesthetic.
An example speaks louder than words, so here is the code to reproduce the behaviour.
library(tidyverse)
d = tibble::tibble(
y_row = rep(c("d1", "d2"), each = 9L),
source = rep(rep(c("s1", "s2", "s3"), 2), rep(4:2, 2)),
value = c(1, 2, 3, 4, 2, 3, 4, 3, 4, 2, 3, 4, 5, 3, 4, 5, 4, 5),
)
pd = position_dodge(width=0.4, orientation="y")
# pd = position_dodge2(width=0.4)
ggplot(d) +
aes(x=value, y=y_row, color=source) +
geom_point(aes(group=source), position=pd) +
geom_line(aes(group=y_row), position=pd)Created on 2025-11-01 with reprex v2.1.1
Expected output: points should be connected by a line of the same color
As v4.0 introduced auxiliary aesthetics nudge_x, nudge_y and order, maybe another could be added to dissociate the grouping for geom_line() and position_dodge()?
Thanks a lot for your time and for maintaining ggplot2!
Happy to help if you need more examples or testing.
Addendum
Using pd = position_dodge2(width=0.4) gives the same result.
Changing the group in geom_line() gives a very different plot, and the dodging is not correct either:
ggplot(d) +
aes(x=value, y=y_row, color=source, group=y_row) +
geom_point(aes(group=source), position=pd) +
geom_line(aes(group=source))The mere fact of adding/removing the position dodge with Y orientation changes the x-axis somehow:
ggplot(d) +
aes(x=value, y=y_row, color=source, group=y_row) +
geom_point(aes(group=source), position=pd) +
geom_line(aes(group=source), position=pd)

