2013-03-20 72 views
10

我想查看我的數據和ANOVA統計數據。使用帶有添加的線條的barplot來指示重要的差異和相互作用是很常見的。你如何使用R來製作這樣的情節?Barplot與顯着的差異和相互作用?

這是我想什麼:

顯着差異:

significant differences

着明顯的相互作用:

significant interactions

背景

我真正水流使用barplot2{ggplots}繪製條形圖和置信區間,但我願意使用任何包/程序來完成工作。要獲得統計信息,我正在使用TukeyHSD{stats}pairwise.t.test{stats}獲取差異,並使用anova函數之一(aovezANOVA{ez}gls{nlme})進行交互。

只給你一個想法,這是我目前的情節: barplot2 with CIs

+2

multcomp中有一個plot.cld函數,您可以將字母放在您的酒吧上方指示重要性。 Perhabs這也適合你... – EDi 2013-03-20 22:28:07

+0

還有'agricolae'包中的'bar.group',它爲你打上字母。 – mnel 2013-03-20 22:35:05

+0

如果您使用base R的'barplot',則可以存儲像barstore < - barplot(1:3)'這樣的條的中心點。爲了驗證這是否正常,請嘗試'abline(v = barstore)'並注意垂直線都切斷了條的中心。使用'段'可以使用這些存儲點來繪製比較/交互線。 – thelatemail 2013-03-20 23:43:11

回答

9

當您使用功能barplot2()從庫gplots,將使用這種方法給出的例子。

首先,在幫助文件barplot2()函數中給出了barplot。 ci.lci.u是假置信區間值。 Barplot應該保存爲對象。

hh <- t(VADeaths)[1:2, 5:1] 
mybarcol <- "gray20" 
ci.l <- hh * 0.85 
ci.u <- hh * 1.15 
mp <- barplot2(hh, beside = TRUE, 
       col = c("grey12", "grey82"), 
       legend = colnames(VADeaths)[1:2], ylim = c(0, 100), 
       cex.names = 1.5, plot.ci = TRUE, ci.l = ci.l, ci.u = ci.u) 

如果您看對象mp,它包含所有酒吧的x座標。

mp 
    [,1] [,2] [,3] [,4] [,5] 
[1,] 1.5 4.5 7.5 10.5 13.5 
[2,] 2.5 5.5 8.5 11.5 14.5 

現在我使用上置信區間值來計算段的y值的座標。分段將從比置信區間結束高1的位置開始。 y.cord包含四行 - 第一行和第二行對應第一個欄,其他兩行對應第二個欄。最高的y值是從每個柱對的置信區間的最大值計算出來的。 x.cord值只是在mp對象中重複相同的值,每個值爲2次。

y.cord<-rbind(c(ci.u[1,]+1),c(apply(ci.u,2,max)+5), 
      c(apply(ci.u,2,max)+5),c(ci.u[2,]+1)) 
x.cord<-apply(mp,2,function(x) rep(x,each=2)) 

barplot由使用sapply()後使5個線段使用計算出的座標(因爲此時有5組)。

sapply(1:5,function(x) lines(x.cord[,x],y.cord[,x])) 

要繪製的線段上述文本計算x和y座標,其中x是兩個條形的x值的中間點和y值被從置信區間爲每個杆對加上一些恆定的極大值來計算。然後使用功能text()添加信息。

x.text<-colMeans(mp) 
y.text<-apply(ci.u,2,max)+7 
text(c("*","**","***","NS","***"),x=x.text,y=y.text) 

enter image description here

2

我想,現在你的問題已經或多或少的解決,所以我反而會鼓勵你使用不同的方法,那就是在數據的可視化表示要好得多 - 點陣圖。舉個例子你barplot比較類似的數據點構建的點陣圖:

#example data similar to your barplot 
d <- data.frame(group=rep(c("control","group1","group2"),each=4), 
       esker=c(1.6,1.4,1.8,1.5,2,1.8,1.6,1.4,2.3,2,1.7,1.4), 
       se=rep(0.1,12), 
       cond=rep(c("t1","t2","t3","t4"),3)) 
#dotplot - you need Hmisc library for version with error bars 
library(Hmisc) 
Dotplot(cond ~ Cbind(esker, esker+se, esker-se) | group, data=d, col=1, 
     layout=c(1,3), aspect="xy", 
     par.settings = list(dot.line=list(lwd=0), plot.line=list(col=1))) 

enter image description here

比較它barplot。在點圖中,水平繪製時可以更容易地看到差異,不需要額外的圖例或條形圖或顏色來向您顯示條件,您不需要準則和其他嘈雜的元素。你擁有這三個面板中的所有內容。當然,我知道你可能想強調一下你的重要效果,也可能在少數條件下工作正常。但是,如果因素數量增加,劇情就會隨着星星和狗屎而溢出。

保持簡單。保持它的dotplot。請查看William Cleveland和Edward Tufte的書籍以瞭解更多信息。

0

我建議使用ggplot代替barplot,你可以手動構建線路是這樣的:

這是開始像下面這樣的data.table: data.table used

gg <- ggplot(data, aes(x = time, y = mean, fill = type)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    scale_fill_manual(values = c("RGX" = "royalblue2", "EX" = "tomato2")) + 
    xlab("Post-treatment Time Point (months)") + 
    ylab(paste("data", "Change Score")) + 
    scale_y_continuous(expand = c(0, 0)) + 
    ylim(c(0,max(data$mean*1.5))) 

# add horizontal bars 
gg <- gg + geom_errorbar(aes(ymax = hline, ymin = hline), width = 0.45) 

# add vertical bars 
gg <- gg + geom_linerange(aes(ymax = max(data$mean)+3, ymin = max(data$mean)+1), position = position_dodge(0.9)) 

# add asterisks 
gg <- gg + geom_text(data = data[1:2], aes(y = max(data$mean)+4), label = ifelse(data$p_value[1:2] <= 0.4, "*", ifelse(data$p_value[1:2] <= 0.05, "*", "")), size = 8) 

gg 

plot output