• HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE


    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(dose) %>%
      t_test(len ~ supp) %>%
      adjust_pvalue(method = "bonferroni") %>%
      add_significance("p.adj")
    stat.test
    # Create a box plot
    bxp <- ggboxplot(
      df, x = "dose", y = "len", 
      color = "supp", palette = c("#00AFBB", "#E7B800")
    )
    
    # Add p-values onto the box plots
    stat.test <- stat.test %>%
      add_xy_position(x = "dose", dodge = 0.8)
    bxp + stat_pvalue_manual(
      stat.test,  label = "p", tip.length = 0
    )
    
    # Add 10% spaces between the p-value labels and the plot border
    bxp + stat_pvalue_manual(
      stat.test,  label = "p", tip.length = 0
    ) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(dose) %>%
      t_test(len ~ supp) %>%
      adjust_pvalue(method = "bonferroni") %>%
      add_significance("p.adj")
    stat.test
    # Create a box plot
    bxp <- ggboxplot(
      df, x = "dose", y = "len", 
      color = "supp", palette = c("#00AFBB", "#E7B800")
    )
    
    # Add p-values onto the box plots
    stat.test <- stat.test %>%
      add_xy_position(x = "dose", dodge = 0.8)
    # Use adjusted p-values as labels
    # Remove brackets
    bxp + stat_pvalue_manual(
      stat.test,  label = "p.adj", tip.length = 0,
      remove.bracket = TRUE
    )
    
    # Show adjusted p-values and significance levels
    # Hide ns (non-significant)
    bxp + stat_pvalue_manual(
      stat.test,  label = "{p.adj}{p.adj.signif}", 
      tip.length = 0, hide.ns = TRUE
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Additional statistical test
    stat.test2 <- df %>%
      t_test(len ~ dose, p.adjust.method = "bonferroni")
    stat.test2
    # Add p-values of `stat.test` and `stat.test2`
    # 1. Add stat.test
    stat.test <- stat.test %>%
      add_xy_position(x = "dose", dodge = 0.8)
    bxp.complex <- bxp + stat_pvalue_manual(
      stat.test,  label = "p", tip.length = 0
    )
    # 2. Add stat.test2
    # Add more space between brackets using `step.increase` 
    stat.test2 <- stat.test2 %>% add_xy_position(x = "dose")
    bxp.complex <- bxp.complex + 
      stat_pvalue_manual(
        stat.test2,  label = "p", tip.length = 0.02,
        step.increase = 0.05
      ) +
      scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))
    
    # 3. Display the plot
    bxp.complex 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Create a bar plot with error bars (mean +/- sd)
    bp <- ggbarplot(
      df, x = "dose", y = "len", add = "mean_sd", 
      color= "supp", palette = c("#00AFBB", "#E7B800"),
      position = position_dodge(0.8)
    )
    # Add p-values onto the bar plots
    stat.test <- stat.test %>%
      add_xy_position(fun = "mean_sd", x = "dose", dodge = 0.8) 
    bp + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", tip.length = 0.01
    )
    
    # Move down the brackets using `bracket.nudge.y`
    bp + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", tip.length = 0,
      bracket.nudge.y = -2
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Create a bar plot with error bars (mean +/- sd)
    bp2 <- ggbarplot(
      df, x = "dose", y = "len", add = "mean_sd", 
      color = "supp", palette = c("#00AFBB", "#E7B800"),
      position = position_stack()
    )
    
    # Add p-values onto the bar plots
    # Specify the p-value y position manually
    bp2 + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", tip.length = 0.01,
      x = "dose", y.position = c(30, 45, 60)
    )
    
    # Auto-compute the p-value y position
    # Adjust vertically label positions using vjust
    stat.test <- stat.test %>%
      add_xy_position(fun = "mean_sd", x = "dose", stack = TRUE) 
    bp2 + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", 
      remove.bracket = TRUE, vjust = -0.2
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Create a line plot with error bars (mean +/- sd)
    lp <- ggline(
      df, x = "dose", y = "len", add = "mean_sd", 
      color = "supp", palette = c("#00AFBB", "#E7B800")
    )
    
    # Add p-values onto the line plots
    # Remove brackets using linetype = "blank"
    stat.test <- stat.test %>%
      add_xy_position(fun = "mean_sd", x = "dose") 
    lp + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", 
      tip.length = 0, linetype  = "blank"
    )
    
    # Move down the significance levels using vjust
    lp + stat_pvalue_manual(
      stat.test,  label = "p.adj.signif", 
      linetype  = "blank", vjust = 2
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    pwc <- df %>%
      group_by(supp) %>%
      t_test(len ~ dose, p.adjust.method = "bonferroni")
    pwc
    # Box plot
    pwc <- pwc %>% add_xy_position(x = "dose")
    bxp +
      stat_pvalue_manual(
        pwc, color = "supp", step.group.by = "supp",
        tip.length = 0, step.increase = 0.1
      )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Box plot
    pwc <- pwc %>% add_xy_position(x = "dose")
    bxp +
      stat_pvalue_manual(
        pwc, color = "supp", step.group.by = "supp",
        tip.length = 0, step.increase = 0.1
      )
    # Bar plots
    pwc <- pwc %>% add_xy_position(x = "dose", fun = "mean_sd", dodge = 0.8)
    bp + stat_pvalue_manual(
      pwc, color = "supp", step.group.by = "supp",
      tip.length = 0, step.increase = 0.1
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Line plots
    pwc <- pwc %>% add_xy_position(x = "dose", fun = "mean_sd")
    lp + stat_pvalue_manual(
      pwc, color = "supp", step.group.by = "supp",
      tip.length = 0, step.increase = 0.1
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Bar plots (dodged)
    # Take a subset of the pairwise comparisons
    pwc.filtered <- pwc %>% 
      add_xy_position(x = "dose", fun = "mean_sd", dodge = 0.8) %>%
      filter(supp == "VC")
    bp +
      stat_pvalue_manual(
        pwc.filtered, color = "supp", step.group.by = "supp",
        tip.length = 0, step.increase = 0
      )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Bar plots (stacked)
    pwc.filtered <- pwc %>% 
      add_xy_position(x = "dose", fun = "mean_sd", stack = TRUE) %>%
      filter(supp == "VC")
    bp2 +
      stat_pvalue_manual(
        pwc.filtered, color = "supp", step.group.by = "supp",
        tip.length = 0, step.increase = 0.1
      )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Box plots
    bxp <- ggboxplot(
      df, x = "supp", y = "len", fill = "dose",
      palette = "npg"
    )
    bxp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    
    # Bar plots
    bp <- ggbarplot(
      df, x = "supp", y = "len", fill = "dose",
      palette = "npg", add = "mean_sd",
      position = position_dodge(0.8)
    )
    bp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(supp) %>%
      t_test(len ~ dose)
    stat.test 
    # Box plots with p-values
    stat.test <- stat.test %>%
      add_xy_position(x = "supp", dodge = 0.8)
    bxp + 
      stat_pvalue_manual(
        stat.test, label = "p.adj", tip.length = 0.01,
        bracket.nudge.y = -2
      ) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(supp) %>%
      t_test(len ~ dose)
    stat.test 
    # Bar plots with p-values
    stat.test <- stat.test %>%
      add_xy_position(x = "supp", fun = "mean_sd", dodge = 0.8)
    bp + 
      stat_pvalue_manual(
        stat.test, label = "p.adj", tip.length = 0.01,
        bracket.nudge.y = -2
      ) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(supp) %>%
      t_test(len ~ dose, ref.group = "0.5") 
    stat.test
    # Box plots with p-values
    stat.test <- stat.test %>%
      add_xy_position(x = "supp", dodge = 0.8)
    bxp + 
      stat_pvalue_manual(
        stat.test, label = "p.adj", tip.length = 0.01,
        bracket.nudge.y = -2
      ) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    stat.test <- df %>%
      group_by(supp) %>%
      t_test(len ~ dose, ref.group = "0.5") 
    stat.test
    # Box plots with p-values
    stat.test <- stat.test %>%
      add_xy_position(x = "supp", dodge = 0.8)
    # Show only significance levels
    # Move down significance symbols using vjust
    bxp + stat_pvalue_manual(
      stat.test, x = "supp",  label = "p.adj.signif", 
      tip.length = 0.01, vjust = 2
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    library(ggpubr)
    library(rstatix)
    # Transform `dose` into factor variable
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df, 3)
    # Bar plots with p-values
    stat.test <- stat.test %>%
      add_xy_position(x = "supp", fun = "mean_sd", dodge = 0.8)
    bp + 
      stat_pvalue_manual(
        stat.test, label = "p.adj", tip.length = 0.01,
        bracket.nudge.y = -2
      ) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    原文链接

  • 相关阅读:
    springboot福佳生活超市进销存管理系统毕业设计源码261620
    双编码器构建机器人零力拖动/导纳控制思路
    9.FPN网络代码实现
    注意力机制 - 注意力汇聚:Nadaraya-Watson核回归
    【lwip】08-ARP协议一图笔记及源码实现
    面向OLAP的列式存储DBMS-13-[ClickHouse]的MergeTree表引擎原理解析
    目标跟踪(1)SORT Windows实战+代码解析
    为什么MySQL 8.0删除了查询缓存
    面渣逆袭:二十二图、八千字、二十问,彻底搞定MyBatis!
    SSM+Mysql实现的共享单车管理系统(功能包含分角色,登录、用户管理、服务点管理、单车管理、分类管理、学生信息管理、单车租赁、信息统计、系统设置等)
  • 原文地址:https://blog.csdn.net/m0_38127487/article/details/125420062