0
点赞
收藏
分享

微信扫一扫

add-p-values-to-ggplot-facets-with-different-scales

在这里插入图片描述

set.seed(20190708)
genes <- paste("gene",1:1000,sep="")
x <- list(
  A = sample(genes,300), 
  B = sample(genes,525), 
  C = sample(genes,440),
  D = sample(genes,350)
)
if (!require(devtools)) install.packages("devtools")
devtools::install_github("yanlinlin82/ggvenn")
library(ggvenn)
ggvenn(
  x, 
  fill_color = c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF"),
  stroke_size = 0.5, set_name_size = 4
)

在这里插入图片描述

if (!require(devtools)) install.packages("devtools")
devtools::install_github("gaospecial/ggVennDiagram")
library("ggVennDiagram")
ggVennDiagram(x, label_alpha = 0)

在这里插入图片描述

在这里插入图片描述
Facet with fixed scales

library(ggpubr)
library(rstatix)
# Transform `dose` into factor variable
df <- ToothGrowth
df$dose <- as.factor(df$dose)
# Add a random grouping variable
df$group <- factor(rep(c("grp1", "grp2"), 30))
# Add some extremely high values in column 1 at rows c(1, 3, 5).
df[c(1, 3, 5),  1] <- c(500, 495, 505)
head(df, 3)
stat.test <- df %>%
  group_by(group, supp) %>%
  tukey_hsd(len ~ dose) 
stat.test 
# Create bar plots with significance levels
# Hide ns (non-significant)
# Add 15% space between labels and the plot top border
stat.test <- stat.test %>% add_xy_position(x = "dose", fun = "mean_se")
ggbarplot(
  df, x = "dose", y = "len", fill = "#00AFBB",
  add = "mean_se", facet = c("supp", "group")
) +
  stat_pvalue_manual(stat.test, hide.ns = TRUE, tip.length = 0, step.increase = 0) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

Facet with free scales
Facet wrap
You need to specify the option scales = “free” in both the add_xy_position() and in the ggbarplot() functions.
在这里插入图片描述

stat.test <- stat.test %>% 
  add_xy_position(x = "dose", fun = "mean_se", scales = "free")
ggbarplot(
  df, x = "dose", y = "len", fill = "#00AFBB",
  add = "mean_se", facet.by = c("supp", "group")
) +
  facet_wrap(vars(supp, group), scales = "free") +
  stat_pvalue_manual(stat.test, hide.ns = TRUE, tip.length = 0) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

Facet grid
When the scales = “free” argument is added in facet grid, plots on the same row cannot have different y-axis. Similarly, there can be only single x-axis for each column. Using facet_wrap(), each plot is displayed independently, so it can “free” its x-axis and y-axis.

Facet grid is useful when you want to relatively compare the plots within a category, which can be accomplished by setting the same axis scales. Meanwhile, facet wrap is more useful for plots that are more independent between one another.

There are two possible solutions to customize the y position of significance levels.

Solution 1: Using the option step.increase
The default of the function add_xy_position() is to automatically compute a global step increase value between brackets. This calculation assumes that the y scales of plot panels are fixed.

In the situation, where you want free scales, you can:

Set the option step.increase to 0 when calling the function add_xy_position().
Specify only the option step.increase in the function stat_pvalue_manual(). In this case, the step.increase will be adapted to each plot panel.
在这里插入图片描述

stat.test <- stat.test %>% 
  add_xy_position(x = "dose", fun = "mean_se", step.increase = 0)
bp <- ggbarplot(
  df, x = "dose", y = "len", fill = "#00AFBB", add = "mean_se",
  facet.by = c("supp", "group"), scales = "free"
) 
bp +
  stat_pvalue_manual(stat.test, hide.ns = TRUE, tip.length = 0, step.increase = 0.2) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

Solution 2: Using the option scales = “free”.
In facet grid, the scales of the generated plot panels are not completely free. Consequently you will need more customization to adapt the look of the significance level position. You will have to play with the options step.increase and bracket.nudge.y in the stat_pvalue_manual() function.
在这里插入图片描述

stat.test <- stat.test %>% 
  add_xy_position(x = "dose", fun = "mean_se", scales = "free")
bp <- ggbarplot(
  df, x = "dose", y = "len", fill = "#00AFBB", add = "mean_se",
  facet.by = c("supp", "group"), scales = "free"
) 
bp +
  stat_pvalue_manual(stat.test, hide.ns = TRUE, tip.length = 0) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

在这里插入图片描述

bp +
  stat_pvalue_manual(
    stat.test, hide.ns = TRUE, tip.length = 0,
    step.increase = 0.1
  ) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

在这里插入图片描述

bracket.nudge.y <- c(
  -2, -3,               # Panel 1: grp1/OJ
  -100, -160,           # Panel 2: grp1/VC
  -10, -11,             # Panel 3: grp2/OJ
  -250, -250, -250      # Panel 4: grp2/VC
)
bp +
  stat_pvalue_manual(
    stat.test, hide.ns = TRUE, tip.length = 0,
    step.increase = 0.09, bracket.nudge.y = bracket.nudge.y
  ) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

参考资料:
https://www.datanovia.com/en/blog/add-p-values-to-ggplot-facets-with-different-scales/

举报

相关推荐

p4 add 目录

0 条评论