Search code examples
rannotationsboxplotggpubr

How to Annotate a boxplot with p values using a combination of ggpubr and ggsignif?


I am trying to create a boxplot that shows multiple comparisons. I made a toy data set that seems to give me the same error as I am getting in my larger one.

library(tidyverse)
library(ggsignif)
library(ggpubr)


dat <- data.frame(measurement = c("750","850","900", "300","200","400", "20", "30", "50"),
                   diagnosis = c("Healthy", "Healthy", "Healthy","Moderate","Moderate","Moderate",  "Sick", "Sick", "Sick"))

dat$measurement <- as.numeric(dat$measurement)

#List of comparisons
dat.compare <- list(c("Healthy", "Moderate"), 
                    c("Healthy", "Sick"), 
                    c("Moderate", "Sick"))

#Running Anova
dat.lm <- lm(measurement ~ diagnosis, data = dat)
TukeyHSD(aov(dat.lm))
Yields: 
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = dat.lm)

$diagnosis
                       diff       lwr        upr     p adj
Moderate-Healthy -4.3333333 -8.830369  0.1637022 0.0574078
Sick-Healthy     -4.6666667 -9.163702 -0.1696312 0.0433911
Sick-Moderate    -0.3333333 -4.830369  4.1637022 0.9720206

dat.p <- list("0.05","0.04", "0.97")

The p adj is what I am trying to annotate to my boxplot with the following code:

ggboxplot(dat, x ="diagnosis" , y = "measurement" ,
               color = "diagnosis", palette = "jco",
               add = "jitter") +
  ggsignif::geom_signif(data=dat, 
                        comparisons = dat.compare, annotations=dat.p, 
                        map_signif_level = TRUE)

When run the code for the boxplot, it gives me the following error:

Warning message:
Computation failed in `stat_signif()`:
names do not match previous names 

The end result should look something like this

Boxplot

As far as I know, the names on the comparison list match the ones in the dataframe. I've been stuck on this for hours, any idea what I'm doing wrong? Thank you!


Solution

  • I discovered that if I change both list formats into vectors, the package will take it without an error.

    I appreciate the help @dc37.