对不同分页界面的分组数据添加显著性标记符号,虽然也可以通过ggpubr包的stat_compare_means()添加分组显著性标记符号,但它要求x轴必须是分组变量,不能对fill映射出来的分组做处理。为应对不同分组的fill映射分组,我们需要另寻他法。本文通过geom_text, geom_segment函数添加分组显著性标记。更多知识分享请到 https://zouhua.top/。
加载R包
library(ggplot2)
library(tibble)
library(dplyr)
library(Rmisc)
导入数据
dat <- read.table("../datset/butterflies.txt", header = T, sep = " ")
glimpse(dat)
汇总数据
datsum <- summarySE(data = dat, measurevar = "winglen",
groupvars = c("spp", "sex", "region"))
head(datsum)
构建显著性标记
显著性标记主要有三部分组成(如图):
-
两侧的两条竖线;
-
中间的横线;
-
显著性标记 *
- 单个界面设置显著性标记:构建显著性标记矩阵
anno <- data.frame(x1 = 1.75, x2 = 2.25,
y1 = 36, y2 = 37,
xstar = 2, ystar = 38,
lab = "***")
anno
-
单个界面分组的画图:
-
geom_point: 绘制mean值点
-
geom_errorbar: 绘制以一倍标准误差波动的errorbar
-
geom_text: 添加显著性标记
-
geom_segment: 添加对应线条
-
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()
- 多个界面设置显著性标记:构建显著性标记矩阵
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"))
head(anno)
- 绘制不同界面显著性标记图
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))
总结
设置不同分组的显著性标记时,x轴分组是以1开始计数,而fill分组分别在+/-0.25处。在设置不同界面分组时,需要设定不同region参数。
系统信息
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
system code page: 936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rmisc_1.5 plyr_1.8.6 lattice_0.20-41 dplyr_1.0.2 tibble_3.0.3 ggplot2_3.3.3
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.19 purrr_0.3.4 haven_2.3.1 carData_3.0-4
[6] colorspace_1.4-1 vctrs_0.3.4 generics_0.1.0 htmltools_0.5.1.1 yaml_2.2.1
[11] rlang_0.4.7 ggpubr_0.4.0 pillar_1.4.6 foreign_0.8-80 glue_1.4.2
[16] withr_2.3.0 readxl_1.3.1 lifecycle_0.2.0 stringr_1.4.0 munsell_0.5.0
[21] ggsignif_0.6.0 gtable_0.3.0 cellranger_1.1.0 zip_2.1.1 evaluate_0.14
[26] knitr_1.30 rio_0.5.16 forcats_0.5.0 curl_4.3 broom_0.7.3
[31] Rcpp_1.0.4.6 backports_1.1.10 scales_1.1.1 jsonlite_1.7.1 abind_1.4-5
[36] hms_0.5.3 digest_0.6.25 stringi_1.5.3 openxlsx_4.2.2 rstatix_0.6.0
[41] grid_4.0.2 tools_4.0.2 magrittr_1.5 crayon_1.3.4 car_3.0-10
[46] tidyr_1.1.2 pkgconfig_2.0.3 ellipsis_0.3.1 rsconnect_0.8.16 data.table_1.13.6
[51] rmarkdown_2.5 rstudioapi_0.11 R6_2.5.0 compiler_4.0.2
参考
参考文章如引起任何侵权问题,可以与我联系,谢谢。
网友评论