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

Viewed 762

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!

2 Answers

A possible solution is to use geom_signif to add manually your values on your boxplot.

But you need first to generate a dataframe that will contain the values for p, both x values being compared, and the y position to set your p values.

Here, an example of how you can do it by starting with your Tukey test:

Tukey_data <- TukeyHSD(aov(dat.lm))$diagnosis

library(dplyr)
maxvalues <- dat %>% group_by(diagnosis) %>% summarise(MAX = max(measurement))

pval <- as.data.frame(Tukey_data) %>% rownames_to_column("Group") %>%
  rowwise() %>%
  mutate(Start = unlist(strsplit(Group,"-"))[1],
         End = unlist(strsplit(Group,"-"))[2]) %>%
  left_join(.,maxvalues, by = c("Start" = "diagnosis")) %>%
  left_join(.,maxvalues, by = c("End" = "diagnosis")) %>% ungroup() %>%
  mutate(ypos = c(12,10,8))
  mutate(End = factor(End, levels = c("Healthy","Moderate","Sick"))) %>% rowwise() %>%
  mutate(ypos = max(MAX.x, MAX.y)*(1+0.25*as.numeric(End)))

# A tibble: 3 x 10
  Group              diff   lwr    upr `p adj` Start    End      MAX.x MAX.y  ypos
  <chr>             <dbl> <dbl>  <dbl>   <dbl> <chr>    <chr>    <dbl> <dbl> <dbl>
1 Moderate-Healthy -4.33  -8.83  0.164  0.0574 Moderate Healthy      5     9    12
2 Sick-Healthy     -4.67  -9.16 -0.170  0.0434 Sick     Healthy      6     9    10
3 Sick-Moderate    -0.333 -4.83  4.16   0.972  Sick     Moderate     6     5     8

Then, you can pass it geom_signif as follow:

library(ggpubr)
library(ggsignif)

ggboxplot(dat, x ="diagnosis" , y = "measurement" ,
          color = "diagnosis", palette = "jco",
          add = "jitter") +
  geom_signif(data = pval, manual = TRUE,
              aes(xmax = End, xmin = Start, y_position= ypos, annotations = round(`p adj`,3)))

enter image description here

Does it answer your question ?

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

I appreciate the help @dc37.

Related