Pheatmap Draws Pretty Heatmaps. A tutorial of how to generate pretty…

What is a heatmap?

The heatmap is a way of representing the data in a 2-dimensional form. The data values are represented as colors in the graph. The goal of the heatmap is to provide a colored visual summary of information.

R base heatmap: heatmap()

The built-in R heatmap() function [in stats package] can be used.

A simplified format is:

  • x: a numeric matrix
  • scale: a character indicating if the values should be centered and scaled in either the row direction or the column direction, or none. Allowed values are in c(“row”, “column”, “none”). Default is “row”.



In the plot above, high values are in red and lo

In the plot above, high values are in red and low values are in yellow.

It’s possible to specify a color palette using the argument col, which can be defined as follow:

  • Using custom colors:
  • Or, using RColorBrewer color palette:

Additionally, you can use the argument RowSideColors and ColSideColors to annotate rows and columns, respectively.

For example, in the the R code below will customize the heatmap as follow:

  1. An RColorBrewer color palette name is used to change the appearance
  2. The argument RowSideColors and ColSideColors are used to annotate rows and columns respectively. The expected values for these options are a vector containing color names specifying the classes for rows/columns.

Video

Access components

Each components of the heatmap/heatmap list has a name. You can go to any viewport by seekViewport(). Following figure almost contains all types of components.

The components that have names are:

The components that have names are:

  • global: the viewport which contains the whole figure.
  • global_column_title: the viewport which contains column title for the heatmap list.
  • global_row_title: the viewport which contains row title for the heatmap list.
  • main_heatmap_list: the viewport which contains a list of heatmaps and row annotations.
  • heatmap_@{heatmap_name}: the viewport which contains a single heatmap
  • annotation_@{annotation_name}: the viewport which contains an annotation either on columns or rows.
  • @{heatmap_name}_heatmap_body_@{i}: the heatmap body.
  • @{heatmap_name}_column_title: column title for a single heatmap.
  • @{heatmap_name}_row_title_@{i}: since a heatmap body may be splitted into several parts. @{i} is the index of the row slice.
  • @{heatmap_name}_hclust_row_@{i}: dendrogram for ith row slice.
  • @{heatmap_name}_hclust_column: dendrogram on columns
  • @{heatmap_name}_row_names_@{i}: the viewport which contains row names.
  • @{heatmap_name}_column_names: the viewport which contains column names.
  • heatmap_legend: the viewport which contains all heatmap legends.
  • legend_@{heatmap_name}: the viewport which contains a single heatmap legend.
  • annotation_legend: the viewport which contains all annotation legends.
  • legend_@{annotation_name}: the viewport which contains a single annotation legend.

Add annotations to rows and columns

The annotation function is one of the most powerful features of pheatmap. Specifically, you can input an independent data frame with annotations to the rows or columns of the heatmap matrix.

For example, I annotated each player with their position, made it a data frame object and input it to the pheatmap function. One thing to note, the row names of the annotation data frame have to match the row names or column names of the heatmap matrix depending on your annotation target.

heatmap by Yufeng

You can see from the heatmap that there is another column of colors that indicate the position of the players.

We see the players are not clustered by their positions, which suggests the relationship between the players’ positions and their playing types are becoming vague with the evolution of basketball.

Also, we can add the column annotation as well. I named the stats with their categories that include Offence, Defence, and others.

Then, I plot the heatmap with column annotation only. This time I only turn on the column clustering.

heatmap by Yufeng

We can see from the heatmap that the offense-related stats tend to be clustered together.

Retroactive heatmaps for your mobile app

Generate a heatmap for important fragments of your mobile app in a matter of minutes! Segment your heatmaps based on date and see how users interact with every fragment in your app. We automatically organize all heatmaps into galleries so you can see and access everything in one place.

Read more

Example 3: Create Heatmap with plot_ly Function [plotly Package]

Another popular package for heatmaps is the plotly package:

