绘图技巧【康华同学】:优秀生物信息学博客

R语言ggplot2绘制热图展示GO富集分析结果的简单小例子

2021-08-25  本文已影响0人  小明的数据分析笔记本

昨天有读者在公众号留言问下面这个热图如何画

image.png

这个图的实现办法有很多,今天的推文介绍一下使用R语言的ggplot2实现上图的代码。

首先是构造示例数据

构造两份数据

构造数据用到的代码

x<-seq(0,1,by=0.001)
set.seed(1234)
x1<-sample(x,240)
mymatrix<-matrix(x1,ncol=6)
head(mymatrix)
colnames(mymatrix)<-paste0("gene",1:6)
rownames(mymatrix)<-paste0("GO:000",1:40," ",
                           sample(LETTERS[1:26],40,replace = T))
write.csv(mymatrix,file = "GO_qvalue.csv",quote=F,row.names = T)
dfclass<-data.frame(x="class",
                    y=rownames(mymatrix),
                    group=c(rep("Biological Process",25),
                            rep("Cellular Component",5),
                            rep("Molecular Function",10)))
write.csv(dfclass,file = "class.csv",quote=F,row.names = F)

大家可以自己运行代码得到示例数据,或者直接在文末留言

数据部分截图如下

image.png image.png
首先是画右侧的如图

最基本的热图代码

df1<-read.csv("GO_qvalue.csv",header = T,row.names = 1)
df1$GO_term<-rownames(df1)
df1.1<-reshape2::melt(df1,var.id="GO_term")
head(df1.1)
df1.1$GO_term<-factor(df1.1$GO_term,
                      levels = row.names(df1))
library(ggplot2)
ggplot(df1.1,aes(x=variable,y=GO_term))+
  geom_tile(aes(fill=value))
image.png
接下来是美化
ggplot(df1.1,aes(x=variable,y=GO_term))+
  geom_tile(aes(fill=value),color="grey")+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0),
                   position = "right")+
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_text(angle = 90,hjust=1,vjust = 0.5))+
  scale_fill_gradient(low="red",high="green")

image.png

说实话这个红绿配色的热图我真欣赏不来,我们换一个配色吧还是

ggplot(df1.1,aes(x=variable,y=GO_term))+
  geom_tile(aes(fill=value),color="grey")+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0),
                   position = "right")+
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_text(angle = 90,hjust=1,vjust = 0.5))+
  scale_fill_viridis_c()
image.png

这个颜色看起来还挺舒服的

接下来是左侧的分组颜色条

df2<-read.csv("class.csv",header = T)
head(df2)
df2$y<-factor(df2$y,
              levels = rownames(df1))
ggplot(df2,aes(x=x,y=y))+
  geom_tile(aes(fill=group),color="grey")+
  theme(panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_text(angle=90,hjust=1,vjust=0.5))+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  scale_fill_manual(name="class",
                    values = c("#619cff","#00ba38","#f8766d"))
image.png
最后就是拼图了
library(ggplot2)
p1<-ggplot(df1.1,aes(x=variable,y=GO_term))+
  geom_tile(aes(fill=value),color="grey")+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0),
                   position = "right")+
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_text(angle = 90,hjust=1,vjust = 0.5))+
  scale_fill_viridis_c(name="Q-value")



p2<-ggplot(df2,aes(x=x,y=y))+
  geom_tile(aes(fill=group),color="grey")+
  theme(panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_text(angle=90,hjust=1,vjust=0.5))+
  #scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  scale_fill_manual(name="class",
                    values = c("#619cff","#00ba38","#f8766d"))

library(aplot)
p1%>%
  insert_left(p2,0.1)

最终的结果如下

image.png

欢迎大家关注我的公众号
小明的数据分析笔记本

上一篇下一篇

猜你喜欢

热点阅读