2017-09-27 28 views
0

跟进this question,我试图制作箱型图和配对比较以显示显着性水平(仅限于重要的配对),但这次我有两个以上的小组进行比较和更复杂的方面。R ggplot2:具有显着性水平(超过2个组:kruskal.test和wilcox.test成对)和多个方面的箱形图

我打算在这里使用虹膜数据集来进行说明。检查下面添加额外“治疗”变量的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,花瓣进行Kruskal-Wallis检验。长度,Petal.Width)和它们之间的成对Wilcoxon检验,由“物种”和“处理”定义的PER FACET。

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

换句话说,我想要做和this other question一样的贴,但是PER FACET。

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

回答

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

嗨Jimobu被收录,非常感谢您的时间。我正在接触类似的东西,但是我对它有一些担忧......首先,我想知道为什么我没有用'geom_signif()'得到与pv < - tidy相同的p值(with( mydf,pairwise.wilcox.test(value,interaction(treatment,Species,variable),p.adjust.method =“BH”)))' – DaniCee

+0

另一件事是将p值以*和* *没有显示那些不重要的比较。完全一样,我们在https://stackoverflow.com/questions/45476950/r-ggplot2-boxplots-ggpubr-stat-compare-means-not-working-正确,但每个方面...我实际上需要相同的p - 值,导致我们在上一个问题中做出的图是来自相同数据的子集 – DaniCee

+0

p值:您必须比较非经过修改的p值。因此使用'p.adjust.method =“none”'。使用此[解决方案](https://stackoverflow.com/questions/45552715/r-ggplot2-perform-pairwise-tests-per-pair-in-a-facet-and-show-the-p-values-wit)为另一个问题。 – Jimbou