I am creating a ridge plot to compare a few groups (using ggridges package) and would like to add significance brackets to show comparisons between some group levels (using ggsignif package).
But this doesn't seem to work because the computation fails in stat_ggsignif.
Here is a reproducible example:
set.seed(123)
library(ggsignif)
library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_density_ridges(scale = 1) +
coord_flip() +
geom_signif(comparisons = list(c("setosa", "versicolor")))
#> Picking joint bandwidth of 0.181
#> Warning in f(..., self = self): NAs introduced by coercion
#> Warning: Computation failed in `stat_signif()`:
#> missing value where TRUE/FALSE needed

Created on 2021-07-29 by the reprex package (v2.0.0)
How can I get these two packages to work with each other? Thanks.
I did not manage to combine A) geom_density_ridges and B) geom_signif. The reason is that (A) requires numerical variable as x and categories as y, while (B) requires numerical variable as y and categories as x. And I have not managed to overwrite this behaviour. But I assume that you have chosen ridge_plots over simple boxplots as you are interested in a more informative visualization of the distribution. To do so, there is a much better solution than ridge_plots, the so called violin plots. See below a standard boxplot (with labelled significance):
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot() +
geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

See below a violin plot (with jitter and labelled significance):
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_violin(trim = F) + geom_jitter() +
geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

This does the job unless you are particularly interest in making ggridges and ggsignif work together. Please note that a violin plot is just a folded density plot (see https://en.wikipedia.org/wiki/Violin_plot#:~:text=A%20violin%20plot%20is%20a,by%20a%20kernel%20density%20estimator for more details).
For the same purpose, see also the sina plot (suggestion by tjebo):
library(ggforce)
ggplot(iris, aes(x = Species, y = Sepal.Length, colour = Species)) +
geom_sina() +
geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

Thanks to a new pull request to ggsignif, the following now works:
set.seed(123)
library(ggsignif)
library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_density_ridges(scale = 1) +
coord_flip() +
geom_signif(comparisons = list(c("setosa", "versicolor")),
y_position = 9)
#> Picking joint bandwidth of 0.181

Created on 2021-08-06 by the reprex package (v2.0.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With