将P值添加到组箱图中的比较中 [英] Add P values to comparisons within groups boxplot

查看:217
本文介绍了将P值添加到组箱图中的比较中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个箱形图,该箱形图仅在箱形图中每个条形的组内显示有效的p值.例如,这里将比较I1和SI2的公平",良好",非常好"等

I'm trying to create a boxplot which shows only the significant p values, within the groups for each bar in a box plot. For example here it would compare I1 and SI2 for the "fair", "good", "very good" etc

我尝试使用以下代码来实现上述情节

I've tried using the following code to achieve the above plot

library(ggplot2)
library(dplyr)
data("diamonds")

labeldat <- diamonds %>%
  group_by(cut, clarity) %>%
  dplyr::summarise(labels = paste(n(), n_distinct(color), sep = "\n"))


Comparisons = list(c("I1","SI2"),c("I1","SI1"),c("I1","VS2"),c("I1","VS1"),c("I1","VVS2"),c("I1","VVS1"),c("I1","IF"),
                   c("SI2","SI1"),c("SI2","VS2"),c("SI2","VS1"),c("SI2","VVS2"),c("SI2","VVS1"),c("SI2","IF"),
                   c("SI1","VS2"),c("SI1","VS1"),c("SI1","VVS2"),c("SI1","VVS1"),c("SI1","IF"),
                   c("VS2","VS1"),c("VS2","VVS2"),c("VS2","VVS1"),c("VS2","IF"),
                   c("VS1","VVS2"),c("VS1","VVS1"),c("VS1","IF"),
                   c("VVS2","VVS1"),c("VVS2","IF"),
                   c("VVS1","IF"))



ggplot(diamonds, aes(x=cut, y=price)) +
  geom_boxplot(aes(fill=clarity), position = position_dodge2(width=0.75)) + 
  theme_bw() + 
  geom_text(data = labeldat, aes(x = cut, y = -250, label = labels), hjust = 0.5, position = position_dodge2(width = .75))+
  stat_compare_means(aes(group=clarity), label = "p.signif", method="t.test", comparisons = Comparisons)

不幸的是,使用compares参数似乎是由于计算错误,我无法解决该问题:警告信息: stat_signif()中的计算失败:缺少需要TRUE/FALSE的值

Unfortunately using the comparisons argument seems through a computation error which I can't work out how to solve: Warning message: Computation failed in stat_signif(): missing value where TRUE/FALSE needed

我尝试在不进行比较的情况下运行它,但这似乎只是给了我总体得分

I have tried running this without the comparisons, but it seems to just give me an overall score

推荐答案

您可以为此使用ggsignif.它允许进行手动注释,因此您可以单独计算p值,并使用过滤后的比较创建注释data.frame.示例:

You could use ggsignif for that. It allows for manual annotation, so you could calculate p-values separately, and create an annotation data.frame with filtered comparisons. Example:

library(ggplot2)
library(ggsignif)
library(dplyr)
library(data.table)

dm <- split(diamonds, diamonds$cut)
getp <- function(y, pval=.05){
    a <- stats::pairwise.wilcox.test(x=y$price, g=y$clarity,
        p.adjust.method="none", paired=FALSE)
    return(as.data.table(as.table(a$p.value))[!is.na(N) & N < pval])
}
dmp <- data.table::rbindlist(lapply(dm, getp), idcol = "cut")
data.table::setnames(dmp, c("cut", "start", "end", "label"))
dmp$label <- formatC(
    signif(dmp$label, digits = 3),
    digits = 3,
    format = "g",
    flag = "#"
)
dmp[, y := (0:(.N-1)) * (2E4/.N)+2e4, by=cut]
data.table::setDF(dmp)

ggplot(diamonds, aes(x=clarity, y=price)) +
    geom_boxplot(aes(fill=clarity), position = position_dodge2(width=0.75)) + 
    facet_wrap(~ cut)+
    ggsignif::geom_signif(data=dmp,
        aes(xmin=start, xmax=end, annotations=label, y_position=y),
        textsize = 2, vjust = -0.2,
        manual=TRUE) + 
    ylim(NA, 4E4) +
    theme_bw() +
    theme(axis.text.x = element_blank())

这篇关于将P值添加到组箱图中的比较中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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