I don't know of way to connect the points in exactly the plot that you produced. But I can show you how to do something similar.
The difficulty is that all the points that belong to a pair of boxplots share the same x-coordinate (that is, the same value for variable
). Therefore I have used interaction(sex, variable)
as the x-coordinate, such that each boxplot has its own x-value. This means, however, that the paired boxplots are less visible. On the other hand, the connecting lines work in the other direction.
In order to plot the lines, they are grouped by interaction(index, variable)
, which means that data points are connected, when they share the same values for index
and variable
.
Enough talk, here's the code:
ggplot(melt_crabs, aes(x = interaction(sex, variable), y = value)) +
geom_boxplot(aes(fill = sex), alpha = 0.5) +
geom_line(aes(group = interaction(index, variable)),
alpha = 0.5, colour = "darkgrey") +
facet_grid(sp~.) +
scale_x_discrete(labels = rep(levels(melt_crabs$variable), each = 2))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…