Indicating the statistically significant difference in bar graph USING R

Jim Bo picture Jim Bo · Feb 19, 2013 · Viewed 40.9k times · Source

This is a repeat of a question originally asked here: Indicating the statistically significant difference in bar graph but asked for R instead of python.

My question is very simple. I want to produce barplots in R, using ggplot2 if possible, with an indication of significant difference between the different bars, e.g. produce something like this. I have had a search around but can't find another question asking exactly the same thing.

bar plot showing error bars and sig. diff marking beween bars

Answer

const-ae picture const-ae · Apr 5, 2017

I know that this is an old question and the answer by Didzis Elferts already provides one solution for the problem. But I recently created a ggplot-extension that simplifies the whole process of adding significance bars: ggsignif

Instead of tediously adding the geom_path and annotate to your plot you just add a single layer geom_signif:

library(ggplot2)
library(ggsignif)

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica")), 
              map_signif_level=TRUE)

Boxplot with significance bar

Full documentation of the package is available at CRAN.