不同分页界面分组添加显著性标记符号

介绍

对不同分页界面的分组数据添加显著性标记符号,虽然也可以通过ggpubr包的stat_compare_means()添加分组显著性标记符号,但它要求x轴必须是分组变量,不能对fill映射出来的分组做处理。为应对不同分组的fill映射分组,我们需要另寻他法。本文通过geom_text, geom_segment函数添加分组显著性标记。

加载R包和导入数据

1
2
3
4
5
6
library(ggplot2)
library(dplyr)
library(Rmisc)

dat <- read.table("../datset/butterflies.txt", header = T, sep = " ")
glimpse(dat)

汇总数据

1
2
3
datsum <- summarySE(data = dat, measurevar = "winglen", 
groupvars = c("spp", "sex", "region"))
head(datsum)

构建显著性标记

显著性标记主要有三部分组成(如图):

  1. 两侧的两条竖线;
  2. 中间的横线;
  3. 显著性标记 *
  • 单个界面设置显著性标记:构建显著性标记矩阵
1
2
3
4
anno <- data.frame(x1 = 1.75, x2 = 2.25, 
y1 = 36, y2 = 37,
xstar = 2, ystar = 38,
lab = "***")
  • 单个界面分组的画图:
    • geom_point: 绘制mean值点
    • geom_errorbar: 绘制以一倍标准误差波动的errorbar
    • geom_text: 添加显著性标记
    • geom_segment: 添加对应线条
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
datNsum <- dat %>% filter(region == "North") %>%
summarySE(measurevar = "winglen",
groupvars = c("spp", "sex"))

ggplot(data = datNsum, aes(x = spp, y = winglen))+
geom_point(aes(colour = sex), position = position_dodge(width = 1)) +
geom_errorbar(aes(colour = sex, ymin = winglen - se, ymax = winglen + se),
width = .2, position = position_dodge(width = 1)) +
ylim(0, 40) +
geom_text(data = anno, aes(x = xstar, y = ystar, label = lab)) +
geom_segment(data = anno, aes(x = x1, xend = x1,
y = y1, yend = y2),
colour = "black") +
geom_segment(data = anno, aes(x = x2, xend = x2,
y = y1, yend = y2),
colour = "black") +
geom_segment(data = anno, aes(x = x1, xend = x2,
y = y2, yend = y2),
colour = "black")+
theme_bw()

  • 多个界面设置显著性标记:构建显著性标记矩阵
1
2
3
4
5
anno <- data.frame(x1 = c(1.75, 0.75), x2 = c(2.25, 1.25), 
y1 = c(36, 36), y2 = c(37, 37),
xstar = c(2, 1), ystar = c(38, 38),
lab = c("***", "**"),
region = c("North", "South"))
  • 绘制不同界面显著性标记图
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
ggplot(data = datsum, aes(x = spp, y = winglen)) +
geom_point(aes(shape = sex), position = position_dodge(width = 1), size = 2) +
scale_shape_manual(values = c(1, 19), labels = c("Female", "Male") )+
geom_errorbar(aes(group = sex, ymin = winglen - se, ymax = winglen + se),
width = .2, position = position_dodge(width = 1)) +
geom_hline(yintercept = seq(0, 40, 10), color = "gray") +
labs(x="", y="Wing length (mm)")+
scale_y_continuous(breaks = seq(0, 40, 10),
expand = c(0, 0),
limits = c(0, 40))+
geom_text(data = anno, aes(x = xstar, y = ystar, label = lab)) +
geom_segment(data = anno, aes(x = x1, xend = x1,
y = y1, yend = y2),
colour = "black") +
geom_segment(data = anno, aes(x = x2, xend = x2,
y = y1, yend = y2),
colour = "black") +
geom_segment(data = anno, aes(x = x1, xend = x2,
y = y2, yend = y2),
colour = "black")+
facet_grid(. ~ region) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
strip.text = element_text(color = 'black', size = 14, face = "bold"),
axis.title = element_text(color = 'black', size = 14, face = "bold"),
axis.text = element_text(color = 'black', size = 12),
text = element_text(size = 10, color = "black", family="serif"),
legend.key = element_blank(),
legend.title = element_blank(),
axis.line = element_line(color = "black", size = 1),
panel.border = element_rect(colour = "black", fill=NA, size=1))

参考

  1. Adding different annotation to each facet in ggplot

参考文章如引起任何侵权问题,可以与我联系,谢谢。


------------- The End Thanks for reading --------