Reputation: 4309
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)
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
Upvotes: 4
Views: 15597
Reputation: 10131
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)
Upvotes: 5