An R library called ggpval allows us to perform statistical tests by adding the corresponding p-value to the ggplot charts.
How to Install the ggpval library
We can install the ggpval library either from CRAN or from GitHub
# Install `ggpval` from CRAN: install.packages("ggpval") # You can install the lastest ggpval from github with: # install.packages("devtools") devtools::install_github("s6juncheng/ggpval")
Let’s run a simple example of a t-test by comparing two groups.
library(ggpval) library(tidyverse) set.seed(5) X1<-rnorm(100, 0, 1) X2<-rnorm(100, 2, 1) Groups<-rep(c("G1", "G2"), each = 100) Values<-c(X1,X2) df<-data.frame(Groups=Groups, Values=Values) plt <- ggplot(df, aes(Groups, Values)) + geom_boxplot() + geom_jitter() add_pval(plt, pairs = list(c(1, 2)))
As we can see, the P-Value was added at the top of the chart (P<2e-16).