Adding Significance Levels and Asterisks to Plots in R
Last Updated :
14 Apr, 2025
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 s
ets "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 without significance4. 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:
Bar Plot with Significance levelWe 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:
Astericks in BarplotWe 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(): a
dds a boxplot layer to the plot.
R
gbox <- ggplot(data,aes(x=Groups,y=Values))+
geom_boxplot()
gbox
Output:
Boc Plot without Significance Level3. 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:
Boc Plot with Significance LevelHere 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()
a
dds 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:
Boc Plot with AstericksWe 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.
Similar Reads
Adding Text to Plots in R programming - text() and mtext () Function
Text is defined as a way to describe data related to graphical representation. They work as labels to any pictorial or graphical representation.1. text() Function in Rtext() Function in R Programming Language is used to draw text elements to plots in Base R.Syntax: text(x, y, labels)Parameters:Â x an
3 min read
Axes and Scales in Lattice Plots in R
In R Programming Language, lattice graphics are a powerful method for displaying data. They offer a versatile and dependable framework for making many other types of plots, such as scatterplots, bar charts, and more. Understanding how to adjust and modify the axes and scales is a crucial component i
4 min read
Adding Notches to Box Plots in R
In this article, we will study how to add a notch to box plot in R Programming Language. The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be installed into the working space
2 min read
Adding x and y Axis Label to ggplot-grid Built with cowplot in R
ggplot2 is one of the most widely used libraries for creating elegant data visualizations in R. Often, you will want to arrange multiple plots together to create a grid layout for comparison or presentation purposes. cowplot is a popular extension that simplifies the arrangement of multiple ggplot o
4 min read
Adding axis to a Plot in R programming - axis () Function
axis() function in R Language is to add axis to a plot. It takes side of the plot where axis is to be drawn as argument. Syntax: axis(side, at=NULL, labels=TRUE) Parameters: side: It defines the side of the plot the axis is to be drawn on possible values such as below, left, above, and right. at: Po
2 min read
How to add a specific point to legend in ggvis in R?
In this article, we will be going through the approach to add a specific point to a legend in ggvis package in the R programming language. The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. T
4 min read
How To Add Circles Around Specific Data Points in R
In this article, we will discuss how to add circles around specific data points in R  Programming Language. Method 1: Using geom_point method The ggplot2 package in R is used to perform data visualization. To install this package type the below command in the terminal. Syntax: install.packages("gg
3 min read
Add Vertical and Horizontal Lines to ggplot2 Plot in R
In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language. Adding Vertical Line To R Plot using geom_vline() For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position. Syntax: geom_vline(xint
4 min read
How to Make a Scatter Plot Matrix in R
A scatterplot matrix is ââa grid of scatterplots that allows us to see how different pairs of variables are related to each other. We can easily generate a scatterplot matrix using the pairs() function in R programming. In this article, we will walk through the process of creating a scatterplot matr
6 min read
How to Change Axis Intervals in R Plots?
In this article, we will be looking at the different approaches to change axis intervals in the R programming language. Method 1: Using xlim() and ylim() functions in base R In this method of changing the axis intervals, the user needs to call the xlim() and ylim() functions, passing the arguments o
3 min read