r - 具有多个组的 stat_compare_means

标签 r ggplot2 ggpubr

我需要一些有关 stat_compare_means 和多个组的帮助。

这是我的数据。

> head(df_annot)
  Row.names Diversity_sh Diversity_si  Evenness    Chao1 Location Bean Fungi Insect
1      R-B1     1.314181    0.6040213 0.3053349 91.00000     Root Bean    M-     NI
2      R-B2     1.323718    0.6117602 0.3075507 77.43750     Root Bean    M-     NI
3      R-B3     1.249950    0.5737293 0.2877545 81.50000     Root Bean    M-     NI
4    R-BF-1     1.177111    0.5414276 0.2693958 92.33333     Root Bean    M+     NI
5    R-BF-2     1.191254    0.5252688 0.2742420 79.54545     Root Bean    M+     NI
6    R-BF-3     1.397233    0.6285945 0.3179540 85.50000     Root Bean    M+     NI

这是一个图表,我希望标记所有比较。

enter image description here

这是一些代码。我知道我的 my_comparisons 不正确,但我不知道从哪里开始这两个组。我想将 M+/Insect 与 M-/Insect 以及 M+/Insect 与 M+/NI 等进行比较,所有双向比较。任何建议都会很棒。谢谢

my_comparisons<- list( c("M+", "M-"), c("Insect", "NI"))
  ggplot(df_annot,aes_string(x="Insect",y=index,fill="Fungi"))+
    geom_boxplot(alpha=0.8)+
    geom_point(aes(fill=Fungi),size = 3, shape = 21,position = position_jitterdodge(jitter.width = 0.02,jitter.height = 0))+
    stat_compare_means(comparison=my_comparisons,label="p.format",method="wilcox.test")+
    #ggtitle(df_name)+
    ylab(paste(index))+
    xlab("")+
    #    scale_x_discrete(labels= c("M+","M-","soil alone"))+
    theme(plot.title = element_text(size = 18, face = "bold"))+
    theme(axis.text=element_text(size=14),
          axis.title=element_text(size=14)) + 
    theme(legend.text=element_text(size=14),
          legend.title=element_text(size=14)) +
    theme(strip.text.x = element_text(size = 14))


dput(df_annot)
structure(list(Row.names = structure(c("R-B1", "R-B2", "R-B3", 
"R-BF-1", "R-BF-2", "R-BF-3", "R-BFi-1", "R-BFi-2", "R-Bi-1", 
"R-Bi-2", "R-Bi-3"), class = "AsIs"), Diversity_sh = c(1.31418133185869, 
1.32371839350534, 1.24994951615418, 1.17711111336449, 1.19125374868316, 
1.39723272927515, 1.34145146126423, 1.21674449259962, 1.20721660188555, 
1.17245529262564, 1.20912937911657), Diversity_si = c(0.604021268328531, 
0.611760247980402, 0.573729285531772, 0.541427625516077, 0.525268755766239, 
0.628594506768001, 0.597250229879166, 0.554646956896473, 0.548992316400345, 
0.531291238688503, 0.583806537719818), Evenness = c(0.305334910927276, 
0.307550737463383, 0.287754490536268, 0.269395848882803, 0.274241968272787, 
0.317954009728278, 0.305260435164649, 0.276882141486585, 0.273949061455415, 
0.269914321375221, 0.275929262855007), Chao1 = c(91, 77.4375, 
81.5, 92.3333333333333, 79.5454545454545, 85.5, 87.5, 90.5454545454545, 
89.3333333333333, 88.6666666666667, 88.0769230769231), Location = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Root", "Rhizospheric Soil"
), class = "factor"), Bean = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "Bean", class = "factor"), 
    Fungi = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L), .Label = c("M+", "M-"), class = "factor"), Insect = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect", 
    "NI"), class = "factor")), row.names = c(NA, -11L), class = "data.frame")

最佳答案

facet_wrap()正如所讨论的那样,可能会帮助您here

ggplot(df_annot, aes(x=df_annot$Insect, y= df_annot$Evenness)) +
  facet_wrap(~df_annot$Fungi)+
  geom_boxplot(alpha=0.8)  +
  geom_point()+
  stat_compare_means(comparisons = list(c("Insect", "NI") ), label="p.format",method="wilcox.test")

编辑

好吧,这是一个 - 不太优雅 - 没有分面的解决方案。

创建一个包含昆虫信息和真菌状态的新变量:

df_annot$var <- paste(df_annot$Insect,df_annot$Fungi, sep = "_" )

然后建立对比

my_comparisons <- rev(list(c("Insect_M-","Insect_M+"),c("NI_M-","Insect_M-"),c("NI_M+","Insect_M-"),
                       c("Insect_M+", "NI_M-"), c("Insect_M+", "NI_M+"), c("NI_M-","NI_M+")))

并绘制图表

ggplot(df_annot,aes_string(x="var",y="Evenness",fill="Fungi"))+
  geom_boxplot(alpha=0.8)+
  geom_point(aes(fill=Fungi),size = 3, shape = 21,position = position_jitterdodge(jitter.width = 0.02,jitter.height = 0))+
  stat_compare_means(comparison=my_comparisons,label="p.format",method="wilcox.test")+
  #ggtitle(df_name)+
  ylab(paste("Evenness"))+
  xlab("")+
  #    scale_x_discrete(labels= c("M+","M-","soil alone"))+
  theme(plot.title = element_text(size = 18, face = "bold"))+
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14)) + 
  theme(legend.text=element_text(size=14),
        legend.title=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14))

enter image description here

你可能想要起更好的名字等等。但这可能就是您正在寻找的。

关于r - 具有多个组的 stat_compare_means,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60457722/

相关文章:

r - dplyr::copy_to 和 sparklyr::sdf_copy_to 有什么区别?

r - 保存 ggplotGrob 生成的 ggplot2 时间序列图 grob

r - 如何重新调整 logit 输出值

r - 如何在 ggplot geom_line 的 x 轴上创建具有多个组的分类变量

r - 面标签字体大小

r - 如何将 y 轴更改为对数刻度?

r - 在 R 中组合 map 和 XY ggplot 图表

r - ggpubr:更改 stat_compare_means Kruskal-Wallis p 值的字体大小

r - 将 fiddle 图与森林图对齐

r - 在 R 中使用 plyr 插值数据