From your question, I don't this df$x
is relevant to your data at all, especially if you can re-order it. How about just using group
as x, and jitter
the actual x position to separate the points:
ggplot(data=df, aes(x=group,y=y,color=group)) + geom_point() +
geom_jitter(position = position_jitter(width = 0.4)) +
geom_errorbar(stat = "hline", yintercept = "mean",
width=0.8,aes(ymax=..y..,ymin=..y..))
I have used errorbar instead of h_line (and collapsed the ymax and ymin to y) since hline is complex. If anyone has a better solution to that part, I'd love to see.
update
If you want to preserve the order of X, try this solution (with modified X)
df$x = factor(df$x)
ggplot(data = df, aes(x, y, group=group)) +
facet_grid(.~group,space="free",scales="free_x") +
geom_point() +
geom_line(stat = "hline", yintercept = "mean")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…