Barplot有显着的差异和相互作用?

时间:2013-03-20 22:07:40

标签: r plot interaction significance

我想想象我的数据和ANOVA统计数据。通常使用带有添加线条的条形图来指示显着的差异和相互作用。你怎么用R做这样的情节?

这就是我想要的:

显着差异:

significant differences

重要的互动:

significant interactions

背景

我目前正在使用barplot2{ggplots}绘制条形图和置信区间,但我愿意使用任何包/程序来完成工作。要获取统计信息,我目前正在使用TukeyHSD{stats}pairwise.t.test{stats}进行差异,并使用其中一个anova函数(aovezANOVA{ez}gls{nlme})进行交互。< / p>

只是为了给你一个想法,这是我目前的情节: barplot2 with CIs

3 个答案:

答案 0 :(得分: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))

在制作条形图后,使用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)

enter image description here

答案 1 :(得分:3)

我想现在你的问题已被或多或少地解决了,所以我会鼓励你使用不同的方法,这种方法在数据的可视化表示方面要好得多 - 点图。例如,将您的条形图与使用类似数据点构建的点图进行比较:

#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的书籍。

答案 2 :(得分: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