ComplexHeatmap|绘制单个热图-I

简介: ComplexHeatmap|绘制单个热图-I


ComplexHeatmap可以绘制很复杂的热图,能满足日常以及文章所需,本次先简单的介绍单个热图绘制的内容。


单个热图由热图主体和热图组件组成。其中主体可分为行和列;组件可以是标题、树状图、矩阵名称和热图注释,在主图的四周均可,且顺序可调整。

一 载入数据,R包


1.1 载入ComplexHeatmap包


#if (!requireNamespace("BiocManager", quietly = TRUE))
#    install.packages("BiocManager")
#BiocManager::install("ComplexHeatmap")
library(ComplexHeatmap)

1.2 载入数据

为更贴近生信使用场景,直接使用内置的基因表达数据


expr = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/gene_expression.rds"))
#查看数据
str(expr)
expr[1:4,c(1:4,25:27)]

image.png

expr[1:4,c(1:4,25:27)

s1_ce1101s2_ce1102s3_ce1103s4_ce11011ength

chr

S4

type

10.84344124882protein_coding

11.11631512.65986411.380503

gene1

chr1

10.21785

51244

10.9470698.56027111.370300

chr1

protein_coding

gene2

10.48915

39318

9.4523918.0375358,938325

chr1

gene3

protein-coding

13.94239

codingchr1

45043

protei

gene412.55503511.83230811.475035

拿到一个新数据后,除了检查[1:4,1:4]外,也许还需要看看最后几列,另外还需要观察列名称的规律。


去除最后几列,或者只选取列名字包含cell的(TCGA数据处理中也会经常遇到)



mat = as.matrix(expr[, grep("cell", colnames(expr))])

1.3 绘制最简单的热图



Heatmap(mat)

image.png

mmmwimmmm

AOMDEAASOMOAMa

1:0114444540000144404450006

matrix

o

中一师

TMMMMN8Z电9014014409

Zolle0s

Zol0s

ZOlle28s

s0lla0ms

Zolla9S

c0ll9吃S

cOl

ZolLaocs

collaocs

Lols6Ls

Lolieos

collesbis

s0l/0gis

c0l0cs

colLa9zis

c0l109s

S0IL9S

Loljaacs

c0l98s

Lolaos

Loll0s

m

Loleas

0

可以看到有很多需要“美化”的地方,别急,一点点来。

二 热图修饰

2.1 颜色

1)连续型变量

可以使用circle::colorRamp2()函数来生成Heatmap()中的颜色映射函数,输入参数为分割位置以及分割点上的颜色。下例中,大于12的值都映射为红色,小于12的值映射为绿色;


library(circlize)
#c中的范围要根据实际情况设置
col_fun = colorRamp2(c(8, 12, 16), c("green", "white", "red"))
Heatmap(mat, name = "mat", col = col_fun)

image.png

EW型4

ma

业9

可4441410020000900901004049

1T0a4cnn

colleas

collaouis

c0llaocls

ZOII25Zs

吕易

Lollaag1s

coleo8is

Lolasels

Zol0忆s

E0I09S

c010日s

COlla18S

Zoleas

c01306S

Lolo6Ls

colla0s

Lolos

cola0os

LoL202s

LolooLs

olscs

LOLea-Zs

s0l09ls

2)分类型变量

更改分类变量的颜色,需要把所有分类的数字均进行赋值。

discrete_mat = matrix(sample(1:4, 100, replace = TRUE), 10, 10)
colors = structure(1:4, names = c("1", "2", "3", "4")) # black, red, green, blue
Heatmap(discrete_mat, name = "mat", col = colors,
    column_title = "a discrete numeric matrix")

image.png

adiscretenumericmatrix

mat

CNM

更多颜色修改请参考官方文档,文末的参考资料的链接。


2.2 标题

1)设置行,列和图例的标题


Heatmap(mat, 
        name = "legend title", #图例title
        column_title = "I am a column title", #列title 
        row_title = "I am a row title",
        column_title_side = "bottom") #行title


image.png

Iamacolumntitle

PEcaocNcmOmomoo8

派MM

CWQHNaeo-OOOO

可出IMONEWB

legendtitle

2OCeALa-N

JUMAWDAOIOET

warogopao.

ZOll5s

c0l0s

ZOLIs

coll0US

s0ll0ts

c0l08ls

colleo9s

ZOIIOs

colos

c0lOcs

吕言易

col0吃s

ZOIOZ

c0lsgLs

c0llozLs

c0113156s

LOLIss

LOIIJ61s

Loloes

LOllass

LOL0OLS

LoLLOZ

Lol2

Lolao9s

2)设置标题的位置,颜色,字体,大小


Heatmap(mat, name = "mat", 
        row_title = "row title",
        row_title_rot = 0, #旋转方向
        column_title = "I am a big column title", 
        column_title_side = "bottom", #标题位置
        column_title_gp = gpar(fontsize = 20, fontface = "bold",col = "red")) #颜色,字体,大小


image.png

Htowwgr

TNN

rowtitle

5器SSIAS

y8

8

0

WOO

出:d时

ZOL

ZolL9

colL20Z1

zolaoos

COleac

zole0

c0ll0s

OLomis

col30

col08LS

Zole

s0l159s

Lol396s

s0IL30

Olis

s0l06s

