5

注意:我在与 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) 
4

1 回答 1

3

编辑解决方案,因为 OP 澄清了需要方面:

ggplot(DF, aes(x = interaction(iv.y, iv.x), 
               y = dv, fill = iv.y, colour = iv.y)) + 
  facet_grid(iv.w ~ iv.z) +      
  geom_sina() +
  stat_summary(fun.y=mean, geom="point", 
               colour="black", show.legend = FALSE, size=.2, 
               position=position_dodge(width=1))+
  stat_summary(fun.data=mean_cl_normal, geom="errorbar", 
               position=position_dodge(width=1), width=.2, 
               show.legend = FALSE,
               colour="black", size=.2) +
  scale_x_discrete(name = "iv.x", 
                   labels = c("c", "", "d", "")) +
  theme(panel.grid.major.x = element_blank(),
        axis.text.x = element_text(hjust = -4),
        axis.ticks.x = element_blank())

这种方法不是使用构面来模拟颜色之间的闪避,而是创建一个新变量interaction(colour.variable, x.variable)以映射到 x 轴。

scale_x_discrete()&中的其余代码theme()用于隐藏默认的 x 轴标签/刻度线/网格线。

axis.text.x = element_text(hjust = -4)是一种将 x 轴标签移动到大约正确位置的技巧。这很难看,但考虑到用例是提交稿件,我假设图的大小是固定的,你只需要调整一次。

已编辑的解决方案

原始解决方案

假设您的绘图不需要刻面,您可以使用刻面模拟外观:

ggplot(DF, aes(x = iv.y, y = dv, colour = iv.y)) +
  geom_sina() + 
  facet_grid(~iv.x, switch = "x") +
  labs(x = "iv.x") +
  theme(axis.text.x = element_blank(),      # hide iv.y labels
        axis.ticks.x = element_blank(),     # hide iv.y ticks
        strip.background = element_blank(), # make facet strip background transparent
        panel.spacing.x = unit(0, "mm"))    # remove horizontal space between facets

阴谋

于 2018-05-18T11:30:19.310 回答