简体   繁体   English

plot ggplot 中多行的总误差条 r

[英]plot TOTAL errorbar for multiple lines in ggplot r

I would like to plot the data by subject but adding the errorbar of the total mean and se.我想按主题 plot 数据,但添加总平均值和 se 的误差线。 I mean, not an error bar for each subject.我的意思是,不是每个主题的错误栏。 I've tried geom_errorbar and stat_summary but still failed to get my ideal plot (see the figure I drew).我已经尝试过 geom_errorbar 和 stat_summary 但仍然未能得到我理想的 plot(参见我画的图)。 在此处输入图像描述

and here is the code I used to draw this figure (the errorbars are added by hand).这是我用来绘制此图的代码(错误栏是手动添加的)。

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)

Anyone could help?任何人都可以帮忙吗? Thank you very much!!!非常感谢!!!

Use annotate to add the error bars.使用annotate添加误差线。 I don't have your data, so I created my own.我没有你的数据,所以我创建了自己的。 You're going to need the confidence interval and the average for each group.您将需要每个组的置信区间和平均值。 My average-by-group values and confidence interval-by-group are stored in df4$meanV and df4$ci .我的按组平均值和按组置信区间存储在df4$meanVdf4$ci中。 You can replace these with your variable names.您可以将这些替换为您的变量名称。 In annotate, you'll include the data frame in the call like you would in base R plots.在注释中,您将在调用中包含数据框,就像在基本 R 图中一样。 Like base R, you can just use raw values, as well.与基础 R 一样,您也可以只使用原始值。 Multiple values can be joined with c() .可以使用c()连接多个值。 As in y = c(12, 10) .y = c(12, 10) If you have any questions, just let me know.如果您有任何问题,请告诉我。

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))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM