ggsignif intro

Constantin Ahlmann-Eltze

2017-04-26

This package provides an extension for the popular ggplot2 package.

Often people are looking for a way to quickly annotate if the difference between 2 groups in a plot is significantly different (look at all those questions). The usual answer is along the lines of just manually adding lines and annotation to the plot.

But I believe that a fundamental strength of ggplot is that it allows one to quickly make advanced plots by combining layers of visualization, which can encapsulate complex statistical methods (geom_smooth, geom_contour etc.).

So I created this package which provides a single layer geom_signif which can calculate the significance of a difference between groups and add the annotation to the plot in a single line.

How to use it?

First load poth packages

library(ggplot2)
library(ggsignif)

Second step: plot your data

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

That’s it, it is really simple!

Advanced options

Sometimes one might need more advanced control over the display. Following the patterns of other ggplot layers, one just provides the data and necessary aesthetics and the layer takes care of the rest.

dat <- data.frame(Group = c("S1", "S1", "S2", "S2"),
                  Sub   = c("A", "B", "A", "B"),
                  Value = c(3,5,7,8))  

ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),
                              y=c(5.8, 8.5), annotation=c("**", "NS")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation, group=c(1,2))) +
  geom_signif(comparisons=list(c("S1", "S2")), annotations="***",
              y_position = 9.3, tip_length = 0, vjust=0.4) +
  scale_fill_manual(values = c("grey80", "grey20"))

For more detailed documentation of the available parameters see the man page for the geom_signif function (> ?geom_signif).

Bugs, Comments or Questions?

If you have any problems with the package, just file an issue at https://github.com/Artjom-Metro/ggsignif.