Как добавить результаты wilcox_test из rstatix ​​в ggboxplot (), используя stat_pvalue_manual? - PullRequest
0 голосов
/ 27 июня 2019

Я пытаюсь создать блок-график и вручную добавить 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

1 Ответ

0 голосов
/ 01 июля 2019

Вам необходимо указать аргумент fill в geom_boxplot(), а не в ggplot().

Установите последнюю версию dev для 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')

enter image description here

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

enter image description here

...