查看原文
其他

R可视化——环形热图

王志山 科研后花园 2023-09-08



引言



     像上面这种环形热图在生物信息学分析过程中被经常用到,但是通过一些软件去绘制又比较复杂,所以,今天小编给大家介绍一下如何使用R语言中的circlize包来绘制属于自己的环形热图。



正文——代码
1

前期准备





1)设置工作目录

rm(list=ls())#clear Global Environmentsetwd('D:\\桌面\\环形热图')#设置工作路径

2)加载包

#安装包if(!requireNamespace("BiocManager", quietly = T)) install.packages("BiocManager") BiocManager::install("ComplexHeatmap")install.packages("circlize")#加载包library(circlize)library(ComplexHeatmap)


2

读取数据





df <- read.table(file="example.txt",sep="\t",header=T,check.names=FALSE,row.names = 1)head(df)



3

常见热图绘制





Heatmap(df)




4

环形热图绘制





1)使用circlize包中的circos.heatmap函数绘制环形热图,先查看该函数的参数

??circos.heatmap
circos.heatmap(mat, split = NULL, col, na.col = "grey", cell.border = NA, cell.lty = 1, cell.lwd = 1, bg.border = NA, bg.lty = par("lty"), bg.lwd = par("lwd"), ignore.white = is.na(cell.border), cluster = TRUE, clustering.method = "complete", distance.method = "euclidean", dend.callback = function(dend, m, si) reorder(dend, rowMeans(m)), dend.side = c("none", "outside", "inside"), dend.track.height = 0.1, rownames.side = c("none", "outside", "inside"), rownames.cex = 0.5, rownames.font = par("font"), rownames.col = "black",               show.sector.labels = FALSE, cell_width = rep(1, nrow(mat)), ...)

2)绘图

#颜色color <- colorRamp2(c(-5, 0, 5), c("blue", "white", "green"))circos.par(gap.after = c(20))#间隔circos.heatmap(df, #数据 col = color,#颜色 dend.side = "inside",#确定聚类结果放在圈内还是圈外 rownames.side = "outside",#组名 track.height = 0.4 # clustering.method = "complete",#归一化处理 # distance.method = "euclidean"#聚类方法,默认为欧氏距离 )

3)添加组名

circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) { if(CELL_META$sector.numeric.index == 1) { A = length(colnames(df)) circos.text(rep(CELL_META$cell.xlim[2], A) + convert_x(0.2, "mm"), #x坐标 28+(1:A)*10,#y坐标 colnames(df), #标签 cex = 0.5, adj = c(0, 1), facing = "inside") }}, bg.border = NA)##组名的标签位置需要耐心调整参数以到合适的位置,当然也可以导出PDF在AI中进行添加及位置调整

4)添加图例

grid.draw(Legend(title = "Title", col_fun = color))

注意

circos.clear()#清除参数,如果前面需要调整参数,必须先执行此命令,否则绘制的新图会和之前的图重叠在一起


5

拓展





通过该函数也可以实现多个热图的绘制:

color2=colorRamp2(c(-5, 0, 5), c("green", "white", "red"))circos.heatmap(df, col = color, dend.side = "outside")circos.heatmap(df, col = color2,rownames.side = "inside")circos.clear()



参考资料:https://jokergoo.github.io/circlize_book/book/

源码及数据在后台回复“环形热图”获取!!!




欢迎大家点赞、转发并分享,让更多朋友看到!!!


我就知道你“在看”


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存