昨天有讀者在公眾號留言問下面這個熱圖如何畫
這個圖的實(shí)現(xiàn)辦法有很多,今天的推文介紹一下使用R語言的ggplot2實(shí)現(xiàn)上圖的代碼术裸。
首先是構(gòu)造示例數(shù)據(jù)
構(gòu)造兩份數(shù)據(jù)
- 一份是最左側(cè)的分組顏色條
- 一份是右側(cè)展示數(shù)值的熱圖
構(gòu)造數(shù)據(jù)用到的代碼
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)
大家可以自己運(yùn)行代碼得到示例數(shù)據(jù)限府,或者直接在文末留言
數(shù)據(jù)部分截圖如下
首先是畫右側(cè)的如圖
最基本的熱圖代碼
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")
說實(shí)話這個紅綠配色的熱圖我真欣賞不來愧哟,我們換一個配色吧還是
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()
這個顏色看起來還挺舒服的
接下來是左側(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)
最終的結(jié)果如下
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本