Gplots Heatmap

Posted : admin On 3/31/2022

This tutorial explains how to create a heatmap in R using ggplot2.

Example: Creating a Heatmap in R

To create a heatmap, we’ll use the built-in R dataset mtcars.

Currently mtcars is in a wide format, but we need to melt it into a long format in order to create the heatmap.

We can use the following code to create the heatmap in ggplot2:

Unfortunately, since the values for disp are much larger than the values for all the other variables in the data frame, it’s hard to see the color variation for the other variables.

One way to solve this problem is to rescale the values for each variable from 0 to 1 using the rescale() function in the scales() package and the ddply() function in the plyr() package:

We can also change up the colors of the heatmap by changing the colors used in the scale_fill_gradient() argument:

Enhanced heatmap representation with partition and summary statistics (optional). This is an enhanced version of `heatmap.2' function in the Package gplots. The enhancement includes: 1) Improved. Require(gplots) heatmap.2(x) To add a title, x- or y-label to your heatmap, you need to set the main, xlab and ylab: heatmap.2(x, main = 'My main title: Overview of car features', xlab='Car features', ylab = 'Car. Hello all, I'm trying to plot a heatmap using the heatmap.2 function from the 'Gplots' package.I. Retrieving individual clusters from RNA-seq hierarchical clustering in R I have a dataset which consists.

Note that the heatmap is currently ordered by car name. We could instead order the heatmap according to the values of one of the variables like mpg using the following code:

To order the heatmap by mpg ascending, we simply need to use -mpg in the reorder() argument:

Lastly, we can remove the x-axis and y-axis labels along with the legend if we don’t like how it looks using the labs() and theme() arguments:

5 min read2021-01-22

With InteractiveComplexHeatmap, the following heatmaps can be exported as an interactive Shiny app:

  1. heatmaps directly produced from ComplexHeatmap,
  2. heatmaps from other functions or packages which are implement with ComplexHeatmap,
  3. heatmaps originally produced by stats::heatmap(), gplots::heatmap.2()and pheatmap::pheatmap(), but can be reproduced by the “translationfunctions”: ComplexHeatmap:::heatmap(), ComplexHeatmap:::heatmap.2()and ComplexHeatmap::pheatmap().

All these types of heatmaps can be turned into interactive just by callinghtShiny() after the heatmaps are drawn. E.g.:

which means you don’t need to touch your heatmap code. After you see the heatmap in your R terminal,directly calling htShiny() with no argument will produce an interactive heatmap, like magic. :P

Now there is a fourth scenario where the heatmap is produced by third-partyfunctions which uses stats::heatmap(), gplots::heatmap.2() orpheatmap::pheatmap(). Since now we cannot directly interact withheatmap(), heatmap.2() or pheatmap(), how can we turn these heatmaps intointeractive? The solution is fairly simple. We just need to go to e.g.pheatmapnamespace and replace pheatmap with ComplexHeatmap::pheatmap.

The following example is from the SC3 package wherefunction sc3_plot_expression() internally uses pheatmap().

Gplots Heatmap R

To replace the internally use of pheatmap::pheatmap withComplexHeatmap::pheatmap, we can use assignInNamespace() to directlychange the value of pheatmap in pheatmap namespace. After that,recalling sc3_plot_expression() will directly useComplexHeatmap::pheatmap() and now you can use htShiny() to export it asan interactive app. Of course, you need to regenerate the heatmap with thesame code.

If you check the source code of sc3_plot_expression(), pheatmap() is usedby explicitely adding its namespace (check the last few lines of the function definition):

Plots heatmap

In this case, changing pheatmap in pheatmap namespace directly affects sc3_plot_expression().

However, if the heatmap function is called without adding the namespace, youneed to first unload the package, modify the heatmap function in the heatmapnamespace and later load the package back.

Let’s look at the next example from GOexpress package where the function heatmap_GO() internally use heatmap.2().

Now note in heatmap_GO() function, heatmap.2() is used without gplots namespace (go tothe end of the function definition listed below).

Gplots Heatmap.2 Function

In this case, the GOexpression namespace should firstly be removed bydetach(), or else heatmap_GO() will still use gplots::heatmap.2().

In the end, to safely change all stats::heatmap(), gplots::heatmap.2() andpheatmap::pheatmap() to ComplexHeatmap:::heatmap(),ComplexHeatmap:::heatmap.2() and ComplexHeatmap::pheatmap(), you can add following linesat the start of your R script: