砷在氟中

V1

2023/02/18阅读:36主题:默认主题

GO富集分析可视化——柱形图和棒棒糖图

在RNA-seq的GO富集分析中,若遇到较多的基因一起拿去GO富集分析,此时得到的GO term玩玩很多,而且GO term还可以分为BP(Biological Process)、CC(Cellular Component)和MF(Molecular Function),因此多的时候甚至能富集到100多个GO terms,此时可以借助柱形图或者棒棒糖进行可视化。

library(tidyverse)
library(cowplot)
library(ggsci)
library(openxlsx)
 
#=========================导入数据==========================
GO_term <- read.xlsx('NPvsCK.xlsx') %>%
  dplyr::select(Category, Description, Count)
 
#=========================棒棒糖图==========================
 
ggplot(data = GO_term, aes(x = Description, y = Count)) +
  geom_segment(aes(x = Description, y = 0, xend = Description, yend = Count, color = Category)) +
  geom_point(size = 4, aes(color = Category))+
  scale_color_npg() +
  scale_y_continuous(expand = c(0,0), limits = c(0,90)) +
  theme_half_open() +
  #旋转x轴的刻度
  theme(axis.text.x = element_text(angle = 90,
                                   size = 7,
                                   hjust = 1,
                                   vjust = 0.2))

绘图结果如下 从图中可以看出,虽然ggplot帮助我们画出了棒棒糖图,但是它并没有按照BP,MF,CC以及Count的数目进行排序,这是要是因为,我们没有设置Description的因子(factor),因此ggplot默认按Description的字母顺序进行排列,所以就显得画出来的图很无序,不美观。接下来就需要设置factor的levels。

要想设置按BP,CC,MF的顺序,以及Count的顺序进行绘图,就需要修稿Description这一列的factors里面的levels,因此我们利用arrange, 以Category为主要关键字,Count为次要关键字进行排序,从而得到新的levels的Description的顺序,代码如下:

GO_term <- read.xlsx('NPvsCK.xlsx') %>%
  dplyr::select(Category, Description, Count) %>%
  #先进行排序,然后得到排序后的Description的顺序
  arrange(Category, desc(Count)) %>%
  #将排序好后的Description赋值给factor里面的levels
  mutate(Description = factor(Description, levels = Description))

设置好Description这列的因子(factor)里面的levels后就有可以进行绘图了,代码还是上述代码:

ggplot(data = GO_term, aes(x = Description, y = Count)) +
  geom_segment(aes(x = Description, y = 0, xend = Description, yend = Count, color = Category)) +
  geom_point(size = 4, aes(color = Category))+
  scale_color_npg() +
  scale_y_continuous(expand = c(0,0), limits = c(0,90)) +
  theme_half_open() +
  theme(axis.text.x = element_text(angle = 90,
                                   size = 7,
                                   hjust = 1,
                                   vjust = 0.2))

运行结果如下: 从上图可以看出,修改levels后画出的图顺序并不混乱。而且容易看出哪些GO term里面的基因较多。

当然,我们也可以展示为柱形图,代码如下:

ggplot(data = GO_term, aes(x = Description, y = Count)) +
  geom_col(width = 0.7, aes(fill = Category))+
  scale_fill_npg() +
  scale_y_continuous(expand = c(0,0)) +
  theme_half_open() +
  #旋转x轴的刻度
  theme(axis.text.x = element_text(angle = 90,
                                   size = 7,
                                   hjust = 1,
                                   vjust = 0.2))

分类:

后端

标签:

后端

作者介绍

砷在氟中
V1