美文网首页ggplot集锦
相似性热图复现

相似性热图复现

作者: monkey_study | 来源:发表于2022-12-01 16:19 被阅读0次

原图

image.png

复现结果

image.png

复现代码

  1. 载入R包
library(ComplexHeatmap)
library(ggplot2)
library(dplyr)
library(circlize)
  1. 构造数据
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)

相关文章

网友评论

    本文标题:相似性热图复现

    本文链接:https://www.haomeiwen.com/subject/ufoefdtx.html