R ggplot2:具有显着性水平的箱图(超过2组:kruskal.test和wilcox.test成对)和多个方面

时间:2017-09-27 11:09:43

标签: r testing ggplot2 boxplot

继续this question,我试图制作箱形图和成对比较以再次显示重要性水平(仅针对重要对),但这次我有两组以上的比较和更复杂的方面

我将在此处使用虹膜数据集进行说明。检查下面的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

这个想法是在&#34;变量&#34;上执行Kruskal-Wallis测试。组(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width)和它们之间的成对Wilcoxon测试,PER FACET由&#34; Species&#34;和&#34;治疗&#34;。

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

换句话说,我想和我发布的this other question做同样的事情,但我要做到这一点。

我感到非常困惑和困惑,虽然解决方案应该非常相似......任何帮助都将受到赞赏!谢谢!

1 个答案:

答案 0 :(得分:2)

你可以尝试

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")