表示条形图基数 R 的统计显着差异

Indicating the statistically significant difference in bar graph base R

这个问题之前已经被问过 post:使用 R 表示条形图在统计上的显着差异。但是,他们想知道如何使用 ggplot2 来做到这一点。我想知道您如何仅使用基本包或函数 barplot() 来执行此操作。 我想要如下图所示的东西:

http://i.stack.imgur.com/3I6El.jpg

我当前的代码:

barcenter3<- barplot(newMEANs3$Percent_Viability, names.arg=c("Control", "Cyp28d1", "A3", "A4"), ylab = "Average Emergent",  ylim=c(0, 1.1), xlab= "RNAi Line", main = "Trip Nicotine UAS-RNAi Emergents")
segments(barcenter3, newMEANs3$Percent_Viability-newSDs3$Percent_Viability, barcenter3, newMEANs3$Percent_Viability+newSDs3$Percent_Viability, lwd=1);
segments(barcenter3 - 0.1, newMEANs3$Percent_Viability-newSDs3$Percent_Viability, barcenter3  + 0.1, newMEANs3$Percent_Viability-newSDs3$Percent_Viability, lwd=1);
segments(barcenter3 - 0.1, newMEANs3$Percent_Viability+newSDs3$Percent_Viability, barcenter3  + 0.1, newMEANs3$Percent_Viability+newSDs3$Percent_Viability, lwd=1);
dev.off();

我想添加对比对比的p值

这是执行此操作的简单函数。

## Sample Data
means <- seq(10,40,10)
pvals <- seq(0.01, 0.05, 0.02)

barPs <- function(means, pvals, offset=1, ...) {
    breaks <- barplot(means, ylim=c(0, max(means)+3*offset), ...)
    ylims <- diff(means) + means[-length(means)] + offset
    segments(x0=breaks[-length(breaks)], y0=ylims, x1=breaks[-1], y1=ylims)
    segments(x0=c(breaks[-length(breaks)], breaks[-1]),
             y0=rep(ylims, each=2), y1=rep(ylims-offset/2, each=2))
    text(breaks[-length(breaks)]+diff(breaks[1:2])/2, ylims+offset, 
         labels=paste("p=", pvals))
}

barPs(means, pvals, offset=1, main="Bar w/ P-value", 
      names.arg=toupper(letters[1:4]))