2017-09-27 229 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