Open In App

Adding Significance Levels and Asterisks to Plots in R

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The significance level (alpha) is the probability of rejecting a true null hypothesis i.e Type I error. It is commonly expressed using p-values which range from 0 to 1. In simpler words, it helps in understanding whether differences in data are meaningful or due to random chance. The standard significance levels are represented using asterisks:

" *** " : p < 0.001 (highly significant)
" ** " : p < 0.01 (moderately significant)
" * " : p ≤ 0.05 (weakly significant)
" NS " : Not significant

In R geom_signif() function from ggsignif package helps in adding significance levels to plots.

Syntax:

geom_signif(data, stat, position, comparisons, map_signif_level)

Where

  • data: Dataset to be used.
  • stat: Specifies the statistical transformation (use "signif").
  • position: Adjusts placement of annotations ("identity" default).
  • comparisons: Specifies pairs of groups to compare.
  • map_signif_level: TRUE to use default significance annotations, FALSE to customize.

Adding Significance Levels or Asterisks to Bar Plots

1. Install the required packages

To add significance level and asterisks to Barplot in R first we need to install and load the required packages ggplot2 for visualization and ggsignif for adding significance levels.

  • install.packages(): Installs the package if it's not already installed.
  • library(): Loads the package .
R
install.packages("ggplot2")
install.packages("ggsignif")
library(ggplot2)
library(ggsignif)

2. Create a Sample Dataset

We create a simple dataset which contains the number of placements across different branches. We use data.frame() to create a dataframe .

R
data <- data.frame(
  Branches<-c('CSE','ECE','MECH','EEE','CIVIL'),
  Placements <-c(190,98,90,90,75))

3. Plot Bar Graph Without Significance Level

We generate a basic bar plot using ggplot showing placements in different branches.

  • ggplot(data, aes(x = Branches, y = Placements)) : initializes a ggplot object using the dataset "data" and sets "Branches" on the x-axis and "Placements" on the y-axis.
  • geom_bar(...): creates a bar chart
  • stat = "identity": ensures that the bar heights reflect actual values from the dataset.
  • aes(fill = Branches): assigns different colors to each branch.
R
gbar <- ggplot(data,aes(x=Branches,y=Placements))+
  geom_bar(stat="identity",aes(fill=Branches))

gbar

Output:

bar_plot
Bar Plot without significance

4. Add Significance Levels

We are going to add a Significance Level to enhance the plot. The "gbar +" function adds a new layer to the existing gbar bar plot. Here:

  • geom_signif() is used to add significance annotations between specific categories in a plot.
  • data = data: Specifies the dataset used for significance comparison.
  • stat = "signif": Uses the statistical transformation to determine significance.
  • position = "identity": Keeps the position of elements unchanged.
  • comparisons = list(c("CIVIL", "EEE")): Compares the CIVIL and EEE branches.
  • map_signif_level = TRUE: Displays significance levels in the plot.
R
gbar+geom_signif(data=data,stat="signif",position="identity",
                 comparisons=list(c("CIVIL","EEE")),map_signif_level = TRUE)

Output:

barplot
Bar Plot with Significance level

We can observe that the bar plot remains the same but a significance annotation usually a line with a p-value appears between the CIVIL and EEE bars indicating whether the difference in placements is statistically significant.

5. Adding Asterisks to the Barplot in R

Instead of default p-values we can also use asterisks to indicate significance levels . The "gbar +" continues from the previously created bar plot gbar, adding a new annotation layer. Here :

  • map_signif_level = TRUE: enables the automatic mapping of significance levels, allowing significance values to be displayed.
  • annotations = "***": overrides the default significance annotation by explicitly displaying "***" above the comparison bar
R
gbar+geom_signif(data=data,stat="signif",position="identity",
                 comparisons=list(c("CIVIL","EEE")),map_signif_level = TRUE,annotations="***")

Output:

barplot
Astericks in Barplot

We can observe that the bar plot remains the same but a significance annotation (three asterisks ***) appears above the bars of CIVIL and EEE, highlighting that their difference is statistically significant.

Adding Significance Levels or Asterisks to Box Plots

1. Install required packages and Creating Sample Dataset

We first install and load the necessary libraries ggplot2 and ggsignif. Then we create a simple dataset with three groups (A ,B and C) and corresponding values.

R
install.packages("ggplot2")
install.packages("ggsignif")
library(ggplot2)
library(ggsignif)

data <- data.frame(
    Groups=c('A','B','A','C','A','B','B','C'),
    Values=c(1,2,3,2,3,1,3,1))

2. Plotting the Box Plot using ggplot without Significance Level and Stars.

we create a boxplot which shows the interquartile range (IQR), the horizontal line inside the box represents the median and the whiskers extend to the minimum and maximum values. Outliers if any are represented by dots.

  • ggplot initializes a ggplot object using "data", where:
  • "Groups" represents the categorical variable (x-axis).
  • "Values" represents the numerical variable (y-axis).
  • geom_boxplot(): adds a boxplot layer to the plot.
R
gbox <- ggplot(data,aes(x=Groups,y=Values))+
  geom_boxplot()
gbox

Output:

boxplot
Boc Plot without Significance Level

3. Adding Significance Level to the BoxPlot in R

We are going to add a Significance Level to the plot using geom_signif().

  • comparisons=list(c("A", "B")): Specifies that we want to compare groups "A" and "B".
  • By default the function computes the p-value and displays "NS" (Not Significant) if no significant difference is found.
R
gbox+geom_signif(data=data,comparisons=list(c("A","B","C")))

Output:

boxplot
Boc Plot with Significance Level

Here we can observe that the boxplot remains unchanged in structure but a horizontal line will be drawn above groups "A" and "B" indicating they are being compared. If significance is detected an appropriate annotation will be displayed above the line.

5. Adding asteriks to the Boxplot in R

Here we are changing the annotation of the default significance level and placing some strings in place of it.

  • geom_signif() adds a significance annotation comparing "A" and "B" in the plot.
  • map_signif_level = TRUE : enables the automatic mapping of significance levels allowing significance values to be displayed.
  • annotations="***" : overrides the default significance annotation by explicitly displaying "***" above the comparison bar.
R
gbox+geom_signif(data=data,comparisons=list(c("A","B")),map_signif_level = TRUE,annotations="***")

Output:

boxplot
Boc Plot with Astericks

We can observe that ,the boxplot remains unchanged but a horizontal significance bar will be drawn above the "A" and "B" groups and the annotation "***" will be displayed above the line manually indicating strong statistical significance.


Next Article

Similar Reads