如何使用 ComplexHeatmap 在 R 中制作热图?
热图是一种矩阵形式的可视化技术。它主要用于分析数据集中的数字特征并可视化数据的趋势。这种技术可以使用颜色强度来突出值的范围,即它用更亮的颜色指示更高的值,并且颜色对于较小的值逐渐淡出。
在本文中,我们将了解如何使用 R 编程语言中的ComplexHeatmap Package 的 Heatmap ()函数绘制热图。
安装
要使用 ComplexHeatmap 创建热图,必须首先将其安装并加载到工作空间中。
句法:
# installing the package
install.packages("ComplexHeatmap")
library(ComplexHeatmap)
热图
要使用此模块创建热图,请使用带有适当参数的简单 heatmap()函数。
Syntax:
Heatmap(m, col = c(), cluster_columns = FALSE, cluster_rows = FALSE, rect_gp = gpar() … )
Parameters:
- m : matrix or vector
- cluster_columns : specify clusters for columns
- cluster_rows : specify clusters for rows
- rect_gp : to add gap spaces to heatmap
示例:简单热图
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
Heatmap(m)
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with custom colors
Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with col clusters
Heatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with row clusters
Heatmap(m,cluster_columns = FALSE,
rect_gp = gpar(col = "white", lwd = 2),
col = rev(rainbow(10)))
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with no clusters
Heatmap(m,cluster_rows = FALSE,cluster_columns = FALSE)
R
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap 1
h1<-Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))
# heatmap 2
h2<-Heatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))
# add to combine 2 heatmaps
h1+h2
输出:
自定义热图的颜色
要更改热图的颜色,col 参数与要更改颜色的值一起使用。
示例:更改热图的颜色
电阻
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with custom colors
Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))
输出:
带有列簇的热图
我们将 cluster_rows 指定为 FALSE 以删除行集群。
示例:删除行集群
电阻
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with col clusters
Heatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))
输出:
带有行簇的热图
我们将 cluster_columns 指定为 FALSE 以删除列簇。
示例:具有行集群的热图
电阻
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with row clusters
Heatmap(m,cluster_columns = FALSE,
rect_gp = gpar(col = "white", lwd = 2),
col = rev(rainbow(10)))
输出:
没有集群的热图
让我们可视化一个没有集群的热图。为此,cluster_rows 和cluster_columns 设置为FALSE。
示例:没有集群的热图
电阻
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap with no clusters
Heatmap(m,cluster_rows = FALSE,cluster_columns = FALSE)
输出:
将两个热图组合在一起
让我们添加 2 个热图并将它们一起可视化。因为这个图是独立创建的,然后连接在一起。
示例:将两个热图组合在一起
电阻
library(ComplexHeatmap)
# code to create a simple heatmap
set.seed(2020)
# generate random numbers
d1 <- runif(500, 0, 1)
m<-matrix(d1,nrow = 20,ncol=10)
# randomize the data points
m<-m[,sample(ncol(m))]
# heatmap 1
h1<-Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))
# heatmap 2
h2<-Heatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))
# add to combine 2 heatmaps
h1+h2
输出: