<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/heatmaply-an-r-package-for-creating-interactive-cluster-heatmaps-for-online-publishing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-statistics.com/2017/10/heatmaply-an-r-package-for-creating-interactive-cluster-heatmaps-for-online-publishing/"> R – R-statistics blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>This post on the <a rel="nofollow" href="https://cran.r-project.org/web/packages/heatmaply/" target="_blank">heatmaply package</a> is based on my <a rel="nofollow" href="https://doi.org/10.1093/bioinformatics/btx657" target="_blank">recent paper from</a> the journal <a rel="nofollow" href="https://bioinformatics.oxfordjournals.org/" target="_blank">bioinformatics</a> (a link to <a rel="nofollow" href="https://doi.org/10.1093/bioinformatics/btx657" target="_blank">a stable DOI</a>). The paper was published just last week, and since it is released as <a rel="nofollow" href="https://creativecommons.org/licenses/by/4.0/" target="_blank">CC-BY</a>, I am permitted (and delighted) to republish it here in full. My co-authors for this paper are <a rel="nofollow" href="https://github.com/yonicd" target="_blank">Jonathan Sidi</a>, <a rel="nofollow" href="https://github.com/Alanocallaghan" target="_blank">Alan O’Callaghan</a>, and <a rel="nofollow" href="https://github.com/cpsievert" target="_blank">Carson Sievert</a>.</em></p>
<p><strong>Summary:</strong> <a rel="nofollow" href="https://cran.r-project.org/web/packages/heatmaply/" target="_blank"><em>heatmaply</em></a> is an R package for easily creating interactive cluster heatmaps that can be shared online as a stand-alone HTML file. Interactivity includes a tooltip display of values when hovering over cells, as well as the ability to zoom in to specific sections of the figure from the data matrix, the side dendrograms, or annotated labels.  Thanks to the synergistic relationship between <em>heatmaply</em> and other R packages, the user is empowered by a refined control over the statistical and visual aspects of the heatmap layout.</p>
<p><strong>Availability:</strong> The <em>heatmaply</em> package is available under the GPL-2 Open Source license. It comes with a detailed vignette, and is freely available from: <a rel="nofollow" href="http://cran.r-project.org/package=heatmaply" target="_blank">http://cran.r-project.org/package=<em>heatmaply</em></a></p>
<p><span></span></p>
<p><strong>Contact:</strong> <a rel="nofollow" href="mailto:Tal.Galili@math.tau.ac.il" target="_blank">Tal.Galili@math.tau.ac.il</a></p>
<h3>Introduction</h3>
<p>A cluster <a rel="nofollow" href="https://en.wikipedia.org/wiki/Heat_map" target="_blank">heatmap</a> is a popular graphical method for visualizing high dimensional data. In it, a table of numbers is scaled and encoded as a tiled matrix of colored cells. The rows and columns of the matrix are ordered to highlight patterns and are often accompanied by dendrograms and extra columns of categorical annotation. The ongoing development of this iconic visualization, spanning over more than a century, has provided the foundation for one of the most widely used of all bioinformatics displays (Wilkinson and Friendly, 2009). When using the R language for statistical computing (R Core Team, 2016), there are many available packages for producing static heatmaps, such as: <em>stats</em>, <a rel="nofollow" href="https://cran.r-project.org/web/packages/gplots/" target="_blank"><em>gplots</em></a>, <a rel="nofollow" href="https://cran.r-project.org/web/packages/heatmap3/" target="_blank"><em>heatmap3</em></a>, <a rel="nofollow" href="https://cran.r-project.org/web/packages/fheatmap/" target="_blank"><em>fheatmap</em></a>, <a rel="nofollow" href="https://cran.r-project.org/web/packages/pheatmap/" target="_blank"><em>pheatmap</em></a>, and others. Recently released packages also allow for more complex layouts; these include <a rel="nofollow" href="https://cran.r-project.org/web/packages/gapmap/" target="_blank"><em>gapmap</em></a>, <a rel="nofollow" href="https://cran.r-project.org/web/packages/superheat/" target="_blank"><em>superheat</em></a>, and <a rel="nofollow" href="https://cran.r-project.org/web/packages/ComplexHeatmap/" target="_blank"><em>ComplexHeatmap </em></a>(Gu <em>et al.</em>, 2016). The next evolutionary step has been to create interactive cluster heatmaps, and several solutions are already available. However, these solutions, such as the <a rel="nofollow" href="https://cran.r-project.org/web/packages/idendro/" target="_blank"><em>idendro</em> </a>R package (Sieger <em>et al.</em>, 2017), are often focused on providing an interactive output that can be explored only on the researcher’s personal computer. Some solutions do exist for creating shareable interactive heatmaps. However, these are either dependent on a specific online provider, such as XCMS Online, or require JavaScript knowledge to operate, such as InCHlib. In practice, when publishing in academic journals, the reader is left with a static figure only (often in a png or pdf format).</p>
<p>To fill this gap, we have developed the <a rel="nofollow" href="https://cran.r-project.org/web/packages/heatmaply/" target="_blank"><em>heatmaply</em> R package</a> for easily creating a shareable HTML file that contains an interactive cluster heatmap. The interactivity is based on a client-side JavaScript code that is generated based on the user’s data, after running the following command:</p>
<div>
<table>
<tr>
<td>
<pre><span>install.<span>packages</span></span><span>(</span><span>&quot;heatmaply&quot;</span><span>)</span>
<span>library</span><span>(</span>heatmaply<span>)</span>
heatmaply<span>(</span><span>data</span>, <span>file</span> <span>=</span> <span>&quot;my_heatmap.html&quot;</span><span>)</span></pre>
</td>
</tr>
</table>
</div>
<p>The HTML file contains a publication-ready, interactive figure that allows the user to zoom in as well as see values when hovering over the cells. This self-contained HTML file can be made available to interested readers by uploading it to the researcher’s homepage or as a supplementary material in the journal’s server. Concurrently, this interactive figure can be displayed in RStudio’s viewer pane, included in a Shiny application, or embedded in a knitr/RMarkdown HTML documents.</p>
<p>The rest of this paper offers guidelines for creating effective cluster heatmap visualization. Figure 1 demonstrates the suggestions from this section on <a rel="nofollow" href="https://benjaminlmoore.wordpress.com/2015/04/09/recreating-the-vaccination-heatmaps-in-r/" target="_blank">data from project Tycho</a> (van Panhuis <em>et al.</em>, 2013), while the online supplementary information includes the interactive version, as well as several examples of using the package on real-world biological data.</p>
<p><strong>Fig. 1. The (square root) number of people infected by Measles in 50 states, from 1928 to 2003. Vaccines were introduced in 1963</strong></p>
<p><strong><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles_heatmaply.html" target="_blank">click the image for the online interactive version of the plot</a></strong></p>
<p><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles_heatmaply.html" target="_blank"><img sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" src="https://i0.wp.com/www.r-statistics.com/wp-content/uploads/2017/10/measles_heatmaply.png?resize=700%2C450" alt="" class="wp-image-61490" data-recalc-dims="1"></a></p>
<h4>An interactive version of the measles heatmap (embedded in the post using iframe)
<p>I uploaded the measles_heatmaply.html to github and then used the following code to embed it in the post:</p>
<p><iframe width="”450″" src="https://www.r-bloggers.com/heatmaply-an-r-package-for-creating-interactive-cluster-heatmaps-for-online-publishing/”https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles_heatmaply.html”" height="”450″"></iframe></p>
<p>Here is the result:</p>
<p><iframe width="450" src="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles_heatmaply.html" height="450"></iframe></p>
<h3>heatmaply – a simple example</h3>
<p>The generation of cluster heatmaps is a subtle process (Gehlenborg and Wong, 2012; Weinstein, 2008), requiring the user to make many decisions along the way. The major decisions to be made deal with the data matrix and the dendrogram. The raw data often need to be transformed in order to have a meaningful and comparable scale, while an appropriate color palette should be picked. The clustering of the data requires us to decide on a distance measure between the observation, a linkage function, as well as a rotation and coloring of branches that manage to highlight interpretable clusters. Each such decision can have consequences on the patterns and interpretations that emerge. In this section, we go through some of the arguments in the function heatmaply, aiming to make it easy for the user to tune these important statistical and visual parameters. Our toy example visualizes the effect of vaccines on measles infection. The output is given in the static Fig. 1, while an interactive version is available online in the supplementary file “measles.html”. Both were created using:</p>
<div>
<table>
<tr>
<td>
<pre>heatmaply<span>(</span>x <span>=</span> <span>sqrt</span><span>(</span>measles<span>)</span>,
           color <span>=</span> viridis, <span># the default</span>
           Colv <span>=</span> NULL,
           hclust_method <span>=</span> <span>&quot;average&quot;</span>, k_row <span>=</span> NA, <span># ...</span>
           <span>file</span> <span>=</span> <span>c</span><span>(</span><span>&quot;measles.html&quot;</span>, <span>&quot;measles.png&quot;</span><span>)</span> <span>)</span></pre>
