By indicating a statistically significant difference in the base of histograms R

This was asked earlier in this post: indicating a statistically significant difference in a histogram USING R. However, they wanted to know how to do this using ggplot2. I was wondering how you do this using just the basic package or the barplot () function. I need something similar to this snapshot below:

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

my current code:

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

      

I want to add p value comparing contrast.

+3


source to share


1 answer


Here's a simple function to do this.

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

      



enter image description here

+5


source







All Articles