The plotly package contains the plot_ly function, which can be used to draw a heatmap by specifying type = “heatmap”:

Figure 6: Default Heatmap in plotly Package.

Figure 6: Default Heatmap in plotly Package.

Again, the patter is the same, but the general plot style is different.

The plotly package also provides additional options for the modification of the heatmap. If we want to change the color, we can either specify a color range manually, or we can use some predefined options such as colorscale = “Greys”:

Figure 7: Heatmap with Manual Color Range in plotl

Figure 7: Heatmap with Manual Color Range in plotly Package.

As you can see based on Figure 7, the Greys specification created a heatmap in greyscale.

Note that the plotly package show its graphics in the RStudio viewer instead of the RStudio plot window. For that reason you need to export these plots differently.

Also note that there are many other packages for the creation of heatmaps in R available. In my opinion, however, Base R, ggplot2, and plotly provide the best solutions.

Conclusion

Statisticians and analysts employ a plethora of tools and methods to sort the collected data and present them in a more user-friendly manner. To this end, heatmaps help professionals from every industry. To sum up, the reason why heatmaps have gained the impetus they have in the past few decades as a statistical and analytical tool is that:

  • It is a visual and accessible method of data representation
  • It is readily and easily consumable as it simplifies numeric data and depicts it using a color scale
  • One can easily compare various data points plotted on different heatmaps 
  • It is versatile and adaptable as it can record and present both absolute and derived values
  • It removes multiple steps from the traditional data analysis and interpretation process by laying down all the values in one single heatmap

These are only some of the examples of where heatmaps have helped businesses across industries visualize data better and make data-backed decisions. The possibilities are endless.

VWO’s free AI-powered heatmap generator allows you to understand how users interact with your website. It enables you to find bottlenecks, follow your visitors’ trails and analyze how they interact with each static or dynamic element. To know more about how you can leverage VWO heatmaps to draw valuable insights, sign up for a free demo session from one of VWO’s optimization experts or opt-in for a free trial.

4.10 Subset the heatmap list

Similar as subsetting the Heatmap object (Section 2.13), the heatmap list can also be subsetted by providing row index and column index. For horizontal heatmap list, row index correspond to rows in all heatmaps and annotations, while column index only corresponds to a subset of heatmaps and annotations. For vertical heatmap list, it’s the other way around.

In following we use horizontal heatmap list as example.

foo1 and bar are components of heatmap rnorm, so t

foo1 and bar are components of heatmap rnorm, so they can not be selected in the subset function, while foo2 and bar2 are independent row annotations and they can selected to take subset of them.

4.6 Annotations as components are adjusted

If some of the heatmaps in the heatmap list have annotations, in most of the cases, the heights of the heatmap annotations are different for different heatmaps. There are automatic adjustment for heatmap annotations, and this adjustment will also involve adjustment of dendrograms.

Normally, the size of simple annotations will not change in the adjustment. In following example, the dendrogram for the second heatmap is adjusted. Note you still can change the size of simple annotation by setting anno_simple_size in HeatmapAnnotation() or globally set ht_opt$anno_simple_size.

If the first two heatmaps all have annotations, si

If the first two heatmaps all have annotations, since the size of simple annotations keep unchanged, the size of complex annotations will be adjusted to make the total heights of the two heatmap annotations the same.

Similarly, if the first heatmap only contains simp

Similarly, if the first heatmap only contains simple annotatins, dendrogram will be adjusted.

If the both heatmaps only contain simple annotatio

If the both heatmaps only contain simple annotations but with unequal number, dendrogram will be adjusted.

If you also want to automatically adjust the size

If you also want to automatically adjust the size of simple annotations, set simple_anno_size_adjust = TRUE in every HeatmapAnnotation() calls.

If the second heatmap has no bottom annotation, column names for the second heatmap are adjusted to be put directly below the heatmap body.

Set heatmap x-axis label