</td>
</tr>
</table>
</div>
<p>The first argument of the function (x) accepts a matrix of the data. In the measles data, each row corresponds with a state, each column with a year (from 1928 to 2003), and each cell with the number of people infected with measles per 100,000 people. In this example, the data were scaled twice – first by not giving the raw number of cases with measles, but scaling them relatively to 100,000 people, thus making it possible to more easily compare between states. And second by taking the square root of the values. This was done since all the values in the data represent the same unit of measure, but come from a right-tailed distribution of count data with some extreme observations. Taking the square root helps with bringing extreme observations closer to one another, helping to avoid an extreme observation from masking the general pattern. Other transformations that may be considered come from Box-Cox or Yeo-Johnson family of power transformations. If each column of the data were to represent a different unit of measure, then leaving the values unchanged will often result in the entire figure being un-usable due to the column with the largest range of values taking over most of the colors in the figure. Possible per-column transformations include the scale function, suitable for data that are relatively normal. normalize, and percentize functions bring data to the comparable 0 to 1 scale for each column. The normalize function preserves the shape of each column’s distribution by subtracting the minimum and dividing by the maximum of all observations for each column. The percentize function is similar to ranking but with the simpler interpretation of each value being replaced by the percent of observations that have that value or below. It uses the empirical cumulative distribution function of each variable on its own values. The sparseness of the dataset can be explored using is.na10.</p>
<p>Once the data are adequately scaled, it is important to choose a good color palette for the data. Other than being pretty, an ideal color palette should have three (somewhat conflicting) properties: (1) Colorful, spanning as wide a palette as possible so as to make differences easy to see; (2) Perceptually uniform, so that values close to each other have similar-appearing colors compared with values that are far away, consistently across the range of values; and (3) Robust to colorblindness, so that the above properties hold true for people with common forms of colorblindness, as well as printing well in grey scale. The default passed to the color argument in heatmaply is viridis, which offers a sequential color palette, offering a good balance of these properties. Divergent color scale should be preferred when visualizing a correlation matrix, as it is important to make the low and high ends of the range visually distinct. A helpful divergent palette available in the package is cool_warm (other alternatives in the package include RdBu, BrBG, or RdYlBu, based on the <a rel="nofollow" href="https://cran.r-project.org/web/packages/RColorBrewer/" target="_blank"><em>RColorBrewer</em></a> package). It is also advisable to set the limits argument to range from -1 to 1.</p>
<p>Passing NULL to the Colv argument, in our example, removed the column dendrogram (since we wish to keep the order of the columns, relating to the years). The row dendrogram is automatically calculated using hclust with a Euclidean distance measure and the average linkage function. The user can choose to use an alternative clustering function (hclustfun), distance measure (dist_method), or linkage function (hclust_method), or to have a dendrogram only in the rows/columns or none at all (through the dendrogram argument). Also, the users can supply their own dendrogram objects into the Rowv (or Colv) arguments. The preparation of the dendrograms can be made easier using the <a rel="nofollow" href="https://cran.r-project.org/web/packages/dendextend/" target="_blank"><em>dendextend</em></a> R package (Galili, 2015) for comparing and adjusting dendrograms. These choices are all left for the user to decide. Setting the k_col/k_row argument to NA makes the function search for the number of clusters (between from 2 to 10) by which to color the branches of the dendrogram. The number picked is the one that yields the highest average silhouette coefficient (based on the find_k function from <a rel="nofollow" href="https://cran.r-project.org/web/packages/dendextend/" target="_blank"><em>dendextend</em></a>). Lastly, the heatmaply function uses the <a rel="nofollow" href="https://cran.r-project.org/web/packages/seriation/" target="_blank"><em>seriation</em></a> package to find an “optimal” ordering of rows and columns (Hahsler <em>et al.</em>, 2008). This is controlled using the seriation argument where the default is “OLO” (optimal-leaf-order) – which rotates the branches so that the sum of distances between each adjacent leaf (label) will be minimized (i.e.: optimize the Hamiltonian path length that is restricted by the dendrogram structure). The other arguments in the example were omitted since they are self-explanatory – the exact code is available in the supplementary material.</p>
<p>In order to make some of the above easier, we created the <a rel="nofollow" href="https://cran.r-project.org/web/packages/shinyHeatmaply/" target="_blank">shinyHeatmaply</a> package (available on CRAN) which offers a GUI to help guide the researcher with the heatmap construction, with the functionality to export the heatmap as an html file and summaries parameter specifications to reproduce the heatmap with heatmaply. For more detailed step-by-step demonstration of using heatmaply on biological datasets, you should explore the <a rel="nofollow" href="https://github.com/talgalili/heatmaplyExamples" target="_blank">heatmaplyExamples</a> package (at <a rel="nofollow" href="https://github.com/talgalili/heatmaplyExamples" target="_blank">github.com/talgalili/heatmaplyExamples</a>).</p>
<p>The following biological examples are available and fully reproducible from within the package. You may also view them online in the following links (the html files also include the R code for producing the figures):</p>
<ul>
<li><a rel="nofollow" href="https://cran.r-project.org/package=heatmaply/vignettes/heatmaply.html" target="_blank">Introduction to heatmaply</a></li>
<li>General biological examples
<ul>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles.html" target="_blank">Using heatmaply with the measles data set</a> ( <a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/measles_heatmaply.html" target="_blank">the final output</a>)</li>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/heatmaply_examples.html" target="_blank">Using heatmaply with famous data sets</a></li>
</ul>
</li>
<li>Reproducing heatmaps from papers published in Nature
<ul>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/reproducing_Nature_2015_Kotsyfakis.html" target="_blank">Using heatmaply to reproduce Nature (2015) Kotsyfakis et al.</a></li>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/reproducing_Nature_2015_Alfano.html" target="_blank">Using heatmaply to reproduce Nature (2015) Alfano et al.</a></li>
</ul>
</li>
<li>Using heatmaply with gene expression data
<ul>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/biological_data.html" target="_blank">Visualization of raw and voom-transformed data (all genes)</a></li>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/biological_data_2.html" target="_blank">Visualization of raw data (median-centered data, PAM50 genes only)</a></li>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/biological_data_3.html" target="_blank">Visualization of voom-transformed data (median-centered data, PAM50 genes only)</a></li>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/non_centred_heatmaps.html" target="_blank">Using heatmaply with non-centred RNAseq heatmaps (PAM50 genes)</a></li>
</ul>
</li>
<li>General examples
<ul>
<li><a rel="nofollow" href="https://cdn.rawgit.com/talgalili/heatmaplyExamples/master/inst/doc/glmnet.html" target="_blank">Using heatmaply for visualizing glmnet coefficient path</a></li>
</ul>
</li>
</ul>
<h3>Acknowledgements</h3>
<p>The heatmaply package was made possible by leveraging many wonderful R packages, including <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggplot2/" target="_blank"><em>ggplot2</em></a> (Wickham, 2009), <a rel="nofollow" href="https://cran.r-project.org/web/packages/plotly/index.html" target="_blank"><em>plotly</em></a> (Sievert <em>et al.</em>, 2016), <a rel="nofollow" href="https://cran.r-project.org/web/packages/dendextend/" target="_blank"><em>dendextend</em></a> (Galili, 2015) and many others. We would also like to thank <a rel="nofollow" href="https://scholar.google.com/citations?user=eZ51GLAAAAAJ" target="_blank">Yoav Benjamini</a>, Madeline Bauer, and Marilyn Friedes for their helpful comments, as well as <a rel="nofollow" href="https://github.com/jcheng5" target="_blank">Joe Cheng</a> for initiating the collaboration with <a rel="nofollow" href="https://www.r-statistics.com/about/" target="_blank">Tal Galili</a> on <a rel="nofollow" href="https://github.com/rstudio/d3heatmap" target="_blank">d3heatmap</a>, which helped lay the foundation for heatmaply.</p>
<p><em>Funding</em>: This work was supported in part by the European Union Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 604102 (Human Brain Project). <em> </em></p>
<p><em>Conflict of Interest</em>: none declared.</p>
<h3>References</h3>
<ul>
<li>Galili,T. (2015) dendextend: an R package for visualizing, adjusting and comparing trees of hierarchical clustering. <em>Bioinformatics</em>, <strong>31</strong>, 3718–3720.</li>
<li>Gehlenborg,N. and Wong,B. (2012) Points of view: Heat maps. <em>Nat. Methods</em>, <strong>9</strong>, 213–213.</li>
<li>Gu,Z. <em>et al.</em> (2016) Complex heatmaps reveal patterns and correlations in multidimensional genomic data. <em>Bioinformatics</em>, <strong>32</strong>, 2847–2849.</li>
<li>Hahsler,M. <em>et al.</em> (2008) Getting Things in Order : An Introduction to the R Package seriation. <em>J. Stat. Softw.</em>, <strong>25</strong>, 1–27.</li>
<li>van Panhuis,W.G. <em>et al.</em> (2013) Contagious Diseases in the United States from 1888 to the Present. <em>N. Engl. J. Med.</em>, <strong>369</strong>, 2152–2158.</li>
<li>R Core Team,(R Foundation for Statistical Computing) (2016) R: A Language and Environment for Statistical Computing.</li>
<li>Sieger,T. <em>et al.</em> (2017) Interactive Dendrograms: The R Packages idendro and idendr0. <em>J. Stat. Softw.</em>, <strong>76</strong>.</li>
<li>Sievert,C. <em>et al.</em> (2016) plotly: Create Interactive Web Graphics via ‘plotly.js’.</li>
<li>Weinstein,J.N. (2008) BIOCHEMISTRY: A Postgenomic Visual Icon. <em>Science (80-. ).</em>, <strong>319</strong>, 1772–1773.</li>
<li>Wickham,H. (2009) ggplot2 Elegant Graphics for Data Analysis.</li>
<li>Wilkinson,L. and Friendly,M. (2009) The History of the Cluster Heat Map. <em>Am. Stat.</em>, <strong>63</strong>, 179–184.</li>
</ul>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-statistics.com/2017/10/heatmaply-an-r-package-for-creating-interactive-cluster-heatmaps-for-online-publishing/"> R – R-statistics blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/HAUDW4d9h9M" height="1"></a></div></h4>