knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE, message = FALSE
)

Introduction

Heatmap is a popular visualization method for data with many variables. It is often used as a way to present data generated by high through-put analysis methods, such as RNA-seq, 16S-seq, and MS based proteomics and metabolomics. There are already several packages developed in the R community that is designated for heatmap. The package gplots provids a function, heatmap.2, that is able of generating basic heatmaps. The package pheatmap is another option that is more flexiable than heatmap.2. Besides that, there is a package called "heatmaply" that was built based on ggplot and plotly packages to generate interactive heatmaps. One advantage about the heatmaply package is using the seriation package to rotate the lobes of the dendrogram in order to let variables that are most similar to each other close to eacher. The zheatmap borrowed the idea of using the seriation package, and offers a heatmap system with only ggplot and grid.

Installation

The package is currently under developing and can only be installed from github

devtools::install_github("zhuchcn/zheatmap")

Basic usage

The zheatmap function can take many arguments. Most usful arguments will be explained here. To see a whole list of the arguments, please see the help document.

The mtcars data set will be used for demostration.

head(mtcars)
cars = mtcars[,-2]

The zheatmap function must take a data.frame object as a input, and project the data into the heatmap as the same oriention. That means the columns in the input data are columns in heatmap, and rows are also rows. By default, a dendrogram will be drawn on both the top and right of the heatmap. THe dendrogram will draw samples similar to each other be next to each other.

library(zheatmap)
zheatmap(cars)

Your can remove the dendrogram by setting the Rowv and Colv to FALSE. By doing that, the order of columns and rows will be exactly the same as the input data.

zheatmap(cars, Rowv = FALSE, Colv = FALSE)

The data is scaled after passed to zheatmap and before the values were plotted as color. The reason for doing that is to make the color range more even. We can turn off the scaling by setting the scale = FALSA . As show below, because disp and hp are not in the same scale as the rest variables, the heatmap becomes un-informative.

zheatmap(cars, Rowv = FALSE, Colv = FALSE, scale = "none")

By default the scale is set to "column" which means every column is scaled into a z-score. So after transformation, the mean of each column is 0 and the standard deviation is 1. However sometimes the transformation needs to be done in a row-wise mannar and that can be done by setting the scale = "row". And we can show the x axis texts and rotate them 90 degree.

zheatmap(t(cars), scale = "row", xtext = TRUE, xtext.angle = 90)

Besides the default z-score scaling method, the absolute scaling method is also provided as an option which will force the range of each variable into -1 and 1.

zheatmap(cars, scale.fun = "absolute_scale")

As stated previously, the seriate function from the seration package is used to rotate lobes of the dendrogram. The default seriate method is "OLO". Different seriate method may be used according to the data.

zheatmap(cars, seriate = "GW")

If HC is specified, it will use hierachical clustering.

zheatmap(cars, seriate = "HC")

The color can also be changed. The RdBu color panel from the RColorBrew package might be one of the most popular color scheme for heatmaps.

library(RColorBrewer)
zheatmap(cars, colors = rev(brewer.pal(11, "RdBu")))

Some times we want to add some annotations to either the column side or row side of the heatmap. This can be done using the colSideBar and rowSideBar arguments

cyl = factor(mtcars$cyl)
zheatmap(cars, rowSideBar = cyl)
zheatmap(t(cars), scale = "row", colSideBar = cyl)


zhuchcn/zheatmap documentation built on July 18, 2019, 3:14 p.m.