注意:我在与 Z. Lin 讨论后更新了这篇文章。最初,我将问题简化为双因素设计(请参阅“原始问题”部分)。但是,我的实际数据由四个因素组成,需要 facet_grid。因此,我在下面提供了一个四因素设计的示例(请参阅“编辑”部分)。
原始问题
假设我有一个双因素设计,其中 dv 作为我的因变量,iv.x 和 iv.y 作为我的因素/自变量。一些快速的示例数据:
DF <- data.frame(dv = rnorm(900),
iv.x = sort(rep(letters[1:3], 300)),
iv.y = rep(sort(rep(rev(letters)[1:3], 100)), 3))
我的目标是分别显示每个条件,这可以很好地用小提琴图完成:
ggplot(DF, aes(iv.x, dv, colour=iv.y)) + geom_violin()
我最近遇到了新浪的情节,也想在这里做同样的事情。不幸的是,新浪地块不这样做,而是折叠数据。
ggplot(DF, aes(iv.x, dv, colour=iv.y)) + geom_sina()
显式调用 position dodge 也无济于事,因为这会产生错误消息:
ggplot(DF, aes(iv.x, dv, colour=iv.y)) + geom_sina(position = position_dodge(width = 0.5))
新浪地块的作者在 2016 年就已经意识到了这个问题: https ://github.com/thomasp85/ggforce/issues/47
我的问题更多是在时间方面。我们很快就想提交一份手稿,而新浪地块将是展示我们数据的好方法。谁能想到新浪图的解决方法,这样我仍然可以像上面的小提琴图示例中那样显示两个因素?
编辑
四因素设计的样本数据:
DF <- data.frame(dv=rnorm(400),
iv.w=sort(rep(letters[1:2],200)),
iv.x=rep(sort(rep(letters[3:4],100)), 2),
iv.y=rep(sort(rep(rev(letters)[1:2],50)),4),
iv.z=rep(sort(rep(letters[5:6],25)),8))
我想使用新浪图创建的小提琴图示例:
ggplot(DF, aes(iv.x, dv, colour=iv.y)) +
facet_grid(iv.w ~ iv.z) +
geom_violin(aes(y = dv, fill = iv.y),
position = position_dodge(width = 1))+
stat_summary(aes(y = dv, fill = iv.y), fun.y=mean, geom="point",
colour="black", show.legend = FALSE, size=.2,
position=position_dodge(width=1))+
stat_summary(aes(y = dv, fill = iv.y), fun.data=mean_cl_normal, geom="errorbar",
position=position_dodge(width=1), width=.2, show.legend = FALSE,
colour="black", size=.2)