gpt4 book ai didi

r - stat_compare_means 与多个组

转载 作者:行者123 更新时间:2023-12-04 10:30:04 26 4
gpt4 key购买 nike

我需要一些有关 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com