We can add a label in x-axis by using the xlabel attribute of Matplotlib as shown in the following code:

The result will be as follows:

The result will be as follows:

Example 1: Create Heatmap with heatmap Function [Base R]

The most common function for creating heatmaps in R is the heatmap() function, which is already provided by the base installation of R.

The heatmap function is applied as shown below:

Figure 1: Default Heatmap in Base R.

Figure 1: Default Heatmap in Base R.

Figure 1 illustrates the output of the previous R code. By default, the heatmap function returns a heatmap with column and row names as well as a dendrogram.

If we want, we can disable the automatically created dendrogram:

Figure 2: Heatmap without Dendrogram in Base R.

Figure 2: Heatmap without Dendrogram in Base R.

Furthermore, we can modify the colors of the heatmap by specifying our own color range with the colorRampPalette function. The following R code produces a function, which creates color ranges between the colors cyan and deeppink3:

Now, we can create a heatmap with this color range as follows:

Figure 3: Heatmap with Manual Color Range in Base

Figure 3: Heatmap with Manual Color Range in Base R.

A list of heatmaps

You can arrange more than one heatmaps which are placed columns by columns. Actually, one single heatmap is just a special case of the heatmap list of length one.

Heatmap() is actually a class constructor function for a single heatmap. If more than one heatmaps are to be combined, users can append one heatmap to the other by + operator.

Under default mode, dendrograms from the second he

Under default mode, dendrograms from the second heatmap will be removed and row orders will be same as the first one.

The returned value of addition of two heatmaps is a HeatmapList object. Directly call ht_list object will call draw() method with default settings. With explicitly calling draw() method, you can have more control on the legend and titles.

You can also append any number of heatmaps to the heatmap list. Also you can append a heatmap list to a heatmap list.

Titles

A heatmap list also has titles which are independent to the heatmap titles.

Legends

Legends

Legends for all heatmaps and all annotations will be drawn together. The legends for heatmaps and legends for annotations are put in independent viewports.

draw(ht_list, heatmap_legend_side = "left&quo

draw(ht_list, show_heatmap_legend = FALSE, show_an

You can choose to only add some of the heatmap leg

You can choose to only add some of the heatmap legends by setting show_heatmap_legend to a logical value.

ComplexHeatmap only generate legends for heatmaps

ComplexHeatmap only generate legends for heatmaps and simple annotations. Self-defined legends can be passed by annotation_legend_list as a list of grob objects.

Graphic parameters for legends for simple annotati

Graphic parameters for legends for simple annotations can be set. Actually these arguments are passed to color_mapping_legend() on ColorMapping class.

Gaps between heatmaps

Gaps between heatmaps

The gaps between heatmaps can be set by gap argument by a unit object.

draw(ht_list + ht_list, gap = unit(c(3, 6, 9, 0),

Size of heatmaps

Size of heatmaps

The width for some (not all) heatmaps can be set to a fixed width.

Auto adjustment

Auto adjustment

There are some automatic adjustment if more than one heatmaps are plotted. There should be a main heatmap which by default is the first one. Some settings for the remaining heatmaps will be modified to the settings in the main heatmap. The adjustment are:

  • row clusters are removed.
  • row titles are removed.
  • if the main heatmap is split by rows, all remaining heatmaps will also be split by same levels as the main one.

The main heatmap can be specified by main_heatmap argument. The value can be a numeric index or the name of the heatmap (of course, you need to set the heatmap name when you create the Heatmap object).

draw(ht2 + ht1)

draw(ht2 + ht1, main_heatmap = "ht1")

If there is no row clustering in the main heatmap,

If there is no row clustering in the main heatmap, all other heatmaps have no row clustering neither.

Recommended for you

Recommended for you

This section contains best data science and self-development resources to help you on your path.

Popular Courses Launched in 2020

Amazing Selling Machine

Tags

Leave a Reply

Your email address will not be published.