Add Significance Level & Stars to the box plot

The process of adding significance levels and stars to a box plot involves visually indicating the statistical significance of differences between groups within the plot. In the below example, the ggpubr package is used to add significance levels and stars to a box plot of petal length by species in the iris dataset. We can install ggpubr package using below command

install.packages("ggpubr")

Specifically, the stat_compare_means() function is used to add the significance levels and stars. This function computes the specified statistical test (in this case, a t-test) and adds a label to the plot with the resulting p-value. The comparisons argument specifies which groups to compare, and the method argument specifies the statistical test to use. The appearance of the significance level and stars is controlled by arguments such as size, vjust, and tip.length.

R




# Load the required packages
library(ggplot2)
library(ggpubr)
 
# Load a dataset.
data(iris)
 
# Create the box plot using `ggplot()`
# function with `aes()` mapping the x-axis
# to `Species` and y-axis to `Petal.Length`.
p <- ggplot(iris, aes(x = Species,
                      y = Petal.Length)) +
 
  # Fill the boxes with light blue color
  # and black border.
  geom_boxplot(fill = "lightblue",
               color = "black") +  
 
  # Add label for y-axis.
  ylab("Petal Length")  
 
# Add significance level and stars using
# `stat_compare_means()` function.
# Here, comparisons are made between
# "versicolor" and "setosa" and "virginica" and "versicolor" species.
p + stat_compare_means(comparisons = list(c("versicolor",
                                            "setosa"),
                                          c("virginica",
                                            "versicolor")),
                       label = "p.format",
                       method = "t.test",
                       size = 8,
                       vjust = -1.5,
                       tip.length = 0.01)


Explanation: In the above code, we first load the ggplot2 and ggpubr packages. We then load a pre-defined dataset, in this case, the iris dataset. We create a boxplot of the Petal.Length variable grouped by Species. We then use the stat_compare_means() function from the ggpubr package to add significance level and stars to the plot. The comparisons argument specifies the pairwise comparisons to be made (in this case, comparing the mean Petal.Length of versicolor to setosa, and virginica to versicolor). The method argument specifies the statistical test to be used (in this case, a t-test). The label argument specifies the format of the p-value labels. Finally, we specify the appearance of the significance level and stars using the size, vjust, and tip.length arguments.

Output:

 



Grouped Bar Graphs and Facet_Wrap in R

In this article, we are going to learn how to define data when using ggsignif with grouped bar graphs and facet_wrap in R programming language.

ggplot2 is a popular R Language package used for data visualization. It allows users to create a wide range of plots and graphs, including bar graphs. However, adding statistical significance bars to bar graphs can be a bit tricky. That’s why ggsignif, another R package, comes in handy. ggsignif provides an easy way to add significance bars to bar graphs created with ggplot2. In this article, we will explore how to define data when using ggsignif with grouped bar graphs and facet_wrap in R.

Installing required packages

Execute the below commands to install the dplyr, ggplot2, and ggsignif packages in R respectively.

install.packages("dplyr")
install.packages("ggplot2")
install.packages("ggsignif")

Similar Reads

Prepare the Data

We will start by creating some sample data to work with. We will use the mtcars dataset which is an inbuilt dataset in R. The mtcars dataset contains information about various car models, including the number of cylinders, horsepower, and miles per gallon (mpg)....

Create a Grouped Bar Graph

...

Add Significance Bars with ggsignif

Next, we will create a grouped bar graph using the ggplot2 package. We will use the geom_bar() function to create the bar graph and facet_wrap() function to create separate plots for each number of cylinders....

Add Significance Level & Stars to the box plot

...