3

The package ggsignif is very useful for quickly and easily indicating significant comparisons in ggplot graphs. However the comparisons call requires manual typing of each pair of values to be compared.

Eg.

library(ggplot2)
library(ggsignif)

data(iris)

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

enter image description here

I'm wondering how this could be circumvented by referring to all the possible combinations at once? For example, expand.grid(x = levels(iris$Species), y = levels(iris$Species)), gives all the combinations

           x          y
1     setosa     setosa
2 versicolor     setosa
3  virginica     setosa
4     setosa versicolor
5 versicolor versicolor
6  virginica versicolor
7     setosa  virginica
8 versicolor  virginica
9  virginica  virginica

But how to have this accepted by geom_signif(comparisons=...?

Package info is available here https://cran.r-project.org/web/packages/ggsignif/index.html

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
J.Con
  • 4,101
  • 4
  • 36
  • 64
  • 4
    `combn(levels(iris$Species), 2)` will give you all three pairwise combinations – Adam Quek May 16 '17 at 03:29
  • @AdamQuek using that without `list()` gives the error, `Warning message: Computation failed in stat_signif(): not enough 'y' observations`. Using with `list()` only adds the first comparison to the plot. – J.Con May 16 '17 at 03:42
  • Not answering your query on `geom_signif` (never used it and don't intend to). Just suggesting that `combn` is the more sensible way to get pairwise combination instead of `expand.grid`. – Adam Quek May 16 '17 at 03:44
  • @AdamQuek Oh. Well thanks. – J.Con May 16 '17 at 03:46

1 Answers1

4

Building on the comment of Adam Quek you just need to transpose the created matrix and turn each row into a list:

split(t(combn(levels(iris$Species), 2)), seq(nrow(t(combn(levels(iris$Species), 2)))))

$`1`
[1] "setosa"     "versicolor"

$`2`
[1] "setosa"    "virginica"

$`3`
[1] "versicolor" "virginica" 

ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = split(t(combn(levels(iris$Species), 2)), seq(nrow(t(combn(levels(iris$Species), 2))))), 
              map_signif_level = TRUE)

enter image description here

erc
  • 10,113
  • 11
  • 57
  • 88