[英]Barplot with significant differences and interactions?
我想想象我的數據和ANOVA統計數據。 通常使用帶有添加線條的條形圖來指示顯着的差異和相互作用。 你怎么用R做這樣的情節?
這就是我想要的:
我目前正在使用barplot2{ggplots}
繪制條形圖和置信區間,但我願意使用任何包/程序來完成工作。 為了得到我目前使用的統計數據TukeyHSD{stats}
或pairwise.t.test{stats}
的差異和方差分析功能(一個aov
, ezANOVA{ez}
, gls{nlme}
)的相互作用。
只是為了給你一個想法,這是我目前的情節:
當您使用庫gplots
函數barplot2()
,將使用此方法給出示例。
首先,在barplot2()
函數的幫助文件中給出了barplot2()
。 ci.l
和ci.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))
在繪制條形圖之后,使用sapply()
)使用計算的坐標制作五個線段(因為這次有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)
我想現在你的問題已經或多或少得到了解決,所以我會鼓勵你使用不同的方法,這種方法在數據的可視化表示方面要好得多 - 點圖。 例如,將您的條形圖與使用類似數據點構建的點圖進行比較:
#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)))
將其與barplot進行比較。 在點圖中,水平繪制時更容易看到差異,您不需要額外的圖例或條形或顏色來顯示條件,您不需要指南和其他噪聲元素。 你擁有這三個面板中的所有內容。 當然,我知道你可能想突出你的重要影響,並且它可能適用於少數條件。 但是如果因子的數量增加,情節會溢出星星和狗屎。
把事情簡單化。 保持dotplot。 請查看William Cleveland和Edward Tufte的書籍。
我建議使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.