ggplot 折线图显示所有单个值而不是均值

问题描述

the graph that I currently get

您好,我对 R 非常陌生,想在折线图中演示两个变量之间可能的交互。但是,我得到的图表确实包括所有个人反应时间值而不是均值。我猜我的数据可能格式错误?目前,我的位置和紧张状态分别在不同的列中指定,反应时间作为结果变量在另一列中指定。

我使用的代码是:

line <- ggplot(data_tense_final,aes(f2.f.position,RT3,colour = f2.f.tense))

line +
  stat_summary(fun.y = mean,geom = "point") + 
  stat_summary(fun.y = mean,geom = "line",aes(group = f2.f.tense)) + 
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar",width = 0.2) + 
  labs(x = "Position",y = "Mean RT",colour = "f2.f.tense")

The dataframe looks more or less like this:

  f2.f.participant f2.f.condition f2.f.tense f2.f.position              RT3
1                 1              1       past          back 445.944444444444
2                 1              2     future         front 448.882352941176
3                 1              3       past         front 454.222222222222
4                 1              4     future          back         526.4375
5                 2              1       past          back 338.631578947368
6                 2              2     future         front 342.058823529412
7                 2              3       past         front 350.222222222222
8                 2              4     future          back 341.266666666667
9                 3              1       past          back              331
10                3              2     future         front 325.647058823529


The output from deput(x) is:

structure(list(f2.f.position = c("back","front","back","back"
),RT3 = c("445.944444444444","448.882352941176","454.222222222222","526.4375","338.631578947368","342.058823529412","350.222222222222","341.266666666667","331","325.647058823529","303.9375","361.111111111111","304.722222222222","288.647058823529","281.823529411765","309.944444444444","309.944444444444"
),f2.f.tense = c("past","future","past","future")),row.names = c(1L,20L,39L,58L,77L,96L,115L,134L,153L,172L,191L,210L,229L,248L,267L,286L,305L,324L,343L,362L),class = "data.frame")


我可能犯了一个非常明显的错误,提前道歉! 非常感谢!

解决方法

尝试使用函数ggline(...,add = "mean")。您可以找到更多信息here

,
line <- ggplot(data_tense_final,aes(f2.f.position,RT3,colour = f2.f.tense))

line +
  stat_summary(fun = "mean",geom = "point") + # I guess "s are needed
  stat_summary(fun = "mean",geom = "line") + # grouping is already done by colour
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar",width = 0.2) + 
  labs(x = "Position",y = "Mean RT",colour = "f2.f.tense")