rggplot2ggpubrsignificance

Manually plotting significance relations between sub-groups on ggplot2 barplot


I've been trying to plot manually labelled significance bars for a subset of groups on a ggplot2 barplot using ggsignif or ggpubr without much luck. The data is something like the following MWE:

set.seed(3)
## create data
df <- data.frame(activity = rep(c("Flying", "Jumping"), 3),
                 mean = rep(rnorm(6, 50, 25)),
                 group = c(rep("Ecuador", 2),
                           rep("Peru", 2),
                           rep("Brazil", 2)))
## plot it
ggplot(df, aes(x = activity, y = mean, fill = group)) +
    geom_bar(position = position_dodge(0.9), stat = "identity",
             width = 0.9, colour = "black", size = 0.1) +
    xlab("Activity") + ylab("Mean")

Example plot Where I'd like to manually specify significance labels, say between Brazil/Ecuador" on "Flying", and Ecuador/Peru on "Jumping". Does anyone know how to properly deal with this kind of data, for example with ggsignif? And is there a way to refer to each bar by name, rather than try to work out its x-axis position?


Solution

  • If you know on which barchart you want to add your significance labels, you can do:

    library(ggsignif)
    library(ggplot2)
    
    ggplot(df, aes(x = activity, y = mean, fill = group)) +
      geom_bar(position = position_dodge(0.9), stat = "identity",
               width = 0.9, colour = "black", size = 0.1) +
      xlab("Activity") + ylab("Mean")+
      geom_signif(y_position = c(60,50), xmin = c(0.7,2), xmax = c(1,2.3),
                  annotation=c("**", "***"), tip_length=0)
    

    enter image description here

    Does it answer your question ?