Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple comparisons with geom_signif function, R

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

like image 491
J.Con Avatar asked Oct 16 '25 00:10

J.Con


1 Answers

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

like image 125
erc Avatar answered Oct 18 '25 16:10

erc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!