[R language]——Drawing a circular heat map 4 (nanny-level tutorial)

The previous issue "[R Language] - Drawing heat maps grouped by clustering results 3" introduced the R language pheatmap package to draw heat maps grouped by clustering results. This issue mainly introduces the use of ComplexHeatmap and circlize packages to draw circular heat maps. , the circular heat map can turn an overly long heat map into a circular shape, thereby saving space and making the gene labels clearer and more beautiful.

1 Data preparation

Data input format (csv format):

 

2 R package loading and data import

#下载包#

install.packages("circlize")

BiocManager::install("ComplexHeatmap")

install.packages("RColorBrewer")

#加载包#

library('ComplexHeatmap')

library('circlize')

library("RColorBrewer")

#加载绘图数据#

#setwd("C:/Users/Desktop/环形热图")#设置工作路径

data<-read.table(file='C:/Rdata/jc/pheatmap.csv',header=TRUE,row.names= 1,sep=',')

#data=log2(data[,1:6]+1) #对基因表达量数据进行处理

head(data) #查看数据

#转化matrix格式矩阵及数据归一化

data <- as.matrix(data)

cir1<-t(scale(t(data)))

head(cir1) #查看数据#转化matrix格式矩阵及数据归一化

data <- as.matrix(data)

cir1<-t(scale(t(data)))

head(cir1) #查看数据

3 Ring heat map drawing

3.1 Draw ordinary heat map

#定义热图颜色梯度:

mycol=colorRamp2(c(-2.5, 0.3, 3.1),c("blue","white","red"))#设置legend颜色,范围;可从https://www.58pic.com/peisebiao/网站进行配色

mycol1 = colorRamp2(c(-2, 0, 2), c("#003399", "white", "#cccc00"))

#mycol= colorRamp2(c(-1.45, 0, 2.27),c("#0273c2","white","#efc001"))

#mycol= colorRamp2(c(-1.45, 0, 2.27),c("#0da9ce","white","#e74a32"))

#默认参数绘制普通热图

Heatmap(cir1)

Heatmap(cir1,row_names_gp = gpar(fontsize = 6),

        column_names_gp= gpar(fontsize = 8),

        col= mycol,

        name="legend")

#计算数据大小范围

range(cir1)

 

Figure 1 A common heat map drawn

3.2 Basic circular heat map drawing

#绘制基础环形热图:

circos.heatmap(cir1,col=mycol)

circos.clear()#绘制完成后需要使用此函数完全清除布局

 

Figure 2 Basic ring heat map

3.3 Adjust and beautify the circular heat map

3.3.1 Preliminary adjustments to the donut heat map

#在circos.heatmap()中添加参数进行环形热图的调整和美化:

circos.par(gap.after=c(30)) #circos.par()调整圆环首尾间的距离,数值越大,距离越宽

circos.heatmap(cir1,col=mycol,

               dend.side="inside", #聚类放在环形内测,控制行聚类树的方向,inside为显示在圆环内圈,outside为显示在圆环外圈

               rownames.side="outside", #基因名放在环形外侧,控制矩阵行名的方向,与dend.side相同;但注意二者不能在同一侧,必须一内一外

               rownames.col="black",

               rownames.cex=0.5, #字体大小

               rownames.font=0.5, #字体粗细

               bg.border = "black",#背景边缘颜色

               #show.sector.labels = T,#显示分的区域的标签

               cluster=TRUE) #cluster=TRUE为对行聚类,cluster=FALSE则不显示聚类

circos.clear()#画完图结束。一定要运行这个,不然后续画图会叠加

Figure 3 Adjusted circular heat map

 

3.3.2 Adjust and beautify the circular heat map clustering tree

#聚类树的调整和美化:

install.packages("dendextend")#改颜色

install.packages("dendsort")#聚类树回调

library(dendextend)

library(dendsort)

circos.par(gap.after=c(30)) #circos.par()调整圆环首尾间的距离,数值越大,距离越宽

