Search code examples
rggplot2plotboxplot

How to annotate significance for specific pairs in a grouped boxplot?


I am trying to annotate the significance of various pairs of boxplots in my plot. I am using ggsignif since it seems the easiest way to do it. This is what my full boxplot looks like at present:

enter image description here

In this case, I have added a significance bar between the groups of Ct2 and D9. However, since the bars are automatically placed between each pair of boxplots, this implies that the significance for both regime comparisons is significant. This is not the case for my data, and I want to show that only the comparison between the two blue boxplots (Nfix regime) is significant between the treatments of Ct2 and D9. Is it possible to over-ride the placement of the bar somehow and have it be shifted to the right a little, or is there a neater way to do it within ggsignif? I would like to keep all of my boxplots together, even though I am aware it would be simpler to place the significance bars if I were to split them into two by regime.

This is the code for the boxplot, with the significance bar added at the end:

library(ggplot2)
library(ggsignif)

# Import Data
Structured_Data <- read_delim("RoundedCleanedData.csv", delim = ";", escape_double = FALSE, trim_ws = TRUE)

# Flavonoids boxplot

flavDWBox <- ggplot(data = Structured_Data, aes(x = Treatment, y = Structured_Data$`Flav ug/mg dry`, fill = Nregime)) + geom_boxplot(position = "dodge2") + 
  labs(y = "Flavonoid μg/mg dry weight", title="Flavonoid Concentrations per Treatment") +
  theme_bw() +
  theme(title = element_text(size = 10, face = 'bold'), plot.title = element_text(hjust = 0.5))
flavDWBox

# Adding significance to boxplot

flavDWBox + 
  geom_signif(comparisons = list(c("Ct2", "D9")), map_signif_level = TRUE)

The data I am working with can be downloaded (small 4kb download) from: https://wetransfer.com/downloads/f05d2461d1e131b0a5ae85d0f058311520230610165614/c8dce1

I have also tried using position_dodge and other possibly related pieces of the geom_signif function, but to no avail.

Thank you for the help.


Solution

  • I am not sure if I understand correctly, but if you want to add significance level by hand, we could do it this way:

    library(ggplot2)
    library(ggsignif)
    
    
    # Flavonoids boxplot
    
    Structured_Data %>% 
      select(Treatment, `Flav ug/mg dry`, Nregime) %>% 
      ggplot(aes(x = Treatment, y = Structured_Data$`Flav ug/mg dry`, fill = Nregime)) + geom_boxplot(position = "dodge2") + 
      labs(y = "Flavonoid μg/mg dry weight", title="Flavonoid Concentrations per Treatment") +
      theme_bw() +
      theme(title = element_text(size = 10, face = 'bold'), plot.title = element_text(hjust = 0.5))+
      geom_signif(annotation = "*",
                  y_position = 20, xmin = 1.19, xmax = 3.19, 
                  tip_length = c(0.02, 0.02)
                  )
    
    

    enter image description here