原图
image.png复现结果
image.png复现代码
- 载入R包
library(ComplexHeatmap)
library(ggplot2)
library(dplyr)
library(circlize)
- 构造数据
data=data.frame(cell_type=c('cDC3_LAMP3','cDC2_CD1C','cDC1_CLEC9A'),
mregDC=c(0.95,0.05,0.10))
mat=data%>%tibble::column_to_rownames('cell_type')
col_fun=circlize::colorRamp2(c(0,1),c("#F5E2CE","#EE2928"),space = 'RGB')
mat=as.matrix(mat)
3.画图
## 文本绘制
layer_fun <- function(j, i, x, y, w, h, f) {
grid.text(round(pindex(mat, i, j), 2),
x, y, gp = gpar(fontsize = 10))
}
# 可视化
Heatmap(mat
,col = col_fun
,layer_fun = layer_fun
,rect_gp = gpar(col = "white", lwd = 2) #方格的分割
,cluster_rows = F
,column_names_side = 'bottom'
,column_names_rot = 0
,column_names_gp = gpar(
col='black'
,fontsize=15
)
,row_names_side = 'left' #行名在左
,row_names_centered = T #行名居中
,column_names_centered = T #列名居中
,row_names_gp = gpar(
col='black'
,fontsize=15
)
,border = T
,border_gp = gpar(
col='white'
,alpha=0.5
)
, width = unit(2, "cm") #热图主体
,height = unit(8, "cm")
#图例
,heatmap_legend_param = list(
at=round(seq(0,1,0.25),3)
,labels=c('0.00','0.25','0.50','0.75','1.00')
,title='Predicted\nSimilarity'
,title_gp=gpar(fontface=1,fontsize=12)
,legend_height=unit(6,'cm')
,gird_width=unit(2/3,'cm')
)
)
参考:
R 数据可视化 —— 聚类热图 ComplexHeatmap(四)图例 - 简书 (jianshu.com)
R 数据可视化 —— 聚类热图 ComplexHeatmap(一) - 简书 (jianshu.com)
网友评论