R:ggplot2-每个方面的Kruskal-Wallis测试 [英] R: ggplot2 - Kruskal-Wallis test per facet

查看:141
本文介绍了R:ggplot2-每个方面的Kruskal-Wallis测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个方面都有箱型图,我想在每个方面进行Kruskal-Wallis测试,并将结果放在每个方面的左上角.

为了说明这一点,我使用了虹膜数据集,并向其中添加了一个名为"treatment"的变量.

MWE:

  library(reshape2)库(ggplot2)数据(虹膜)iris $ treatment<-rep(c("A","B"),length(iris $ Species)/2)mydf<-融化(iris,measure.vars = names(iris)[1:4])mydf $治疗<-as.factor(mydf $治疗)mydf $ variable<-factor(mydf $ variable,level = sort(levels(mydf $ variable)))ggplot(mydf,aes(x =变量,y =值))+geom_boxplot(aes(fill = Species))+facet_grid(处理〜种类,scales ="free",space ="free_x")+geom_text(label = paste("Kruskal-Wallis,p =",with(mydf,kruskal.test(value〜variable)$ p.value))) 

以上是我的最佳尝试,它产生以下内容.

显然是错误的.

我希望跨度量(Petal.Length,Petal.Width,Sepal.Length,Sepal.Width)的Kruskal-Wallis测试的结果出现在每个方面的左上角.

每个数据子集应进行6次测试(根据处理方式和种类),因此我认为p.值应进行调整(最好由Benjamini-Hochberg进行).

如果可能的话,最好将每个结果p.value舍入到2个小数位.并且,如果可能的话,我宁愿避免使用ggpubr,因为我对此有问题,并坚持使用geom_text().谢谢!

解决方案

给出的解决方案

I have boxplots in multiple facets and I would like to perform a Kruskal-Wallis test on each facet, and place the result on top-left of each respective facet.

To exemplify this, I am using the iris dataset, to which I added an additional variable named "treatment".

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])
mydf$treatment <- as.factor(mydf$treatment)
mydf$variable <- factor(mydf$variable, levels=sort(levels(mydf$variable)))

ggplot(mydf,aes(x=variable, y=value)) +
    geom_boxplot(aes(fill=Species)) +
    facet_grid(treatment~Species, scales="free", space="free_x") +
    geom_text(label=paste("Kruskal-Wallis, p=", with(mydf, kruskal.test(value ~ variable)$p.value)))

The above is my best attempt, it produces the following.

It is obviously wrong.

I would like the result of a Kruskal-Wallis test across measures (Petal.Length, Petal.Width, Sepal.Length, Sepal.Width), to appear in top-left of each facet.

The test should be performed 6 times per each subset of data (according to treatment and Species), so I guess the p.value should be adjusted (by Benjamini-Hochberg preferably).

If possible, it would be great if each resulting p.value could be rounded to 2 decimal positions. And if possible, I'd rather avoid the use of ggpubr, cause I have problems with it, and stick to geom_text(). Thanks!

解决方案

The solution is given here.

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])
mydf$treatment <- as.factor(mydf$treatment)
mydf$variable <- factor(mydf$variable, levels=sort(levels(mydf$variable)))

library(dplyr)
pv <- mydf %>% group_by(treatment, Species) %>%
    summarize(p.value = kruskal.test(value ~ variable)$p.value)

ggplot(mydf,aes(x=variable, y=value)) +
    geom_boxplot(aes(fill=Species)) +
    facet_grid(treatment~Species, scales="free", space="free_x") +
    geom_text(data=pv, aes(x=2, y=7, label=paste0("Kruskal-Wallis\n p=",p.value)))

这篇关于R:ggplot2-每个方面的Kruskal-Wallis测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