Add Significance Level and Stars to Plot in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Add Significance Level and Stars to Plot in R appeared first on Data Science Tutorials

What do you have to lose?. Check out Data Science tutorials here Data Science Tutorials.

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

The post Add Significance Level and Stars to Plot in R appeared first on Data Science Tutorials

Learn how to expert in the Data Science field with Data Science Tutorials.

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)