2

我非常喜欢使用sciplot实验数据,因为我不必手动计算误差线。过去,我用它对两个因子变量进行分组,例如:

plot1<-bargraph.CI(
  df$factor1,   #categorical factor for the x-axis
  df$y,         #numerical DV for the y-axis
  df$factor2    #grouping factor
)

但是,我现在需要对三个因子变量进行分组。文档表明这sciplot是不可能的sciplot.

所以,现在到了必要的时间来问......我到底该怎么做ggplot2?具体来说,是否有一种简洁的方法来生成包含超过 3 个因子变量的误差线图?我在网上四处寻找,在寻找一个优雅的解决方案时总是不满意。

下面的示例数据:

factor1          factor2             factor3     y
More expensive   Least important     Blue        1
Less expensive   Most important      Blue        0
Same cost        Least important     Red         1
More expensive   Least important     Red         0
Less expensive   Most important      Red         1
Same cost        Least important     Blue        1
More expensive   Least important     Red         1
Less expensive   Least important     Blue        0
Same cost        Most important      Red         1
4

2 回答 2

3

sciplot您可以通过两次调用来复制(在一定程度上)stat_summary

您可以将两个因子水平合并为interaction(使用interaction)或使用分面。

我将使用ToothGrowth基于 R 的 datasets 包中附带的。

# add third factor
ToothGrowth$F3 <- letters[1:2]
# coerce dose to a factor
ToothGrowth$dose <- factor(ToothGrowth$dose, levels = c(0.5,1,2))

# interaction on the x axis
 ggplot(ToothGrowth, aes(y = len, x = interaction(supp, F3))) + 
  stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, geom = 'bar', 
    aes(fill =dose), position = 'dodge') +
  stat_summary(fun.ymin = function(x) mean(x) - sd(x), 
    fun.ymax = function(x) mean(x) + sd(x),  position ='dodge', 
    geom = 'errorbar', aes(group = dose))

在此处输入图像描述

# facetting on the third factor
ggplot(ToothGrowth, aes(y = len, x = supp )) + 
  stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, geom = 'bar', 
   aes(fill =dose), position = 'dodge') +
  stat_summary(fun.ymin = function(x) mean(x) - sd(x), 
               fun.ymax = function(x) mean(x) + sd(x), position ='dodge', 
               geom = 'errorbar', aes(group = dose))+
  facet_wrap(~F3)

在此处输入图像描述

ggplot(ToothGrowth, aes(y = len, x = supp)) + 
  stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, 
               geom = 'bar', aes(fill =interaction(dose, F3)), 
               position = 'dodge') +
  stat_summary(fun.ymin = function(x) mean(x) - sd(x), 
               fun.ymax = function(x) mean(x) + sd(x), 
               position ='dodge', geom = 'errorbar', 
               aes(fill =interaction(dose, F3)))

在此处输入图像描述

于 2013-04-22T02:17:20.097 回答
2

实际上这在 sciplot 中是可能的。下面是两个解决方案,第一个是通过将分组因子指定为列表,第二个是从 ggplot 复制分面解决方案。

library(sciplot)

## add third factor as in above example
ToothGrowth$F3 <- letters[1:2]

## Adding group as a list
bargraph.CI(response=len, x.factor=supp, group=list(dose, F3),
            data=ToothGrowth, legend=TRUE, x.leg=14, xlim=c(0,19),
            err.width=0.025)

输出

## Using "panels"
par(mfrow=c(1,2), xpd=NA)
bargraph.CI(response=len, x.factor=supp, group=dose, data=ToothGrowth,
            subset=F3=="a", xlab="a", cex.lab=1.25,
            legend=TRUE, x.leg=7.5, err.width=.025)
bargraph.CI(response=len, x.factor=supp, group=dose, data=ToothGrowth,
            subset=F3=="b", xlab="b", cex.lab=1.25, err.width=.025)

输出

于 2014-11-23T15:14:49.597 回答