R中阶乘设计的条形图

时间:2015-03-15 14:53:39

标签: r graph ggplot2

我目前正在尝试使用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)

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是你要找的那种情节吗? enter image description here

我使用了书中的Wii data"使用R&#34发现统计数据,这与您的格式类似。

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, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,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 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)