gpt4 book ai didi

R ggplot2 : boxplots with significance level (more than 2 groups: kruskal. 测试和 wilcox.test 成对)和多个方面

转载 作者:行者123 更新时间:2023-11-28 21:08:25 27 4
gpt4 key购买 nike

跟进 this question ,我正在尝试制作箱线图和成对比较以再次显示显着性水平(仅针对重要的成对),但这次我有超过 2 个组要比较和更复杂的方面。

我将在此处使用 iris 数据集进行说明。检查下面的 MWE,我在其中添加了一个额外的“治疗”变量。

library(reshape2)
library(ggplot2)
data(iris)
iris$treatment <- rep(c("A","B"), length(iris$Species)/2)
mydf <- melt(iris, measure.vars=names(iris)[1:4])
ggplot(mydf, aes(x=variable, y=value, fill=Species)) + geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=5, size=4) +
facet_grid(treatment~Species, scales="free", space="free_x") +
theme(axis.text.x = element_text(angle=45, hjust=1))

这会产生以下情节:

plot

想法是对“可变”组(Sepal.Length、Sepal.Width、Petal.Length、Petal.Width)执行 Kruskal-Wallis 检验,并在它们之间进行成对的 Wilcoxon 检验,PER FACET 定义为“物种”和“治疗”。

这很可能涉及更新注释,就像我之前的问题一样。

换句话说,我想做与 this other question 中相同的事情我发布了,但是 PER FACET。

虽然解决方案应该非常相似,但我感到非常困惑和卡住……任何帮助将不胜感激!谢谢!!

最佳答案

你可以试试

library(ggsignif)
ggplot(mydf,aes(x=variable, y=value)) +
geom_boxplot(aes(fill=Species)) + # define the fill argument here
facet_grid(treatment~Species) +
ylim(0,15)+
theme(axis.text.x = element_text(angle=45, hjust=1)) +
geom_signif(test="wilcox.test", comparisons = combn(levels(mydf$variable),2, simplify = F)[-4],
step_increase = 0.2)

enter image description here

Kruskal.wallis 可以通过添加

library(ggpubr)
stat_compare_means(test="kruskal.test")

关于R ggplot2 : boxplots with significance level (more than 2 groups: kruskal. 测试和 wilcox.test 成对)和多个方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46446392/

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