【问题标题】:Bar chart for factorial designs in RR中因子设计的条形图
【发布时间】:2015-03-15 14:53:38
【问题描述】:

我目前正在尝试使用 ggplot2 创建一个聚集条形图。它基本上只是 2x2x2 因子设计的平均响应时间。这三个因素是负载、兼容性和显着性。不过,我很难在其中干扰第三个因素(显着性)。不过它不应该是堆叠图

这是我目前拥有的

bar+stat_summary(fun.y = mean, geom = "bar", position = "dodge") + 
+ stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90), width = 0.2)+
+ labs(x = "Compatibility", y = "Mean RT", fill = "Load")

这是我试图绘制的数据的小样本:

     ID   load   comp           sal           rt                     
1     1   High   Incompatible   Non_Salient   787 
2     1   Low    Compatible     Salient       754
3     2   High   Incompatible   Salient       654

我以前看过很多次这样的图表,但我不知道如何让 ggplot2 同时显示三个自变量。

我尝试通过添加来拆分图表

+ facet_wrap( ~ sal)

但这也不起作用。它只是说“一元运算符的参数无效”

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 SO。你能提供可重现的数据吗?当您可以提供数据时,您可能会获得更多帮助。您可以在 R 控制台中键入 dput(yourdata)。您将看到一些文本,这些文本是您的数据。您可以将它们复制并粘贴到您的问题中。
  • 您还可以添加一个您希望完成的示例。我无法想象您的预期输出是什么样的。

标签: r graph ggplot2


【解决方案1】:

这是您正在寻找的情节吗?

我使用了《使用 R 发现统计数据》一书中的 Wii data,它的格式与您的类似。

structure(list(athlete = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2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L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Playing Wii", 
"Watching Wii"), class = "factor"), injury = c(2L, 2L, 1L, 
2L, 0L, 1L, 2L, 0L, 2L, 2L, 2L, 1L, 4L, 2L, 2L, 0L, 0L, 3L, 
3L, 3L, 2L, 1L, 0L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 4L, 1L, 
2L, 2L, 2L, 1L, 4L, 4L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 
2L, 2L, 1L, 0L, 3L, 3L, 2L, 1L, 2L, 4L, 1L, 2L, 5L, 5L, 3L, 
6L, 4L, 3L, 4L, 5L, 5L, 2L, 6L, 4L, 4L, 4L, 3L, 4L, 3L, 2L, 
1L, 4L, 3L, 2L, 2L, 1L, 3L, 1L, 1L, 3L, 4L, 2L, 7L, 8L, 6L, 
9L, 4L, 7L, 5L, 9L, 6L, 4L, 8L, 5L, 4L, 7L, 10L, 1L, 3L, 
2L, 1L, 3L, 3L, 2L, 3L, 4L, 2L, 0L, 1L, 3L, 2L, 0L)), 
.Names = c("athlete", "stretch", "wii", "injury"), 
class = "data.frame", row.names = c(NA, -120L))

这里是如何产生情节。

library(ggplot2)
library(Hmisc)
ggplot(data=Wii, aes(x=stretch, y=injury, fill=wii)) + 
  facet_wrap(~athlete) + 
  stat_summary(fun.y = mean, geom = "bar", position = "dodge") + 
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90), width = 0.2) 

【讨论】:

  • 这与我得到的一样接近。我实际上想创建一个将这两个图合并为一个图的图。所以 x 轴将包含 2 个嵌套变量,第三个是聚集条。不过感谢您的努力。这样做也一样好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多