ggplot集锦

热图复现-2022.12.3

2022-12-02  本文已影响0人  monkey_study

原图

image.png

复现结果

image.png

代码

#----------------------Ro/e热图-------------------------
#全局设定
options(stringsAsFactors = F)
rm(list = ls())

###载入所需R包
library(dplyr)
library(ComplexHeatmap)
library(tidyverse)
library(readxl)
library(tibble)

# 载入数据
dat=read_xlsx('2022.12.1.xlsx'
              ,sheet = 3)
mat=dat%>%column_to_rownames('Cell_type')%>%
  as.matrix()
#--------画图
# 设置颜色
col_fun = circlize::colorRamp2(c(0,2), c("#F3F3A9","#A0201E"))
#添加文字
mat1=mat
mat1[is.na(mat1)]='-'
cell_fun <- function(j, i, x, y, width, height, fill) {
  grid.text(
    mat1[i, j], 
    x, y,
    gp = gpar(
      fontsize = 6
    ))
}

pdf('D:/Rstudio_data/codes/画图/热图/Ro_e富集热图/Roe_plot.pdf'
    ,width =unit(4,'cm')
    ,height = unit(6,'cm') )
Heatmap(mat
        ,col=col_fun #自定义颜色
        ,cell_fun = cell_fun #添加文字
        ,width = unit(4, "cm") #热图全局大小调整
        ,height = unit(6,'cm')
        ,name = 'Ro/e' #图例名称修改
        ,heatmap_legend_param = list(
          at=seq(0,2,0.5) #break值
          ,labels=c('','0.5','1.0','1.5','2.0') #break对应的标签
          ,labels_gp=gpar(fontsize=6) #图例标签调整
          ,legend_height=unit(2.5,'cm') #图例高度
          ,grid_width=unit(0.3,'cm') #图例宽度
          ,title_gp=gpar(fontsize=8) #图例title参数调整
          
        )
        #列调整
        ,column_names_rot = 45
        ,column_names_gp = gpar(fontsize=8)
        #行调整
        ,row_names_gp = gpar(fontsize=8)
        ,row_names_side = 'left'
        #关闭聚类
        ,cluster_rows = F
        ,cluster_columns = F
  
)
dev.off()

重点: 对图例各参数的调整

image.png
,其中需要注意的是,当图例是连续变量时,修改宽度用grid_width=unit(0.3,'cm'),修改高度用legend_height=unit(2.5,'cm')。!!!
以及 cell_fun添加热图文本的应用

参考:R 数据可视化 —— 聚类热图 ComplexHeatmap(四)图例 - 简书 (jianshu.com)
🎨[可视化|R包]ComplexHeatmap学习笔记⑤Heatmap and Annotation Legends - 简书 (jianshu.com)
Legend: Make a Single Legend in ComplexHeatmap: Make Complex Heatmaps (rdrr.io)

上一篇 下一篇

猜你喜欢

热点阅读