Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding significance brackets to ridgeline plot

Tags:

r

ggplot2

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.

like image 586
pRometheus80 Avatar asked Jul 30 '26 08:07

pRometheus80


2 Answers

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")

enter image description here

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")

enter image description here

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")

enter image description here

like image 157
Ventrilocus Avatar answered Jul 31 '26 23:07

Ventrilocus


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)

like image 34
Indrajeet Patil Avatar answered Aug 01 '26 00:08

Indrajeet Patil