R语言绘制环状热图实操1

# rm(list=ls())#clear Global Environment# setwd('F:\\RStudio\\RStudio_workPath\\heatmap')#设置工作路径# # #安装包# if(!requireNamespace("BiocManager", quietly = T))# install.packages("BiocManager")# BiocManager::install("ComplexHeatmap", force=TRUE, update=F, ask=F)# install.packages("circlize", destdir="F:/R/install/downloaded_packages")#加载包library(circlize)library(ComplexHeatmap)#读取数据df <- read.table(file="example.txt",sep="\t",header=T,check.names=FALSE,row.names = 1)#常规热图# Heatmap(df)###绘制环形热图#颜色color <- colorRamp2(c(-5, 0, 5), c("blue", "white", "green"))#-5 和 5 之间的值被线性插值以获得相应的颜色,大于 5 的值都映射到green,小于 -5 的值都映射到blue。(自然这些参数可以根据自己需求更改,也支持16进制颜色编码字符串)circos.clear()circos.par(gap.after = c(30))#调整圆环首位间距;值越大,缺口越大,方便用于显示列名。circos.heatmap(df, #数据 col = color,#颜色 dend.side = "inside",#确定聚类结果放在圈内(inside)还是圈外(outside) rownames.side = "outside",#行名,经实测,与上面那个参数必须相反。即如果上面为outside,这行必须为inside track.height = 0.3,#热图的高度/厚度,值越大,热图越厚。 dend.track.height = 0.2,#控制树状图的高度,值越大,树状图越高。 # clustering.method = "complete",#归一化处理 # distance.method = "euclidean"#聚类方法,默认为欧氏距离)#添加一个新的环状热图# color2=colorRamp2(c(-5, 0, 5), c("green", "white", "red"))# circos.heatmap(df, col = color2,rownames.side = "inside")#添加列名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坐标,经实测,这个0.2值越大,列名离边缘越远。 28+(1:A)*3.5,#y坐标和间距,经实测,调整这个3.5基本可以达到目的(3.5越大,列名越靠上。自然可以为整数) # 1:n - convert_y(0.5, "mm"),#y坐标 colnames(df), #标签,即要展示的列名 cex = 0.5, #列名的大小 adj = c(0, 1), facing = "inside", #,inside, outside, clockwise, reverse.clockwise, downward, bending.inside 和 bending.outside ) }}, bg.border = NA)##列名的标签位置需要耐心调整参数以到合适的位置,当然也可以导出PDF在AI中进行添加及位置调整#添加图例grid.draw(Legend(title = "Title", col_fun = color))circos.clear()#清除参数,如果前面需要调整参数,必须先执行此命令,否则绘制的新图会和之前的图重叠在一起# #参考资料:https://jokergoo.github.io/circlize_book/book/# ??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)), ...)

参考:https://www.jianshu.com/p/f5c2faf11b9a

没想到微信公众号复制粘贴过来的不自动换行。。。下次注意。

你可能感兴趣的:(R语言绘制环状热图实操1)