GGPUBR: How to Add Adjusted P-values to a Multi-Panel GGPlot

plot of chunk ggpubr-add-adjusted-p-values-to-multi-panel-ggplot


GGPUBR: How to Add Adjusted P-values to a Multi-Panel GGPlot

# Load required R packages
library(ggpubr)
library(rstatix)

# Data preparation
df <- tibble::tribble(
  ~sample_type, ~expression, ~cancer_type,   ~gene,
      "cancer",        25.8,       "Lung", "Gene1",
      "cancer",        25.5,      "Liver", "Gene1",
      "cancer",        22.4,      "Liver", "Gene1",
      "cancer",        21.2,       "Lung", "Gene1",
      "cancer",        24.5,      "Liver", "Gene1",
      "cancer",        27.3,      "Liver", "Gene1",
      "cancer",        30.9,      "Liver", "Gene1",
      "cancer",        17.6,     "Breast", "Gene1",
      "cancer",        19.7,       "Lung", "Gene1",
      "cancer",         9.7,     "Breast", "Gene1",
      "cancer",        15.2,     "Breast", "Gene2",
      "cancer",        26.4,      "Liver", "Gene2",
      "cancer",        25.8,       "Lung", "Gene2",
      "cancer",         9.7,     "Breast", "Gene2",
      "cancer",        21.2,       "Lung", "Gene2",
      "cancer",        24.5,      "Liver", "Gene2",
      "cancer",        14.5,     "Breast", "Gene2",
      "cancer",        19.7,       "Lung", "Gene2",
      "cancer",        25.2,       "Lung", "Gene2",
      "normal",        43.5,       "Lung", "Gene1",
      "normal",        76.5,      "Liver", "Gene1",
      "normal",        21.9,     "Breast", "Gene1",
      "normal",        69.9,      "Liver", "Gene1",
      "normal",       101.7,      "Liver", "Gene1",
      "normal",        80.1,      "Liver", "Gene1",
      "normal",        19.2,     "Breast", "Gene1",
      "normal",        49.5,       "Lung", "Gene1",
      "normal",        34.5,     "Breast", "Gene1",
      "normal",        51.9,       "Lung", "Gene1",
      "normal",        67.5,       "Lung", "Gene2",
      "normal",          30,     "Breast", "Gene2",
      "normal",        76.5,      "Liver", "Gene2",
      "normal",        88.5,      "Liver", "Gene2",
      "normal",        69.9,      "Liver", "Gene2",
      "normal",        49.5,       "Lung", "Gene2",
      "normal",        80.1,      "Liver", "Gene2",
      "normal",        79.2,      "Liver", "Gene2",
      "normal",        12.6,     "Breast", "Gene2",
      "normal",        97.5,      "Liver", "Gene2",
      "normal",        64.5,      "Liver", "Gene2"
  )

# Summary statistics
df %>%
  group_by(gene, cancer_type, sample_type) %>%
  get_summary_stats(expression, type = "common")
## # A tibble: 12 x 13
##    sample_type cancer_type gene  variable     n   min   max median   iqr  mean
##    <chr>       <chr>       <chr> <chr>    <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl>
##  1 cancer      Breast      Gene1 express…     2   9.7  17.6   13.6  3.95  13.6
##  2 normal      Breast      Gene1 express…     3  19.2  34.5   21.9  7.65  25.2
##  3 cancer      Liver       Gene1 express…     5  22.4  30.9   25.5  2.8   26.1
##  4 normal      Liver       Gene1 express…     4  69.9 102.    78.3 10.6   82.0
##  5 cancer      Lung        Gene1 express…     3  19.7  25.8   21.2  3.05  22.2
##  6 normal      Lung        Gene1 express…     3  43.5  51.9   49.5  4.2   48.3
##  7 cancer      Breast      Gene2 express…     3   9.7  15.2   14.5  2.75  13.1
##  8 normal      Breast      Gene2 express…     2  12.6  30     21.3  8.7   21.3
##  9 cancer      Liver       Gene2 express…     2  24.5  26.4   25.4  0.95  25.4
## 10 normal      Liver       Gene2 express…     7  64.5  97.5   79.2 11.1   79.5
## 11 cancer      Lung        Gene2 express…     4  19.7  25.8   23.2  4.53  23.0
## 12 normal      Lung        Gene2 express…     2  49.5  67.5   58.5  9     58.5
## # … with 3 more variables: sd <dbl>, se <dbl>, ci <dbl>
# Statistical test
# group the data by cancer type and gene
# Compare expression values of normal and cancer samples
stat.test <- df %>%
  group_by(cancer_type, gene) %>%
  t_test(expression ~ sample_type) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance()
stat.test
## # A tibble: 6 x 12
##   cancer_type gene  .y.   group1 group2    n1    n2 statistic    df       p   p.adj
## * <chr>       <chr> <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>   <dbl>   <dbl>
## 1 Breast      Gene1 expr… cancer normal     2     3    -1.88   2.92 1.60e-1 9.60e-1
## 2 Breast      Gene2 expr… cancer normal     3     2    -0.921  1.08 5.17e-1 1.00e+0
## 3 Liver       Gene1 expr… cancer normal     5     4    -7.96   3.26 3.04e-3 1.82e-2
## 4 Liver       Gene2 expr… cancer normal     2     7   -12.6    6.53 7.90e-6 4.74e-5
## 5 Lung        Gene1 expr… cancer normal     3     3    -8.41   3.67 1.58e-3 9.48e-3
## 6 Lung        Gene2 expr… cancer normal     4     2    -3.89   1.06 1.50e-1 9.00e-1
## # … with 1 more variable: p.adj.signif <chr>
# Create boxplot
bxp <- ggboxplot(
  df, x = "sample_type", y = "expression", 
  facet.by = c("gene", "cancer_type")
  ) +
  rotate_x_text(angle = 60)

# Add adjusted p-values
stat.test <- stat.test %>% add_xy_position(x = "sample_type")
bxp + stat_pvalue_manual(stat.test, label = "p.adj")

plot of chunk ggpubr-add-adjusted-p-values-to-multi-panel-ggplot

# Display the p-value significance levels
bxp + stat_pvalue_manual(stat.test, label = "p.adj.signif")

plot of chunk ggpubr-add-adjusted-p-values-to-multi-panel-ggplot

# Hide ns and change the bracket tip length
bxp + stat_pvalue_manual(
  stat.test, label = "p.adj.signif",
  hide.ns = TRUE, tip.length = 0
  )

plot of chunk ggpubr-add-adjusted-p-values-to-multi-panel-ggplot

# Show p-values and significance levels
bxp + stat_pvalue_manual(
  stat.test, label = "{p.adj}{p.adj.signif}",
  hide.ns = TRUE, tip.length = 0
  )

plot of chunk ggpubr-add-adjusted-p-values-to-multi-panel-ggplot

Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments