【问题标题】:Adding multiple p-values to ggplot using rstatix package使用 rstatix 包将多个 p 值添加到 ggplot
【发布时间】:2021-10-25 06:45:01
【问题描述】:

我正在尝试将多个 p 值添加到我的堆积条形图中。我可以使用以下代码将单个 p 值添加到我的数据中:

library(tidyverse)
library(ggprism)
library(rstatix)

RDT_Pos_genus_pval <- table(RDT_genus_ratio$Ag_RDT, RDT_genus_ratio$Genus_Cytb) %>%  ##filtered to one group for example purposes
                     fisher.test() %>%
                     .$p.value %>%
                      signif(3)
Pos_genus_pval <- data.frame(
  group1 = "Mastomys",
  group2 = "Praomys",
  label = RDT_Pos_genus_pval,
  y.position = 1.05)

RDT_genus_ratio_bar <- ggplot(RDT_genus_ratio) +
  geom_bar(mapping = aes(x = Genus_Cytb, fill = Ag_RDT), position = "fill") +
  labs(y = "Percentage", x = "Genus") +
  scale_y_continuous(labels = scales::percent, guide = "prism_offset_minor") +
  scale_x_discrete() +
  scale_fill_discrete(name = "Antigen Status", labels = c("FALSE" = "Ag -", "TRUE" = "Ag +")) +
  theme_classic() +
  theme(aspect.ratio = 2/1.5, plot.title = element_text(hjust = 0.5, size = 16),
        axis.text.y = element_text(size = 12, vjust = -0.2), axis.text.x = element_text(size = 10),
        axis.title = element_text(size = 12), axis.title.x.bottom = element_text(size = 12, vjust = 0.5)) +
  add_pvalue(Pos_genus_pval, label = "p = {label}")

但是,我想将成对的 Fisher 测试添加到所有三个组。我知道使用rstatix 包,我应该能够通过专门为此创建一个数据框,然后在add_pvalue 行中添加它来代替Pos_genus_pval。但是,我一直卡在add_y_position part(见下文)

asdf <- table(RDT_genus_ratio$Ag_RDT, RDT_genus_ratio$Genus_Cytb) %>%
  pairwise_fisher_test() 

# A tibble: 3 x 6
  group1   group2      n        p    p.adj p.adj.signif
* <chr>    <chr>   <int>    <dbl>    <dbl> <chr>       
1 Mastomys Praomys   316 4.14e- 1 4.14e- 1 ns          
2 Mastomys Rattus    491 7.44e-18 2.23e-17 ****        
3 Praomys  Rattus    209 1.29e- 2 2.58e- 2 *  

asdf <- table(RDT_genus_ratio$Ag_RDT, RDT_genus_ratio$Genus_Cytb) %>%
  pairwise_fisher_test() %>%
  add_y_position(formula = group1 ~ group2) ##this line is where I'm getting stuck 

Error in add_y_position(., formula = group1 ~ group2) : 
  data and formula arguments should be specified.       

我一直在尝试遵循 rstatix readmeggprism tutorial 中链接的示例,但我仍然遇到问题。如果有任何见解,我将不胜感激!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    改用ggpubr 中的stat_pvalue_manual。计算 pariwise Fisher 测试,然后为每个比较添加 y 位置。从那里您可以将生成的表格中的任何内容添加到图表中。

    RDT_Pos_by_genus_fisherstest <- table(RDT_genus_ratio$Ag_RDT, RDT_genus_ratio$Genus_Cytb) %>%
      pairwise_fisher_test() %>%
      mutate(y.position = c(1.03, 1.1, 1.03))
    RDT_Pos_by_genus_fisherstest
    # A tibble: 3 x 7
      group1   group2      n        p    p.adj p.adj.signif y.position
    * <chr>    <chr>   <int>    <dbl>    <dbl> <chr>             <dbl>
    1 Mastomys Praomys   316 4.14e- 1 4.14e- 1 ns                 1.03
    2 Mastomys Rattus    491 7.44e-18 2.23e-17 ****               1.1
    3 Praomys  Rattus    209 1.29e- 2 2.58e- 2 *                  1.03
    
    RDT_genus_ratio_bar <- ggplot(RDT_genus_ratio) +
      geom_bar(mapping = aes(x = Genus_Cytb, fill = Ag_RDT), position = "fill") +
      labs(y = "Percentage", x = "Genus") +
      scale_y_continuous(labels = scales::percent, limits = c(0,1.2,0.1), guide = "prism_offset_minor") +
      scale_x_discrete() +
      scale_fill_discrete(name = "Antigen Status", labels = c("FALSE" = "Ag -", "TRUE" = "Ag +")) +
      theme_classic() +
      theme(aspect.ratio = 2/1.5, plot.title = element_text(hjust = 0.5, size = 16),
            axis.text.y = element_text(size = 12, vjust = -0.2), axis.text.x = element_text(size = 10),
            axis.title = element_text(size = 12), axis.title.x.bottom = element_text(size = 12, vjust = 0.5)) +
      stat_pvalue_manual(RDT_Pos_by_genus_fisherstest,label = "p.adj.signif", size = 3.7, tip.length = 0.25, bracket.shorten = 0.08)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 2021-10-22
      • 2022-07-06
      • 2019-11-09
      相关资源
      最近更新 更多