【问题标题】:plot TOTAL errorbar for multiple lines in ggplot r在ggplot r中绘制多行的TOTAL误差条
【发布时间】:2022-08-12 16:53:24
【问题描述】:

我想按主题绘制数据,但添加总均值和 se 的误差条。我的意思是,不是每个主题的错误栏。我尝试过 geom_errorbar 和 stat_summary 但仍然未能得到我理想的情节(见我画的图)。

这是我用来绘制此图的代码(错误栏是手动添加的)。

ggplot(ASD, aes(x=period, y=meanF0, group=subject, color=group)) + 
      geom_line(aes(color=group, size=group)) +
      scale_size_manual(values=c(.6, .6, .6, .6)) +
      theme_light()+
      xlab(\"Period\")+
      ylab(\"F0 (Hz)\")+
      ggtitle(\"Mean F0 Adjustment (ASD Group)\") +
      geom_point()+
      scale_color_manual(values=c(\"red\")) +
      theme(plot.title = element_text(size=14.5, face=\"bold\", hjust = 0.5,  family = \"serif\"), 
            axis.title.y= element_text(size=12, face = \"bold\", family = \"serif\"),
            axis.title.x= element_text(size=12, face = \"bold\", family = \"serif\"),
            axis.text.x = element_text(size=11, face=\"bold\", family = \"serif\"),
            axis.text.y = element_text(size=11, face=\"bold\", family = \"serif\"))+
      theme(legend.position = \"none\")+
      geom_hline(yintercept=112.8, linetype=\"dashed\", 
                 color = \"dark grey\", size=.7)

任何人都可以帮忙吗?非常感谢!!!

  • 看起来您对 SO 很陌生;欢迎来到社区!如果您想快速获得很好的答案,最好让您的问题可重现。这包括示例数据,例如来自 dput(head(dataObject))) 的输出以及您正在使用的任何库(尽管在这种情况下,它似乎很可能只是 ggplot2)。看看:making R reproducible questions

标签: r ggplot2 line errorbar


【解决方案1】:

使用annotate 添加误差线。我没有你的数据,所以我创建了自己的数据。您将需要每个组的置信区间和平均值。我的按组平均值和按组置信区间存储在df4$meanVdf4$ci 中。您可以将这些替换为您的变量名。在注释中,您将在调用中包含数据框,就像在基本 R 图中一样。与基础 R 一样,您也可以只使用原始值。多个值可以用c() 连接。如y = c(12, 10)。如果您有任何问题,请告诉我。

ggplot(df2, aes(x = condition, y = value, 
                color = subject, group = subject)) +
  geom_line() + geom_point() + 
  annotate("errorbar",
           x = df4$condition
           ymin = df4$meanV - df4$ci,
           ymax = df4$meanV + df4$ci,
           width = .2) +
  annotate("point",
          x = df4$condition,
          y = df4$meanV) + 
  ylim(min(df2$value), max(df2$value))

【讨论】:

  • 是的,您的解决方案有效!谢谢!
【解决方案2】:

请你能解释一下这个情节的解决方案吗?我有一个类似的图表可以使用 ggplot2 绘制。 enter image description here

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 2021-07-23
相关资源
最近更新 更多