今天跟着我把热图学个遍,囊括所有需求

用于绘制交互式和静态热图的R包和功能很多,包括:

  • heatmap()[R基本函数,统计数据包]:绘制一个简单的热图

  • heatmap.2()[ gplots R包]:与R base函数相比,绘制了增强的热图。

  • pheatmap()[ pheatmap R包]:绘制漂亮的热图,并提供更多控件来更改热图的外观。

  • d3heatmap()[ d3heatmap R包]:绘制交互式/可单击的热图

  • ComplexHeatmap R / Bioconductor的包]:绘制,注释和排列复杂热图(用于基因组数据分析是非常有用的)

在这里,我们从描述绘制热图的5 R函数开始。接下来,我们将重点介绍ComplexHeatmap程序包,该程序包提供了一种灵活的解决方案来安排和注释多个热图。它也可以可视化来自不同来源的不同数据之间的关联

我们使用mtcars数据作为演示数据集。我们首先将数据标准化以使变量具有可比性:

df <- scale(mtcars)# Default plot
heatmap(df, scale = "none")

如何指定调色板

col<- colorRampPalette(c("red", "white", "blue"))(256)

heatmap(df, scale = "none", col = col)

library("RColorBrewer")
col <- colorRampPalette(brewer.pal(10, "RdYlBu"))(256)

heatmap(df, scale = "none", col = col)

此外,可以使用参数RowSideColors和ColSideColors分别注释行和列。

例如,在下面的R代码中,将自定义热图,如下所示:

  • RColorBrewer调色板名称用于更改外观

  • 参数RowSideColors和ColSideColors分别用于注释行和列。按照行列顺序指指定颜色

heatmap是按照聚类来重排行列的,会打乱颜色。

# Use RColorBrewer color palette names
library("RColorBrewer")
col <- colorRampPalette(brewer.pal(10, "RdYlBu"))(256)
heatmap(df, scale = "none", col = col,
RowSideColors = rep(c("blue", "pink"), each = 16),
ColSideColors = c(rep("purple", 5), rep("orange", 6)))

# install.packages("gplots")
library("gplots")
heatmap.2(df, scale = "none", col = bluered(100),
trace = "none", density.info = "none")

pheatmap

这个功能十分强大,出图我们也经常选用,cutree_rows参数用于按照聚类结果进行切割。

library("pheatmap")
pheatmap(df, cutree_rows = 4)

# install.packages("d3heatmap")
library("d3heatmap")
d3heatmap(scale(mtcars), colors = "RdYlBu",
k_row = 4, # Number of groups in rows
k_col = 2 # Number of groups in columns
)

pheatmap不能使用。

ComplexHeatmap 功能十分强大

  • row_names_gp:设置标签自字体大小

  • olumn_title:设置行,列名称标签。

library(ComplexHeatmap)
Heatmap(df,
name = "mtcars", #title of legend
column_title = "Variables", row_title = "Samples",
row_names_gp = gpar(fontsize = 7) # Text size for row names
)

要指定自定义颜色,必须使用colorRamp2()函数[ circlize package],如下所示:

library(circlize)
mycols <- colorRamp2(breaks = c(-2, 0, 2),
colors = c("green", "white", "red"))
Heatmap(df, name = "mtcars", col = mycols)

更换调色板,使用RColorBrewer 中的颜色。

library("circlize")
library("RColorBrewer")
Heatmap(df, name = "mtcars",
col = colorRamp2(c(-2, 0, 2), brewer.pal(n=3, name="RdBu")))

dendextend 同样给复杂热图聚类上色。

library(dendextend)
row_dend = hclust(dist(df)) # row clustering
col_dend = hclust(dist(t(df))) # column clustering
Heatmap(df, name = "mtcars",
row_names_gp = gpar(fontsize = 6.5),
cluster_rows = color_branches(row_dend, k = 4),
cluster_columns = color_branches(col_dend, k = 2))

复杂热图按照聚类切割分块

km/row_km:对列进行聚类拆分

# Divide into 2 groups
set.seed(2)
Heatmap(df, name = "mtcars", km = 4)

按照行进行分割聚类:column_km

# Divide into 2 groups
set.seed(2)
Heatmap(df, name = "mtcars", column_km = 4)

指定行列分隔

split :参数用于指定行的分组,用于分割热图。row_split一样

如果要对行进行聚类,使用column_split,用法一样。

# Split
# data.frame(cyl = rep(1:4,dim(mtcars)[2]))
Heatmap(df, name ="mtcars",
split = data.frame(cyl = mtcars$cyl),
column_split = data.frame(cyl = rep(1:2,dim(mtcars)[2])[1:11]),
row_names_gp = gpar(fontsize = 7))

多重分隔

Heatmap(df, name ="mtcars", col = col,
km = 4, split = mtcars$cyl)

library("cluster")
set.seed(2)
pa = pam(df, k = 3)
Heatmap(df, name = "mtcars", col = col,
split = paste0("pam", pa$clustering))

复杂热图为什么复杂

复杂热图最强大的莫过于对于热图的注释

df <- t(df)# Define some graphics to display the distribution of columns
.hist = anno_histogram(df, gp = gpar(fill = "lightblue"))
.density = anno_density(df, type = "line", gp = gpar(col = "blue"))
ha_mix_top = HeatmapAnnotation(hist = .hist, density = .density)
# Define some graphics to display the distribution of rows
.violin = anno_density(df, type = "violin",
gp = gpar(fill = "lightblue"), which = "row")
.boxplot = anno_boxplot(df, which = "row")
ha_mix_right = HeatmapAnnotation(violin = .violin, bxplt = .boxplot,
which = "row", width = unit(4, "cm"))
# Combine annotation with heatmap
Heatmap(df, name = "mtcars",
column_names_gp = gpar(fontsize = 8),
top_annotation = ha_mix_top) + ha_mix_right

复杂热图的组合

# Heatmap 1
ht1 = Heatmap(df, name = "ht1", km = 2,
column_names_gp = gpar(fontsize = 9))
# Heatmap 2
ht2 = Heatmap(df, name = "ht2",
col = circlize::colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
column_names_gp = gpar(fontsize = 9))
# Combine the two heatmaps
ht1 + ht2

细布控制组合

draw(ht1 + ht2,
row_title = "Two heatmaps, row title",
row_title_gp = gpar(col = "red"),
column_title = "Two heatmaps, column title",
column_title_side = "bottom",
# Gap between heatmaps
gap = unit(0.5, "cm"))

expr <- readRDS(paste0(system.file(package = "ComplexHeatmap"),
"/extdata/gene_expression.rds"))
mat <- as.matrix(expr[, grep("cell", colnames(expr))])
type <- gsub("s\\d+_", "", colnames(mat))
ha = HeatmapAnnotation(df = data.frame(type = type))

ha## A HeatmapAnnotation object with 1 annotation
## name: heatmap_annotation_2
## position: column
## items: 24
## width: 1npc
## height: 5mm
## this object is subsetable
## 9.001mm extension on the right
##
## name annotation_type color_mapping height
## type discrete vector random 5mm
Heatmap(mat, name = "expression", km = 5, top_annotation = ha,
# top_annotation_height = unit(4, "mm"),
show_row_names = FALSE, show_column_names = FALSE) +
Heatmap(expr$length, name = "length", width = unit(5, "mm"),
col = circlize::colorRamp2(c(0, 100000), c("white", "orange"))) +
Heatmap(expr$type, name = "type", width = unit(5, "mm")) +
Heatmap(expr$chr, name = "chr", width = unit(5, "mm"),
col = circlize::rand_color(length(unique(expr$chr))))

ha = HeatmapAnnotation(df = data.frame(type = type))

ha## A HeatmapAnnotation object with 1 annotation
## name: heatmap_annotation_3
## position: column
## items: 24
## width: 1npc
## height: 5mm
## this object is subsetable
## 9.001mm extension on the right
##
## name annotation_type color_mapping height
## type discrete vector random 5mm
Heatmap(mat, name = "expression", km = 5, top_annotation = ha,
# top_annotation_height = unit(4, "mm"),
show_row_names = FALSE, show_column_names = FALSE) +
Heatmap(expr$type, name = "type", width = unit(5, "mm")) +
Heatmap(expr$type, name = "type", width = unit(5, "mm"))

如何添加多个行,或者列

# Annotation data frame

annot_df <- data.frame(cyl = mtcars$cyl, am = mtcars$am,
mpg = mtcars$mpg)
# row.names(annot_df) = row.names(mtcars)
# Define colors for each levels of qualitative variables
# Define gradient color for continuous variable (mpg)
col = list(cyl = c("4" = "green", "6" = "gray", "8" = "darkred"),
am = c("0" = "yellow", "1" = "orange"),
mpg = circlize::colorRamp2(c(17, 25),
c("lightblue", "purple")) )
# Create the heatmap annotation
ha <- HeatmapAnnotation(df = data.frame(cyl = mtcars$cyl, am = mtcars$am,
mpg = mtcars$mpg), col = col)

# Combine the heatmap and the annotation
# df = t(df)
Heatmap(df, name = "mtcars",
top_annotation = ha)

# Annotation data frame

annot_df <- data.frame(cyl = mtcars$cyl, am = mtcars$am,
mpg = mtcars$mpg)
# row.names(annot_df) = row.names(mtcars)
# Define colors for each levels of qualitative variables
# Define gradient color for continuous variable (mpg)
col = list(cyl = c("4" = "green", "6" = "gray", "8" = "darkred"),
am = c("0" = "yellow", "1" = "orange"),
mpg = circlize::colorRamp2(c(17, 25),
c("lightblue", "purple")) )
# Create the heatmap annotation
ha <- HeatmapAnnotation(df = data.frame(cyl = mtcars$cyl, am = mtcars$am,
mpg = mtcars$mpg), col = col)

