2013-04-22 52 views
2

我是使用sciplot獲得實驗數據的忠實粉絲,因爲我不必手動計算誤差線。在過去,我已經將它用於將在兩個因子變量,使得:使用`gpplot2`使用多因素變量複製`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 
+0

當你說*組在三個因子變量*你的意思是聚合因子包含3個變量,或在某種程度上要包括之間的第三個因素變量x軸分組因子? – mnel 2013-04-22 01:45:56

+0

後者 - 我想在x軸分組因子之間加入第三個因子變量! – roody 2013-04-22 01:46:32

回答

3

您可以通過使用兩次調用stat_summary複製(在一定程度上)sciplot

您可以合併兩個因子水平作爲interaction(使用interaction)或使用分面。

我將使用它隨在數據集包用鹼R的ToothGrowth

# 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)) 

enter image description here

# 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) 

enter image description here

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))) 

enter image description here

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) 

output

## 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) 

output

+0

(+1)您現在可以發佈圖片 – 2014-11-23 15:17:57