在 ggplot r 中绘制多行的 TOTAL 误差条

发布于 2025-01-10 01:05:03 字数 1227 浏览 3 评论 0原文

我想按主题绘制数据,但添加总平均值和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)

有人可以帮忙吗?非常感谢!!!

I would like to plot the data by subject but adding the errorbar of the total mean and 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).
enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2025-01-17 01:05:04

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

输入图片此处描述

Use annotate to add the error bars. 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. 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. Like base R, you can just use raw values, as well. Multiple values can be joined with c(). As in 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))

enter image description here

傲性难收 2025-01-17 01:05:04

使用库superb,您可以获得一个计算均值和置信区间的函数。您可以使用各种布局来显示附加信息。其中一种布局显示单独的行,

library(superb)

superb( cbind(value.posttest, value.pretest) ~ ., ASD,
  WSFactors = ("condition(2)"),
  plotStyle = "pointindividualline"
)

您还可以访问其他布局。一种称为corset-plot。它显示了单独的线条,但也显示了侧面的平滑密度。尝试

superb( cbind(value.posttest, value.pretest) ~ ., ASD,
  WSFactors = ("condition(2)"),
  plotStyle = "corset",
  violinParams = list(fill = "green", alpha = .2) # optional: some color
) + theme_bw() # optional : alternative theme

获取:

corsetplot

请注意,我是superior的维护者。

Using the library superb, you get a function which computes the means and confidence intervals. You can, using various layouts, display additional information. One such layout displays the individvidual lines with

library(superb)

superb( cbind(value.posttest, value.pretest) ~ ., ASD,
  WSFactors = ("condition(2)"),
  plotStyle = "pointindividualline"
)

You also have access to other layouts. One is called corset-plot. It shows individual lines, but also a smooth density on the sides. Try

superb( cbind(value.posttest, value.pretest) ~ ., ASD,
  WSFactors = ("condition(2)"),
  plotStyle = "corset",
  violinParams = list(fill = "green", alpha = .2) # optional: some color
) + theme_bw() # optional : alternative theme

You get:

corset plot

Note that I am the maintainer of superb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文