Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

testing - R ggplot2: boxplots with significance level (more than 2 groups: kruskal.test and wilcox.test pairwise) and multiple facets

Following up on this question, I am trying to make boxplots and pairwise comparisons to show levels of significance (only for the significant pairs) again, but this time I have more than 2 groups to compare and more complicated facets.

I am going to use the iris dataset here for illustration purposes. Check the MWE below where I add an additional "treatment" variable.

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

This produces the following plot:

plot

The idea would be to perform a Kruskal-Wallis test across the "variable" groups (Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), and pairwise Wilcoxon tests between them, PER FACET defined by "Species" and "treatment".

It would most likely involve updating the annotation like in my previous question.

In other words, I want to do the same as in this other question I posted, but PER FACET.

I am getting horribly confused and stuck, though the solution should be quite similar... Any help would be appreciated!! Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try

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 can be included by adding

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...