ggplot2:如何将 geom_line 和 geom_point 的层与误差线结合起来
ggplot2: how to combine layers of geom_line and geom_point with error bars
我正在尝试生成四个数据条件的折线图,将每个条件的平均值绘制为时间的函数。 2x2 设计的条件各不相同,所以我一直在使用 geom_line(blue/red 行 solid/dashed)和 geom_point(blue/red 形状 squares/circles) 绘制数据。当我使用图层时效果很好,但我也想在每个时间点包含误差线:
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))
我正在尝试生成四个数据条件的折线图,将每个条件的平均值绘制为时间的函数。 2x2 设计的条件各不相同,所以我一直在使用 geom_line(blue/red 行 solid/dashed)和 geom_point(blue/red 形状 squares/circles) 绘制数据。当我使用图层时效果很好,但我也想在每个时间点包含误差线:
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))