本篇文章为大家展示了R语言ggplot2绘制热图展示GO富集分析结果的是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
这个图的实现办法有很多,今天的推文介绍一下使用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)
大家可以自己运行代码得到示例数据,或者直接在文末留言
数据部分截图如下
最基本的热图代码
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))
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")
说实话这个红绿配色的热图我真欣赏不来,我们换一个配色吧还是
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()
这个颜色看起来还挺舒服的
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"))
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)
最终的结果如下
上述内容就是R语言ggplot2绘制热图展示GO富集分析结果的是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。