3

I am trying to add significance asterisks to my ggplot boxplot, using groups (fill) and facets.

Using geom_signif() I can add bars such as:

Image 1

I am trying to do the same for the dodged boxplots too.. similar to

Image 2

(Imagine there were significance values above the smaller lines...)

The code for the former graph:

data:

library(ggplot2)
library(ggsignif)
df <- data.frame(iris,petal.colour=c("red","blue"), country=c("UK","France","France"))

First plot:

     ggplot(df, aes(country,Sepal.Length))+
geom_boxplot(position="dodge",aes(fill=petal.colour))+
      facet_wrap(~Species, ncol=3)+
      geom_signif(comparisons = list(c("France", "UK")), map_signif_level=TRUE,
                  tip_length=0,y_position = 9, textsize = 4)

and for the smaller bars

+geom_signif(annotations = c("", ""),
              y_position = 8.5, 
            xmin=c(0.75,1.75), xmax=c(1.25,2.25),tip_length=0)

It would great to let R do the work, but if its easier to manually add text above these smaller lines then that's fine with me.

wibeasley
  • 5,000
  • 3
  • 34
  • 62
George
  • 71
  • 1
  • 2
  • 4

2 Answers2

0

I can't figure out how to get them to work for that group using geom_signif. See the first part for my attempt. I was able to get it to work using ggpubr and stat_compare_means, which I believe is an extension of geom_signif.

ggplot(df, aes(country,Sepal.Length)) +
  geom_boxplot(position="dodge",aes(fill=petal.colour)) +
  facet_wrap(~Species, ncol=3) +
  geom_signif(comparisons = list(c("France", "UK")), map_signif_level=TRUE,
              tip_length=0,y_position = 9, textsize = 4) + 
  geom_signif(y_position = 8.5, 
              xmin=c(0.75,1.75), xmax=c(1.25,2.25), tip_length=0, map_signif_level = c("***" = 0.001, "**" = 0.01, "*" = 0.05)) 

Warning messages:
1: In wilcox.test.default(c(4.9, 4.7, 5, 5.4, 5, 4.4, 5.4, 4.8, 4.3,  :
  cannot compute exact p-value with ties
2: In wilcox.test.default(c(7, 6.9, 5.5, 5.7, 6.3, 6.6, 5.2, 5.9, 6,  :
  cannot compute exact p-value with ties
3: In wilcox.test.default(c(6.3, 5.8, 6.3, 6.5, 4.9, 7.3, 7.2, 6.5,  :
  cannot compute exact p-value with ties
4: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0 
5: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0 
6: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0 

Using ggpubr and stat_compare_means. Note you can use different labels, and tests, etc. See ?stat_compare_means.

library(ggpubr)
ggplot(df, aes(country,Sepal.Length)) +
  geom_boxplot(position="dodge",aes(fill=petal.colour)) +
  facet_wrap(~Species, ncol=3) +
  stat_compare_means(aes(group = country), label = "p.signif", label.y = 10, label.x = 1.5) +
  stat_compare_means(aes(group = petal.colour), label = "p.format", label.y = 8.5)

enter image description here

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29
  • Some related links. [Plot grouped data](http://www.sthda.com/english/articles/32-r-graphics-essentials/132-plot-grouped-data-box-plot-bar-plot-and-more/) [Another SO question](https://stackoverflow.com/questions/17084566/put-stars-on-ggplot-barplots-and-boxplots-to-indicate-the-level-of-significanc) – Anonymous coward Oct 24 '18 at 14:41
  • This does not work if we want to compare blue boxes with the other ones (i.e. having a reference group or defining comparisons). Do you have any solution for that? Your method worked for me only when I compared each group with the base. – Ben Dec 19 '19 at 09:57
  • Can you clarify what kind of test you are looking at running? You want to specifically test petal.color blue (I'm noticing now that the text doesn't match the fill color) against a hypothetical third reference group? It may be worth opening a new question. – Anonymous coward Dec 20 '19 at 18:27
-3

Maybe you can save the plot as .pdf file and try to use Adobe Illustrator to manually add whatever you want into the plot, the greatest advantage of R plot is its perfect compatibility with Adobe Illustrator.

Or maybe you can try to set

map_signif_level = c("***"=0.001, "**"=0.01, "*"=0.05)

in geom_signif

Hope that helps

passiflora
  • 332
  • 1
  • 9