我正在尝试生成四个数据条件的折线图,绘制每个条件的平均值作为时间的函数。条件因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(先前已经定义),并且条很难彼此区分(即,重叠)。我该如何解决这个问题?
答案 0 :(得分:2)
我使用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))