Add Significance Level and Stars to Plot in R, In this article, I’ll show you how to use the R programming language’s ggsignif package to annotate a ggplot2 plot with significance levels.
Boxplot with Stars for Significance
The R programming language’s box-and-whisker plot with significance levels is demonstrated in the following R code.
To do this, we must first produce an illustrative data collection.
data <- data.frame(group = rep(LETTERS[1:4], each = 100), value = c(rnorm(100), rnorm(100, 3), rnorm(100), rnorm(100, - 5))) head(data)
group value 1 A -0.56047565 2 A -0.23017749 3 A 1.55870831 4 A 0.07050839 5 A 0.12928774 6 A 1.71506499
In this tutorial, we’ll use the ggplot2 software to plot our data. We must first install and load the ggplot2 package before we can use any of its features.
The code below can be used to create a boxplot without significance levels in the next step:
ggp_box <- ggplot(data, aes(x = group, y = value)) + geom_boxplot() ggp_box
Assume we wish to determine whether the various boxplots (i.e., the various groups in our data) differ considerably.
Let’s also assume that we want to include the significance levels in our image.
The ggsignif package needs to be installed and loaded first.
With the help of the ggsignif package, created by Constantin Ahlmann-Eltze and Indrajeet Patil, you can add group-wise comparisons to your ggplots2 graphs.
We can use the geom_signif function (or, alternatively, the geom_stat function) as demonstrated below to achieve this.
We must specify the groups we want to compare when using the geom_signif function.
ggp_box + geom_signif(comparisons = list(c("A", "B")))
By using the aforementioned syntax, a ggplot2 boxplot compares groups A and B and has a significant level.
We compared our groups in the previous graphic using the p-value. By setting the map signif_level option to TRUE, we can display significant stars instead.
ggp_box + geom_signif(comparisons = list(c("A", "B")), map_signif_level = TRUE)
Additionally, it is possible to compare numerous groups at once.
In order to accomplish this, we must expand our comparison list, and in order to prevent visual overlap, we must additionally define the points on the y-axis where we want to display the important stars.
ggp_box + geom_signif(comparisons = list(c("A", "B"), c("A", "C")), map_signif_level = TRUE, y_position = c(7.5, 9))
The significance levels’ layout can also be changed by the user using the geom_signif function.
As demonstrated here, we may alter the font size, line spacing, and color.
ggp_box + geom_signif(comparisons = list(c("A", "B"), c("A", "C")), map_signif_level = TRUE, y_position = c(7.5, 9.5), col = 2, size = 2, textsize = 5) + ylim(- 8, 12)