How to mark pvalue significance in R language?

Time:2021-11-9

Author: a cat that wants to fly
Reviewer: Tong Meng
Edit: Angelica

Box graph is one of the most common graphs in statistics. This article will describe how to simply compare the average values of two or more groups and add significance markers.

Generally, it can be divided into four categories according to the value of significance p value:
(1)0.01≤p<0.05,*
(2)0.001≤p<0.01,**
(3)0.0001≤p<0.001,***
(4)p<0.0001, ****
Next, we will describe three ways to add significance markers.

Method 1 – add manually

1: Create data and draw box diagram.

library(ggplot2)
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_boxplot()
How to mark pvalue significance in R language?

2: Manually add significance markers.

library(ggplot2)
###Sets the position of the four points of the horizontal line
df2 <- data.frame(a = c(2,2,3,3), b = c(8,8.1,8.1,8))
ggplot(iris, aes(x=Species, y=Sepal.Length))+
  geom_boxplot()+
  ###Draw the line according to the set position
  geom_line(data = df2, aes(x = a, y = b)) + 
  ###Add significance marker information
  annotate("text", x = 2.5, y = 8.2, label = "***", size = 8)
How to mark pvalue significance in R language?

The disadvantage of this method is that when adding significance manually, it is necessary to confirm the p value of significance.

Method 2 – use ggsign

1: Download and install the ggsign package.

install.packages("ggsignif")

2: Create data and draw box diagram.

library(ggplot2) 
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot()
How to mark pvalue significance in R language?

3: Use the ggsign R package to add significance markers.

library(ggplot2) 
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
###Add information about the columns to compare
geom_signif(comparisons = list(c("versicolor", "virginica")),
###P value < 0.05
map_signif_level=TRUE)
How to mark pvalue significance in R language?

Method 3 – use ggpubr

1: Download and install the ggpubr package.

install.packages("ggpubr")

2: Create data and draw box diagram.

library(ggpubr)
###Add a list of pairwise comparisons
my_comparisons <- list(c("setosa", "versicolor"), c("setosa", "virginica"), c("versicolor", "virginica"))
ggboxplot(iris, x="Species", y="Sepal.Length",color = "Species",palette = "jco", add = "jitter")
How to mark pvalue significance in R language?

3: Use the ggpubr R package to add significance markers.

library(ggpubr)
my_comparisons <- list(c("setosa", "versicolor"), c("setosa", "virginica"), c("versicolor", "virginica"))
ggboxplot(iris, x="Species", y="Sepal.Length",color = "Species",palette = "jco", add = "jitter")+
  ###Add the statistical results of multi group comparison
  stat_compare_means(label.y = 9.5)+
  ###Add significance marker location information for each pairwise comparison
  stat_compare_means(comparisons=my_comparisons, label.y = c(7.6, 8.4, 8.0), label ="p.signif")
How to mark pvalue significance in R language?

The R package can increase the p-value value between multiple groups and the comparison between specified groups.


I believe you have learned about the three methods of adding. If you have any better methods or other things you want to share, you are also welcome to contribute to us.