How to annotate different values for each facet with dodged geom_boxplot on R?

拟墨画扇 提交于 2019-12-12 09:49:33

问题


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:

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

(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.


回答1:


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




回答2:


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)



来源:https://stackoverflow.com/questions/52958453/how-to-annotate-different-values-for-each-facet-with-dodged-geom-boxplot-on-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!