我正在尝试生成四个数据条件的折线图,将每个条件的平均值绘制为时间的函数。条件在 2x2 设计中有所不同,因此我一直使用 geom_line(实线/虚线的蓝/红线)和geom_point(正方形/圆形的蓝色/红色形状)来绘制数据。当我使用图层时,这工作得很好,但我也想在每个时间点包含误差线:
pd <- position_dodge(.1)
ggplot(data = df) +
geom_line(data=subset(df, condition == "A"), aes(E, avg),
colour="red", size=1) +
geom_point(data=subset(df, condition == "A"), aes(E, avg),
colour="red", shape=24, fill="white", size=5) +
geom_line(data=subset(df, condition == "B"), aes(E, avg),
colour="red", linetype="dashed",size=1) +
geom_point(data=subset(df, condition == "B"), aes(E, avg),
colour="red", shape=24, fill="red", size=5) +
geom_line(data=subset(df, condition == "C"), aes(E, avg),
colour="blue", size=1) +
geom_point(data=subset(df, condition == "C"), aes(E, avg),
colour="blue", shape=21, fill="white", size=5) +
geom_line(data=subset(df, condition == "D"), aes(E, avg),
colour="blue", linetype="dashed",size=1) +
geom_point(data=subset(df, condition == "D"), aes(E, avg),
colour="blue", shape=21, fill="blue", size=5)
上面的代码工作正常。但是,如果我随后添加这一行:
+ geom_errorbar(aes(x=E, ymin=avg-se, ymax=avg+se), width=.1, position=pd)
该图不考虑 pd(之前已定义),并且条形很难彼此区分(即重叠)。我该如何补救?
我完全重写了您的代码aes
它应该与手动秤一起使用的方式。
# sample data
df <- data.frame(condition = rep(LETTERS[1:4], each = 5),
E = rep(1:5, times = 4),
avg = rnorm(20),
se = .3)
# plotting command
ggplot(data = df, aes(x = E,
y = avg,
color = condition,
linetype = condition,
shape = condition,
fill = condition)) +
geom_line(size=1) +
geom_point(size=5) +
scale_color_manual(values = c(A = "red", B = "red", C = "blue", D = "blue"),
guide = "none") +
scale_linetype_manual(values = c(A = "solid", B = "dashed", C = "solid", D = "dashed"),
guide = "none") +
scale_shape_manual(values = c(A = 24, B = 24, C = 21, D = 21),
guide = "none") +
scale_fill_manual(values = c(A = "white", B = "red", C = "white", D = "blue"),
guide = "none") +
geom_errorbar(aes(x = E, ymin = avg-se, ymax = avg+se, color = NULL, linetype = NULL),
width=.1, position=position_dodge(width = .1))