首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用stat_pvalue_manual将rstatix的wilcox_test结果添加到ggboxplot()中?

如何使用stat_pvalue_manual将rstatix的wilcox_test结果添加到ggboxplot()中?
EN

Stack Overflow用户
提问于 2019-06-27 21:06:39
回答 1查看 1.5K关注 0票数 1

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

代码语言:javascript
运行
复制
    #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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 04:51:19

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

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

代码语言:javascript
运行
复制
# 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')

代码语言:javascript
运行
复制
# 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)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56791735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档