Skip to content

Data Science Tutorials

For Data Science Learners

  • Best Books on Generative AI Course
  • Filtering for Unique Values
    Filtering for Unique Values in R- Using the dplyr R
  • How to Get a Job as a Data Engineer
    How to Get a Job as a Data Engineer? R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to Recode Values in R
    How to Recode Values in R R
  • What is the best way to filter by row number in R?
    What is the best way to filter by row number in R? R
  • How to Compare Two Excel Sheets for Differences
    How to Compare Two Excel Sheets for Differences Excel
  • Number to Percentage in R
    Number to Percentage in R R
Add Significance Level and Stars to Plot in R

Add Significance Level and Stars to Plot in R

Posted on November 19November 19 By Admin No Comments on Add Significance Level and Stars to Plot in R

Add Significance Level and Stars to Plot in R, In this article, I’ll show you how to use the R programming language’s ggsignif package to annotate a ggplot2 plot with significance levels.

Boxplot with Stars for Significance

The R programming language’s box-and-whisker plot with significance levels is demonstrated in the following R code.

To do this, we must first produce an illustrative data collection.

set.seed(123)                                 
data <- data.frame(group = rep(LETTERS[1:4], each = 100),
                       value = c(rnorm(100),
                                 rnorm(100, 3),
                                 rnorm(100),
                                 rnorm(100, - 5)))
head(data)                                  
    group       value
1     A -0.56047565
2     A -0.23017749
3     A  1.55870831
4     A  0.07050839
5     A  0.12928774
6     A  1.71506499

How to perform the MANOVA test in R? – Data Science Tutorials

In this tutorial, we’ll use the ggplot2 software to plot our data. We must first install and load the ggplot2 package before we can use any of its features.

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

The code below can be used to create a boxplot without significance levels in the next step:

ggp_box <- ggplot(data,                     
aes(x = group,
y = value)) +
geom_boxplot()
ggp_box                                          

Assume we wish to determine whether the various boxplots (i.e., the various groups in our data) differ considerably.

Let’s also assume that we want to include the significance levels in our image.

How to Add a caption to ggplot2 Plots in R? (datasciencetut.com)

The ggsignif package needs to be installed and loaded first.

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

With the help of the ggsignif package, created by Constantin Ahlmann-Eltze and Indrajeet Patil, you can add group-wise comparisons to your ggplots2 graphs.

We can use the geom_signif function (or, alternatively, the geom_stat function) as demonstrated below to achieve this.

We must specify the groups we want to compare when using the geom_signif function.

How to create a hexbin chart in R – Data Science Tutorials

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

By using the aforementioned syntax, a ggplot2 boxplot compares groups A and B and has a significant level.

We compared our groups in the previous graphic using the p-value. By setting the map signif_level option to TRUE, we can display significant stars instead.

ggp_box +                                        
geom_signif(comparisons = list(c("A", "B")),
              map_signif_level = TRUE)

Additionally, it is possible to compare numerous groups at once.

In order to accomplish this, we must expand our comparison list, and in order to prevent visual overlap, we must additionally define the points on the y-axis where we want to display the important stars.

Free Data Science Course-Online 2022 ยป

ggp_box +                                     
geom_signif(comparisons = list(c("A", "B"),
                                 c("A", "C")),
              map_signif_level = TRUE,
              y_position = c(7.5, 9))

The significance levels’ layout can also be changed by the user using the geom_signif function.

As demonstrated here, we may alter the font size, line spacing, and color.

ggp_box +                                       
geom_signif(comparisons = list(c("A", "B"),
                                 c("A", "C")),
              map_signif_level = TRUE,
              y_position = c(7.5, 9.5),
              col = 2,
              size = 2,
              textsize = 5) +
  ylim(- 8, 12)

How to use the image function in R – Data Science Tutorials

R

Post navigation

Previous Post: Cross-validation in Machine Learning
Next Post: How to compare the performance of different algorithms in R?

Related Posts

  • Display the structure in R R
  • Anderson-Darling Test in R With Examples
    Anderson-Darling Test in R With Examples R
  • Group By Sum in R
    Group By Sum in R R
  • Convert a continuous variable to a categorical in R R
  • Adding Subtitles in ggplot2 R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Maximizing Model Accuracy with Train-Test Splits in Machine Learning
  • Type II Errors in R
  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • OLS Regression in R
    OLS Regression in R R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • How to Display Percentages on Histogram IN R
    How to Display Percentages on Histogram in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • ChatGPT Coursera Review
    ChatGPT Coursera Review Course
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • Difference between R and Python
    Difference between R and Python R
  • Export output as text in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme