Академический Документы
Профессиональный Документы
Культура Документы
com/en/lessons/heatmap-in-r-static-and-interactive-visualization/#data-preparation
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Data preparation
We use mtcars data as a demo data set. We start by standardizing the data to make variables comparable:
df <- scale(mtcars)
Complex heatmap
ComplexHeatmap is an R/bioconductor package, developed by Zuguang Gu, which provides a flexible solution to
arrange and annotate multiple heatmaps. It allows also to visualize the association between different data from
different sources.
It can be installed as follow:
source("https://bioconductor.org/biocLite.R")
biocLite("ComplexHeatmap")
Simple heatmap
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
)
Additional arguments:
1. show_row_names, show_column_names: whether to show row and column names, respectively. Default value is
TRUE
2. show_row_hclust, show_column_hclust: logical value; whether to show row and column clusters. Default is TRUE
3. clustering_distance_rows, clustering_distance_columns: metric for clustering: “euclidean”, “maximum”, “manhattan”,
“canberra”, “binary”, “minkowski”, “pearson”, “spearman”, “kendall”)
4. clustering_method_rows, clustering_method_columns: clustering methods: “ward.D”, “ward.D2”, “single”,
“complete”, “average”, … (see ?hclust).
To specify a custom colors, you must use the the colorRamp2() function [circlize package], as follow:
library(circlize)
mycols <- colorRamp2(breaks = c(-2, 0, 2),
colors = c("green", "white", "red"))
Heatmap(df, name = "mtcars", col = mycols)
It’s also possible to use RColorBrewer color palettes:
library("circlize")
library("RColorBrewer")
Heatmap(df, name = "mtcars",
col = colorRamp2(c(-2, 0, 2), brewer.pal(n=3, name="RdBu")))
We can also customize the appearance of dendograms using the function color_branches() [dendextend package]:
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))
Splitting heatmap by rows
You can split the heatmap using either the k-means algorithm or a grouping variable.
It’s important to use the set.seed() function when performing k-means so that the results obtained can be
reproduced precisely at a later time.
To split the dendrogram using k-means, type this:
# Divide into 2 groups
set.seed(2)
Heatmap(df, name = "mtcars", k = 2)
To split by a grouping variable, use the argument split. In the following example we’ll use the levels of the factor
variable cyl [in mtcars data set] to split the heatmap by rows. Recall that the column cyl corresponds to the number
of cylinders.
# split by a vector specifying rowgroups
Heatmap(df, name = "mtcars", split = mtcars$cyl,
row_names_gp = gpar(fontsize = 7))
Note that, split can be also a data frame in which different combinations of levels split the rows of the heatmap.
Heatmap annotation
The HeatmapAnnotation class is used to define annotation on row or column. A simplified format is:
HeatmapAnnotation(df, name, col, show_legend)
df: a data.frame with column names
name: the name of the heatmap annotation
col: a list of colors which contains color mapping to columns in df
For the example below, we’ll transpose our data to have the observations in columns and the variables in rows.
df <- t(df)
Simple annotation
A vector, containing discrete or continuous values, is used to annotate rows or columns. We’ll use the qualitative
variables cyl (levels = “4”, “5” and “8”) and am (levels = “0” and “1”), and the continuous variable mpg to annotate
columns.
For each of these 3 variables, custom colors are defined as follow:
Complex annotation
In this section we’ll see how to combine heatmap and some basic graphs to show the data distribution. For simple
annotation graphics, the following functions can be
used: anno_points(), anno_barplot(), anno_boxplot(), anno_density() and anno_histogram().
An example is shown below:
# 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
You can use the option width = unit(3, “cm”)) to control the size of the heatmaps.
Note that when combining multiple heatmaps, the first heatmap is considered as the main heatmap. Some settings
of the remaining heatmaps are auto-adjusted according to the setting of the main heatmap. These include: removing
row clusters and titles, and adding splitting.
The draw() function can be used to customize the appearance of the final image:
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"))
Legends can be removed using the arguments show_heatmap_legend = FALSE, show_annotation_legend =
FALSE.