s0l/0cs

LOLI29

LOl591s

LOl501s

Lolje0s

Lolos

LOLois

LOL0s

Iamabigcolumntitle


3)设置标题的背景

column_title_gp中的填充参数来设置标题的背景颜色


Heatmap(mat, name = "mat", 
        column_title = "I am a column title", 
        column_title_gp = gpar(fill = "red", col = "white", border = "blue"),
        )

image.png

Iamacolumntitle

SBW品SSOONRE

mat

16

免业豆

W

LoIL06s

c0lOS

Z0lla99s

ZOIIOS

ZoIIS6Ls

colacs

ZOlZS

ZOlLS

c0LI96S

s0l1a0cs

s0ll309s

collaogls

LOlI296LS

collascis

colsSs

Lollsgs

m

S0l/07s

LOLL5

Loloos

Lolos

Loliocis

Lolos

col0os


2.3 聚类

聚类是热图可视化的关键组成部分,在ComplexHeatmap包中可以非常灵活的进行设置。


A:一般设置

cluster_rows/columns :是否进行聚类

show_column/row_dend :是否显示聚类树

column/row_dend_side :聚类图绘制的位置

column_dend_height/row_dend_widht聚类树的高度 和 宽度



Heatmap(mat, name = "mat",
        cluster_columns = T,  
        cluster_rows = F, ## turn off row clustering
        show_column_dend = T, ## hide column dendrogram
        show_row_dend = F,
        column_dend_side = "top",  #dendrogram location
        column_dend_height = unit(4, "cm"))


注意:聚类树的高度 和 宽度有区别。

B:距离方法

可选计算距离的方式包括pearson, spearman以及kendall , 或者计算距离的自定义函数。


Heatmap(mat, name = "mat", clustering_distance_rows = "pearson",
    column_title = "pre-defined distance method (1 - pearson)")


2)自定义


Heatmap(mat, name = "mat", clustering_distance_rows = function(x, y) 1 - cor(x, y),
    column_title = "a function that calculates pairwise distance")

C:聚类方法

支持hclust()中的聚类方法。



Heatmap(mat, name = "mat", clustering_method_rows = "single")
D:聚类树的渲染

根据聚类结果将聚类树的枝设置不同的颜色


library(dendextend)
row_dend = as.dendrogram(hclust(dist(mat)))
row_dend = color_branches(row_dend, k = 4) # `color_branches()` returns a dendrogram object
Heatmap(mat, name = "mat", 
        cluster_rows = row_dend,
        row_dend_width  = unit(4, "cm"))

2.4 设置行列顺序

通过row_order/column_order函数自定义其排序,为方便展示选择前30个基因。


mat <- mat[1:30,]
Heatmap(mat, name = "mat", 
          row_order = order(as.numeric(gsub("gene", "", rownames(mat)))), #将gene1替换为1,在排序
          column_order = sort(colnames(mat)),
          column_title = "reorder matrix")


注:此处将gene1,gene10 先替换掉gene(不去的话是按照ASCII码),然后按照数值排序。

相关文章
|
7月前
|
C++
OpenCV-绘制简易直方图DrawHistImg
OpenCV-绘制简易直方图DrawHistImg
|
数据可视化 搜索推荐 数据挖掘
使用ComplexHeatmap绘制复杂热图|Note2:单个热图(万字超详细教程)(中)
使用ComplexHeatmap绘制复杂热图|Note2:单个热图(万字超详细教程)(中)
513 0
使用ComplexHeatmap绘制复杂热图|Note2:单个热图(万字超详细教程)(中)
|
12天前
|
计算机视觉 索引
opencv直方图绘制详解
opencv直方图绘制详解
|
4月前
|
数据可视化 Python
使用pygal库绘制直方图、XY线图和饼状图的技术指南
使用pygal库绘制直方图、XY线图和饼状图的技术指南
20 0
|
11月前
|
人工智能 数据可视化
跟SCI学umap图| ggplot2 绘制umap图,坐标位置 ,颜色 ,大小还不是你说了算
跟SCI学umap图| ggplot2 绘制umap图,坐标位置 ,颜色 ,大小还不是你说了算
745 1
|
11月前
|
数据挖掘
ggplot2|从0开始绘制直方图
ggplot2|从0开始绘制直方图
168 0
|
11月前
|
数据挖掘
ggplot2| 绘制KEGG气泡图
ggplot2| 绘制KEGG气泡图
223 0
|
11月前
|
算法
ggplot2|从0开始绘制PCA图
ggplot2|从0开始绘制PCA图
308 0
|
11月前
|
数据挖掘
ggplot2|从0开始绘制箱线图
ggplot2|从0开始绘制箱线图
|
11月前
|
数据挖掘 数据格式
R语言- ComplexHeatmap 绘制复杂热图示例
ComplexHeatmap是R语言中用于绘制复杂热图的一个重要包。它提供了一种灵活、高效、易于定制的方法来绘制热图,并支持多种数据类型和数据格式,支持包括多种热图类型,包括基本热图、聚类热图、分组热图、矩阵热图等。用户可以根据自己的需求选择不同的热图类型,并进行灵活的定制。在生物信息学、医学、生态学等领域得到广泛应用。 本文将通过一个复杂热图的创建示例分享 ComplexHeatmap的语法规则。
553 0