【问题标题】:ggplot line graph shows all individual values rather than meansggplot 折线图显示所有单个值而不是平均值
【发布时间】:2021-02-23 13:00:20
【问题描述】:

您好,我对 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", "front", "back", 
"back", "front", "front", "back", "back", "front", "front", "back", 
"back", "front", "front", "back", "back", "front", "front", "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", 
"304.722222222222", "288.647058823529", "281.823529411765", "309.944444444444"
), f2.f.tense = c("past", "future", "past", "future", "past", 
"future", "past", "future", "past", "future", "past", "future", 
"past", "future", "past", "future", "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")


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

【问题讨论】:

  • 欢迎来到 SO,JuliaH!有没有办法提供样本数据?在不知道我们在看什么的情况下很难“玩”代码。如果您可以共享真实数据,请发布来自dput(x) 的输出,其中x 是您框架的顶部这么多行,包括您正在使用的列,所以x 可能是head(data_tense_final[,c("f2.f.position", "RT3", "f2.f.tense")], 20) ; mean_cl_boot 类似。如果您无法共享数据(可以理解),这并不能成为没有数据的借口……这只是意味着您需要创建随机/代表性数据或使用公共数据集。谢谢!
  • 非常感谢你,我应该添加一些关于数据框结构的信息(现在已经在上面添加了几行),特别是因为我怀疑问题可能出在数据格式上!非常感谢和道歉!
  • 请阅读我之前的评论。 R 控制台上的data.frame 可能是对数据的错误表示:数字或字符串实际上可能是factor,这会破坏很多东西。再次,请发布dput的输出,它可以澄清很多。
  • 当然,很抱歉(正如我所说,我是 R 新手,不确定哪些信息最有帮助,但是是的,您在之前的评论中说过,抱歉!),我会将输出添加到上面的问题中。我已经为原始数据框编写并应用了一些函数(删除异常值、计算平均分数等)来创建这个数据框,也许这是个问题?非常感谢!
  • 你说得对,我将更详细地探讨我的异常值检测发生在哪一点,因为我将继续使用这个数据集——但我已经很高兴它是善良的现在已经工作了,非常感谢! :-)

标签: r ggplot2 linegraph


【解决方案1】:

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

【讨论】:

  • 非常感谢,我试试这个!
【解决方案2】:
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")

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 2019-04-02
    • 2018-08-15
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多