circos.heatmap(cir1,col=mycol,dend.side="inside",#dend.side:控制行聚类树的方向,inside为显示在圆环内圈,outside为显示在圆环外圈

               rownames.side="outside",#rownames.side:控制矩阵行名的方向,与dend.side相同;但注意二者不能在同一侧,必须一内一外

               track.height = 0.28, #轨道的高度,数值越大圆环越粗

               rownames.col="black",

               rownames.cex=1, #字体大小

               rownames.font=1, #字体粗细

               cluster=TRUE, #cluster=TRUE为对行聚类,cluster=FALSE则不显示聚类

               dend.track.height=0.18, #调整行聚类树的高度

               dend.callback=function(dend,m,si) {#dend.callback:用于聚类树的回调,当需要对聚类树进行重新排序,或者添加颜色时使用包含的三个参数:dend:当前扇区的树状图;m:当前扇区对应的子矩阵;si:当前扇区的名称

                 color_branches(dend,k=15,col=1:15) #color_branches():修改聚类树颜色#聚类树颜色改为1,即单色/黑色

               }

)

Figure 4 Heat map after clustering tree adjustment and beautification

3.3.3 Annular heat map legend adjustment and column name addition

#添加图例标签等

#library(ComplexHeatmap)

#install.packages("gridBase")

library(gridBase)

lg=Legend(title="Exp",col_fun=mycol,

          direction = c("vertical"),

          #title_position= c('topcenter'),

)

grid.draw(lg)

#draw(lg, x = unit(0.9,"npc"), y = unit(0.5,"npc"), just = c("right","center"))#画在右边

#添加列名:

circos.track(track.index=get.current.track.index(),panel.fun=function(x,y){

  if(CELL_META$sector.numeric.index==1){   #if(CELL_META$sector.numeric.index == 3) { # the last sector

    cn=colnames(cir1)

    n=length(cn)

    circos.text(rep(CELL_META$cell.xlim[2],n)+convert_x(0.5,"mm"),#x坐标

                1:n+5,#调整y坐标

                cn,cex=0.6,adj=c(0,0.5),facing="inside")}

},bg.border=NA)

circos.clear()

 

Figure 5 The heat map after the legend of the circular heat map is adjusted and the column names are added.

3.4 Grouped circular heat map drawing

3.4.1 Single track grouping

#分组热图绘制#

library(dendextend)

#但如果矩阵数据分组,可用split参数来指定分类变量

ann_row = data.frame(pathway=c(rep("pathway1",25),rep("pathway2",15),rep("pathway3",10)))#对行进行注释,用于后续的热图分裂

row.names(ann_row) = rownames(cir1)

ann_row <- as.matrix(ann_row)#在circlize函数中,需要为matrix

#分组绘图

circos.par(gap.after=c(2,2,30)) #circos.par()调整圆环首尾间的距离,数值越大,距离越宽#让分裂的一个口大一点,可以添加行信息

circos.heatmap(cir1,col=mycol,

               dend.side="inside",#dend.side:控制行聚类树的方向,inside为显示在圆环内圈,outside为显示在圆环外圈

               rownames.side="outside",#rownames.side:控制矩阵行名的方向,与dend.side相同;但注意二者不能在同一侧,必须一内一外

               track.height = 0.28, #轨道的高度,数值越大圆环越粗

               rownames.col="black",

               bg.border="black", #背景边缘颜色

               split = ann_row,#用行注释分裂热图

               show.sector.labels = T,

               rownames.cex=0.9,#字体大小

               rownames.font=1,#字体粗细

               cluster=TRUE,#cluster=TRUE为对行聚类,cluster=FALSE则不显示聚类

               dend.track.height=0.18,#调整行聚类树的高度

               dend.callback=function(dend,m,si) {#dend.callback:用于聚类树的回调,当需要对聚类树进行重新排序,或者添加颜色时使用包含的三个参数:dend:当前扇区的树状图;m:当前扇区对应的子矩阵;si:当前扇区的名称

                 color_branches(dend,k=10,col=1:10)#color_branches():修改聚类树颜色#聚类树颜色改为1,即单色/黑色

               }

)
#图例与列名设置

lg=Legend(title="Legend",col_fun=mycol,direction = c("horizontal"))

grid.draw(lg)

circos.track(track.index=get.current.track.index(),panel.fun=function(x,y){

  if(CELL_META$sector.numeric.index==3){# the last sector

    cn=colnames(cir1)

    n=length(cn)

    circos.text(rep(CELL_META$cell.xlim[2],n)+convert_x(1,"mm"),#x坐标

                (1:n)*0.2-1.3,#调整y坐标,行距+距离中心距(1:n)*1.2+5,

                cn,cex=0.8,adj=c(0,1),facing="inside")

  }

},bg.border=NA)

circos.clear()

Figure 6 Annular heat map of single track processing

 

