我的数据看起来像这样:
df1 <-
structure(
list(
y = c(-0.19, 0.3,-0.05, 0.15,-0.05, 0.15),
lb = c(-0.61,
0.1,-0.19,-0.06,-0.19,-0.06),
ub = c(0.22, 0.51, 0.09, 0.36,
0.09, 0.36),
x = structure(
c(1L, 2L, 1L, 2L, 1L, 2L),
.Label = c("X1",
"X2"),
class = "factor"
),
Group = c("A", "A", "B", "B", "C",
"C")
),
.Names = c("y", "lb", "ub", "x", "Group"),
row.names = c(NA,-6L),
class = "data.frame"
)
我想使用ggplot2
绘制点x,y
,用group
着色并带有误差线lb, ub
。由于x
是离散的,所以我想要jitter
使点和柱不重叠。目前,我可以jitter
点,但无法处理线条。此外,我希望点的顺序为A、B、C。
ggplot(data = df1, aes(x, y, color = Group)) + geom_point(size = 4, position = "jitter") +
geom_errorbar(
aes(ymin = lb, ymax = ub),
width = 0.1,
linetype = "dotted"
) +
geom_hline(aes(yintercept = 0), linetype = "dashed") + theme_bw()
position_width
将所有点放在垂直线上,因此误差条在类别内重叠。有没有一种方法可以在类别内实现更进一步的抖动,并仍然使误差条与点对齐? - EcologyTom