Is it possible to draw a line using geom_line() between dodged points, when the grouping variables on the x axis, the color and for the line overlap but are always different?
The grey lines in the plot below are supposed to always connect two datapoints with the same name, which are within the same grouping2 (x-axis) but in a different grouping2 (color).
Reproducible example:
library(tidyverse)
tbl = tibble(name = rep(paste("name", 1:12), each = 2),
grouping1 = rep(c("A", "B"), 12),
grouping2 = c(rep("X", 8), rep("Y",8), rep("Z",8)),
value = 1:24)
tbl %>%
ggplot(aes(x = grouping2, y = value, color = grouping1)) +
geom_line(aes(group = name),position= position_dodge(0.5), color = "grey44") +
geom_point(position = position_dodge(0.5))
For my specific example there are some more or less hacky ways around this problem like using geom_segment() for the lines and turning the x-axis grouping2 into numeric or combining two groupings on the x-axis like grouping1 and grouping2.
Therefore, my question is if there is a clean way to achieve the desired plot?
There are some related questions like using position_dodge with geom_line , but they are not quite the same.