在之前的文章中,我们分享了多个基因差异分析的可视化,使用的是ggpubr这个R包,ggpubr在标记p值时,可以根据指定的差异分组自动添加组间的连线,非常方便,但是无法指定添加的p值的位置,在某些时候会缺乏灵活性,今天要介绍的是另外一个R包ggsignif,其帮助手册链接如下
https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html
首先我们用示例数据跑一跑
> library(ggplot2)
> library(ggsignif)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> ggplot(iris, aes(x = Species, y = Sepal.Length)) +
+ geom_boxplot() +
+ geom_signif(
+ comparisons = list(c("versicolor", "virginica"))
+ )
效果图如下
通过comparisons参数来手动指定需要比较的两组,就会自动在上面添加p值和连线,默认都在顶部添加,当我么同时指定了多组数据的比较时,就会重叠,示例如下
> ggplot(iris, aes(x = Species, y = Sepal.Length)) +
+ geom_boxplot() +
+ geom_signif(
+ comparisons = list(
+ c("versicolor", "virginica"),
+ c("setosa", "virginica"),
+ c("setosa", "versicolor")
+ )
+ )
效果图如下
为了避免这个问题,ggsignif还支持直接指定文字注释的内容和横线的宽高度,代码如下
> ggplot(iris, aes(x = Species, y = Sepal.Length)) +
+ geom_boxplot() +
+ geom_signif(
+ annotations = c("First", "Second", "Third"),
+ y_position = c(8, 8.2, 8.5),
+ xmin = c(1, 2, 1),
+ xmax = c(2, 3, 3)
+ )
效果图如下
掌握了基本用法之后,就可以来复现文章中的图表了,首先是两组间的差异,代码如下
> data <- iris[iris$Species %in% c("versicolor", "virginica"), ]
> ggplot(data, aes(x = Species, y = Sepal.Length, fill = Species)) +
+ geom_boxplot() +
+ geom_signif(comparisons = list(c("versicolor", "virginica"))) +
+ theme_classic() +
+ theme(legend.position = "top")
效果图如下
再来复现一个三组比较的,文献中插入如下所示
代码如下
> ggplot(data, aes(x = Species, y = Sepal.Length, fill = Species)) +
+ geom_boxplot() +
+ geom_signif(comparisons = list(c("versicolor", "virginica"))) +
+ theme_classic() +
+ theme(legend.position = "top")
> ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
+ geom_boxplot() +
+ geom_signif(
+ annotations = c("*", "**", "***"),
+ y_position = c(8, 8.2, 8.5),
+ xmin = c(1, 2, 1),
+ xmax = c(2, 3, 3)
+ ) +
+ theme_classic() +
+ theme(legend.position = "top")
效果图如下
通过y_position, xmin, xmax参数来指定p值的位置,通过annotations参数指定标记的具体信息,提升了灵活性。
·end·