# Combine the heatmap and the annotation
df = t(df)
Heatmap(df, name = "mtcars") +
Heatmap(mtcars$mpg, name = "type", width = unit(5, "mm")) +
Heatmap(mtcars$mpg, name = "type", width = unit(5, "mm"))

Heatmap(df, name = "mtcars") +
Heatmap(annot_df , name = "type", width = unit(5, "mm"))

densityHeatmap(scale(mtcars))

reference

https://www.datanovia.com/en/lessons/heatmap-in-r-static-and-interactive-visualization/

(0)

相关推荐

  • Python数据可视化库seaborn的使用总结

    seaborn是python中的一个非常强大的数据可视化库,它集成了matplotlib,下图为seaborn的官网,如果遇到疑惑的地方可以到官网查看.http://seaborn.pydata.or ...

  • R包基础实操—tidyverse包

    核心软件包是ggplot2.dplyr.tidyr.readr.purrr.tibble.stringr和forcats,它们提供了建模.转换和可视化数据的功能. 其中,readr包用于读取数据,ti ...

  • R学习:R for Data Science 循环-迭代 purrr 函数代替 for 循环

    R学习往期回顾: R学习:R for Data Science 循环-迭代(for while)) R学习:R for Data Science 向量(1) R学习:R for Data Scienc ...

  • ComplexHeatmap|绘制单个热图-I

    ComplexHeatmap可以绘制很复杂的热图,能满足日常以及文章所需,本次先简单的介绍单个热图绘制的内容. 单个热图由热图主体和热图组件组成.其中主体可分为行和列:组件可以是标题.树状图.矩阵名称 ...

  • Python-相关性分析

    数据之间,数据和结局指标之间的相关性用python可以直观展示. 加载数据和模块 import pandas as pdimport numpy as npimport warnings%matplo ...

  • 单细胞RNA-seq揭示TNBC的异质性(图表复现04)

    前面的单细胞RNA-seq揭示TNBC的异质性(图表复现03)教程里面我们一起复现了文章" Unravelling subclonal heterogeneity and aggressiv ...

  • NC单细胞文章复现(七):Gene expression signatures(2)

    我们今天继续探索这3个gene signatures,首先看它在不同clusters的细胞之间的表达分布. clust_avg_prognosis <- matrix(NA, nrow = le ...

  • 技术贴 | R语言:大样本多组学的相关性计算、热图绘制

    本文由可爱的乔巴根据实践经验而整理,希望对大家有帮助. 原创微文,欢迎转发转载. 导读 上期介绍了利用psych包计算两组小样本的相关性并进行热图可视化,但当样本数据量非常大时,psych包会耗费的时 ...

  • 技术贴 | R语言:小样本多组学的相关性计算、热图绘制

    本文由可爱的乔巴根据实践经验而整理,希望对大家有帮助. 原创微文,欢迎转发转载. 导读 在多组学数据关联挖掘中,在我们筛选到目标基因集以及目标蛋白质集合或目标代谢物集合后,在进行基因与蛋白质或代谢关联 ...

  • 【看图学拍】还没学会星轨拍摄?抓紧学起来!

    后期强摄影学苑 一堂好课,一听就懂,一学就会,一生受益. 50篇原创内容 公众号 一般数码单反相机提供的最慢快门速度大约在30秒左右,这对于大部分的拍摄创作来说都已经绰绰有余了.但是当需要在弱光环境中 ...

  • 【看图学拍】拍摄倒影原来也挺有讲究

    后期强摄影学苑 一堂好课,一听就懂,一学就会,一生受益. 50篇原创内容 公众号 「天雨方知洼」,在突如其来的大雨过后,有心的你一定会发现平时走过的平坦大路上还有这么多坑坑洼洼,冷不防成为了湿透鞋袜的 ...

  • #看导图学法律# No.3 遗产继承中的...

    #看导图学法律# No.3 遗产继承中的遗嘱执行人与遗产管理人 关键词:民法典 遗产 继承 遗嘱执行人 遗产管理人#无讼学院# 想要高清导图朋友们,可私信小编"领取导图"

  • 这四道素菜,学一遍就会,非常好吃有营养

    五行养生菜 材料:素高汤少许.铁棍山药半根.芦笋3根.胡萝卜半根.木耳1小把.红椒少许. 盐少许 做法: 1.山药削皮斜切薄片,芦笋切断.胡萝卜刻花.红椒切丝: 2.锅内倒入少许油,放入山药爆炒,再放 ...

  • 看图学管桃学化控!

    桃树喷施多效唑,是为了控制新梢旺长.在实际生产中,很多果农使用多效唑很盲目,很少有人能够根据枝条的长势灵活掌握使用时机和浓度,有的控制不住,有的控制过度. (1)新梢封顶不化控. 只有老叶片,完全没有 ...

  • 看图学和田玉术语 |(二十二) 和田玉光泽度

    上一期通过图文方式为您介绍了和田玉(白玉)质地细腻度的等级划分,细腻度对于和田玉有着重要的影响,相信大家一定有了不少收获.今天小编将继续为您介绍和田玉光泽度的几个等级划分,希望通过本期介绍对您今后收藏 ...