Boxplot with Significance Level and Stars

Steps to add significance level and Stars (asterisks) to Boxplot in R:

Step 1: First we need to install and load the required packages.

R




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


Step 2: Next we need to create/import the Dataframe we want to plot.

R




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


Step 3: Plotting the Box Plot using ggplot without Significance Level and Stars.

R




gbox <- ggplot(data,aes(x=Groups,y=Values))+
  geom_boxplot()
gbox


Output:

 

Adding Significance Level to the BoxPlot in R

We are going to add a Significance Level to the plot.

R




gbox+geom_signif(data=data,comparisons=list(c("A","B")))


Output:

 

Adding a star to the Boxplot in R

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

R




gbox+geom_signif(data=data,comparisons=list(c("A","B")),map_signif_level = TRUE,annotations="***")


Output:
 

 



Add Significance Level & Stars to Plot in R

The significance level of a graph is defined as the probability of the wrong elimination of the null hypothesis even though it is true. It’s represented in the form P-Value which lies between 0 and 1. In R Programming the significance levels and stars to the plot are added using geom_signif() function of ggsignif package.

Syntax of geom_signif():

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

Where,

  • data – The data need to be displayed on the layer.
  • stat – The statistical transformation used on the data(“signif”)
  • position – The string or function to adjust the position(“identity”)
  • comparisons – A list of length-2 vectors where the significance level need to be compared.
  • map_signif_level – Set to TRUE to display the default significance level annotation else FALSE.

Barplot with Significance Levels and Stars

Steps to add significance level and Stars (asterisks) to Barplot in R.

Step 1: First, we need to install and load the required packages.

R




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


Step 2: Next we need to create/import the Dataframe we want to plot.

R




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


Step 3: Plot the Bar Graph using ggplot without Significance Level and Stars.

R




gbar <- ggplot(data,aes(x=Branches,y=Placements))+
  geom_bar(stat="identity",aes(fill=Branches))
 
gbar


Output:

 

Adding Significance Level to the Plot in R

We are going to add a Significance Level to the plot.

R




gbar+geom_signif(data=data,stat="signif",position="identity",
                 comparisons=list(c("CIVIL","EEE")),map_signif_level = TRUE)


Output:

 

Adding Asterisks to the Barplot in R

This is nothing but we are changing the annotation of the default significance level and placing some string in place of it. In general, the stars are places based on the significance level as follows:

  • “***” – if there is no significance(NS) among them or the significance level is less than 0.001
  • “**” – the significance level is less than 0.01
  • “*” – the significance level is less than or equal to 0.1

Note: It’s not mandatory to follow the same number of stars based on significance levels one can also define his own number of stars to be plotted based on significance levels.

R




gbar+geom_signif(data=data,stat="signif",position="identity",
                 comparisons=list(c("CIVIL","EEE")),map_signif_level = TRUE,annotations="***")


Output:

 

Similar Reads

Boxplot with Significance Level and Stars

...