Search code examples
rggplot2plotvisualizationgeom-text

How can I add significance letters to a plot in ggplot2, from a pairwise permutation test?


OK, so I have a plot called "meanplot", where its' x-axis is a factor category called "Group" - "Open", "South", "North. y-axis has numeric values, and called "ms".

I've added geom_jitter() for the jitter, and also geom_pointrange, which was mapped to a different df that has means and YEs, and it shows 3 points - one for each factor on x-axis.

Now I'd like to add the significance letters on top, taken from a pairwise permutation test.

The test code goes as follows:

pwpmtest <- pairwisePermutationTest(ms ~ Plot.Type,
                             data = arranged_data,
                             method="fdr")

and then I made a list, that goes:

siglist <- cldList(p.adjust ~ Comparison,
        data = pwpmtest,
        threshold  = 0.05)

which gives out this output:

> siglist
  Group Letter MonoLetter
1 North      a        a  
2  Open      b         b 
3 South      c          c

Then I tried making a plot:

letterplot <- meanplot + geom_text(data = siglist,
                                   aes(label=siglist$MonoLetter, x=Group, y=16))

And the plot I get is the same plot as before, no text. How can I fix this?

Also, can I change the order of letters in the 'siglist'? I'd love to be able to make "Open" "a", "South" "b", and "North" "c".

TIA!


Solution

  • Knowing exactly what you are looking for is difficult without a reproducible example. Here's something that may help, I re-created your data and made up some to fill in where you omitted information.

    siglist <- tibble(Group=c("North","Open","South"),
                      Letter=c('a','b','c'),
                      MonoLetter=c('a','b','c'))
    
    toydat <- tibble(x=c("North","Open","South"),
                     y=c(4,7,3))
    
    ggplot() +
      geom_point(data = toydat,
                 aes(x=x,y=y)) +
      geom_text(data = siglist,
                aes(x=Group,y=10,label=Letter))