【问题标题】:ggplot2: how to combine layers of geom_line and geom_point with error barsggplot2:如何将 geom_line 和 geom_point 层与误差线结合起来
【发布时间】:2015-03-12 01:23:07
【问题描述】:

我正在尝试生成四个数据条件的折线图,将每个条件的平均值绘制为时间的函数。条件在 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 调用中定义x
  • 我很困惑 - 我粘贴的代码不是将 aes 调用中的 x 定义为 y=min=avg-se, ymax=avg+se 吗?
  • 看起来你想要像aes(x=E, ymin=avg-se, ymax=avg+se)这样的东西
  • 感谢您的快速回复!该代码添加了错误栏,但覆盖了将错误栏分开的参数以区分它们。有没有办法重新添加它?
  • 对于任何混淆,我深表歉意,因为我试图以简洁的方式表达我的问题。显然,我应该提到我没有粘贴在这个问题中的早期代码定义了 pd,我现在已经编辑它以包含在问题中。因此,即使 pd 在前面定义,您建议的代码似乎也没有考虑 pd ,并且条形重叠。如果您对如何解决此问题有任何建议,我将不胜感激,也感谢您迄今为止的帮助。

标签: r ggplot2 linegraph


【解决方案1】:

我使用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))

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 2019-12-03
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2020-11-09
  • 2020-06-22
  • 1970-01-01
相关资源
最近更新 更多