Predictive Hacks

How to add the p-value to the ggplot charts

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).

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s