For this illustrative data:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
I want to connect the paired points across ID
, which is possible with the help of this answer Connect jittered points by group:
pj <- position_jitter(seed = 1, width = .1, height = 0)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
# boxplot:
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
) +
# data points:
geom_point(
alpha = 0.5, col = "blue",
position = pj
) +
# connecting lines:
geom_path(aes(group = ID),
alpha = 0.5,
position = pj
)
What bothers me is that the points overplot the boxplots. I would like them to be separated from the boxplots. Specifically they should be moved inside the space between the boxes, like in this plot:
How can this be achieved? Many thanks in advance.