【问题标题】:How to add wilcox_test results from rstatix to ggboxplot() using stat_pvalue_manual?如何使用 stat_pvalue_manual 将 rstatix 的 wilcox_test 结果添加到 ggboxplot()?
【发布时间】:2019-11-09 12:24:16
【问题描述】:

我正在尝试创建一个箱线图并手动将 p 值添加到箱线图之间的每个比较中:

    #I have the following boxplot
    ToothGrowth%>%ggplot(aes(x=dose, y=len, fill=supp))+
      geom_boxplot()

    #I would like to do a wilcoxon sum rank test on the data and label each comparison between OJ and VC
    stat.test <- ToothGrowth%>%group_by(dose)%>%wilcox_test(len~supp, p.adjust.method = 'BH')%>%
      mutate(y.position = c(29, 35, 39))
    stat.test
    #I try to add the p-values to my boxplots
    ToothGrowth%>%ggplot(aes(x=dose, y=len, fill=supp))+
      geom_boxplot()+
      stat_pvalue_manual(stat.test, label = 'p')

    #However I get the following error:
    # Error in FUN(X[[i]], ...) : object 'supp' not found

【问题讨论】:

标签: r ggplot2 boxplot ggpubr


【解决方案1】:

您需要在geom_boxplot() 中而不是在ggplot() 中指定fill 参数。

安装最新的开发版 rstatix 和 ggpubr,然后尝试以下 R 代码。

# Required packages
library(ggpubr)  # https://github.com/kassambara/ggpubr
library(rstatix)  # https://github.com/kassambara/rstatix

# Stat test
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
stat.test <- ToothGrowth %>% 
  group_by(dose) %>% 
  wilcox_test(len~supp, p.adjust.method = 'BH') %>%
  mutate(y.position = c(29, 35, 39))
stat.test

# Plot + pvalue
bxp <- ggplot(ToothGrowth, aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = supp))
bxp + stat_pvalue_manual(stat.test, x = "dose", label = 'p')

# Add automatically x and y positions
stat.test <- ToothGrowth %>% 
  group_by(dose) %>% 
  wilcox_test(len~supp, p.adjust.method = 'BH') %>%
  add_xy_position(x = "dose")
bxp + stat_pvalue_manual(stat.test, label = 'p', tip.length = 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    相关资源
    最近更新 更多