3.4.2 Multi-track grouping

#多轨热图绘制#

#假设有两个热图的矩阵数据(这里仅为一组重复两次以作示范)

cir1<-t(scale(t(data)))

cir2<-t(scale(t(data)))

#但如果矩阵数据分组,可用split参数来指定分类变量

ann_row = data.frame(pathway=c(rep("pathway1",25),rep("pathway2",15),rep("pathway3",10)))#对行进行注释,用于后续的热图分裂

ann_row2 = data.frame(pathway=c(rep("pathway1",25),rep("pathway2",15),rep("pathway3",10)))#对行进行注释,用于后续的热图分裂

row.names(ann_row) = rownames(cir1)

row.names(ann_row2) = rownames(cir2)

ann_row <- as.matrix(ann_row)#在circlize函数中,需要为matrix

ann_row2 <- as.matrix(ann_row2)

#绘图

circos.par(gap.after=c(2,2,30)) #circos.par()调整圆环首尾间的距离,数值越大,距离越宽#让分裂的一个口大一点,可以添加行信息

circos.heatmap(cir1,col=mycol,

               split=ann_row, #用行注释分裂热图

               rownames.col="black",

               show.sector.labels = T,

               #track.height = 0.28, #轨道的高度,数值越大圆环越粗

               #rownames.side="inside",控制矩阵行名的方向,与dend.side相同;但注意二者不能在同一侧,必须一内一外

               rownames.cex=0.2,#字体大小

               rownames.font=1,#字体粗细

               bg.border="black", #背景边缘颜色

               dend.side="outside",#dend.side:控制行聚类树的方向,inside为显示在圆环内圈,outside为显示在圆环外圈

               cluster=TRUE,#cluster=TRUE为对行聚类,cluster=FALSE则不显示聚类

               dend.track.height=0.2,#调整行聚类树的高度

               dend.callback=function(dend,m,si) { #dend.callback:用于聚类树的回调,当需要对聚类树进行重新排序,或者添加颜色时使用包含的三个参数:dend:当前扇区的树状图;m:当前扇区对应的子矩阵;si:当前扇区的名称

                 color_branches(dend,k=10,col=1:10) #color_branches():修改聚类树颜色#聚类树颜色改为1,即单色/黑色

               }

)

circos.heatmap(cir2,

               col = mycol1,

               split=ann_row2,

               rownames.side="inside",

               bg.border="black", #背景边缘颜色

               rownames.cex=0.3)#加入第二个热图

#添加列名#

#第一个环形列名

circos.track(track.index=get.current.track.index(),panel.fun=function(x,y){

  if(CELL_META$sector.numeric.index==3){# the last sector

    cn=colnames(cir1)

    n=length(cn)

    circos.text(rep(CELL_META$cell.xlim[2],n)+convert_x(1,"mm"),#x坐标

                (1:n)*0.4+3.6,#调整y坐标,行距+距离中心距(1:n)*1.2+5,

                cn,cex=0.6,adj=c(0,1),facing="inside")

  }

},bg.border=NA)

#第二个环形列名

circos.track(track.index=get.current.track.index(),panel.fun=function(x,y){

  if(CELL_META$sector.numeric.index==3){# the last sector

    cn=colnames(cir2)

    n=length(cn)

    circos.text(rep(CELL_META$cell.xlim[2],n)+convert_x(1,"mm"),#x坐标

                (1:n)*0.4+1,#调整y坐标,行距+距离中心距(1:n)*1.2+5,

                cn,cex=0.6,adj=c(0,1),facing="inside")

  }

},bg.border=NA)

#添加放置在左侧的图例#

install.packages("gridBase")

library(gridBase)

lg_Exp1=Legend(title="Exp1",col_fun=mycol,direction = c("vertical"))

lg_Exp2=Legend(title="Exp2",col_fun=mycol1,direction = c("vertical"))

circle_size= unit(0.07,"snpc")

h= dev.size()

lgd_list= packLegend(lg_Exp1,lg_Exp2, max_height = unit(2*h,"inch"))

draw(lgd_list, x = circle_size, just ="left")

circos.clear()

Figure 7 Multi-track heat map

 

Okay, that’s it for this sharing. In the next issue, we will share the correlation heat map drawing.

 

Scan the QR code to follow the official account and send "Ring Heat Map 4" to get the complete code and demo data package

Guess you like

Origin blog.csdn.net/weixin_54004950/article/details/128225038