Author: Tal Galili ( Tal.Galili@gmail.com )
A heatmap is a popular graphical method for visualizing high-dimensional data, in which a table of numbers are encoded as a grid of colored cells. The rows and columns of the matrix are ordered to highlight patterns and are often accompanied by dendrograms. Heatmaps are used in many fields for visualizing observations, correlations, missing values patterns, and more.
Interactive heatmaps allow the inspection of specific value by hovering the mouse over a cell, as well as zooming into a region of the heatmap by draging a rectangle around the relevant area.
This work is based on the ggplot2 and plotly.js engine. It produces similar heatmaps as d3heatmap, with the advatange of speed (plotly.js is able to handle larger size matrix), and the ability to zoom from the dendrogram.
To install the stable version on CRAN:
# install.packages('heatmaply')
To install the GitHub version:
# You'll need devtools
install.packages.2 <- function (pkg) if (!require(pkg)) install.packages(pkg);
install.packages.2('devtools')
# make sure you have Rtools installed first! if not, then run:
#install.packages('installr'); install.Rtools()
devtools::install_github("ropensci/plotly")
devtools::install_github('talgalili/heatmaply')
And then you may load the package using:
library("heatmaply")
library(heatmaply)
heatmaply(mtcars)
Because the labels are somewhat long, we need to manually fix the margins (hopefully this will be fixed in future versions of plot.ly)
heatmaply(mtcars) %>% layout(margin = list(l = 130, b = 40))
We can use this with correlation. Notice the use of limits to set the range of the colors, and how we color the branches:
heatmaply(cor(mtcars),
k_col = 2, k_row = 2,
limits = c(-1,1)) %>%
layout(margin = list(l = 40, b = 40))
We can use different colors:
heatmaply(mtcars, colors = heat.colors(100))
Or even more customized colors using scale_fill_gradient_fun
:
heatmaply(mtcars,
scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(low = "blue", high = "red", midpoint = 200, limits = c(0, 500)))
This package is thanks to the amazing work done by MANY people in the open source community. Beyond the many people working on the pipeline of R, thanks should go to the plotly team, and especially to Carson Sievert and others working on the R package of plotly. Also, many of the design elements were inspired by the work done on heatmap, heatmap.2 and d3heatmap, so special thanks goes to the R core team, Gregory R. Warnes, and Joe Cheng from RStudio. The dendrogram side of the package is based on the work in dendextend, in which special thanks should go to Andrie de Vries for his original work on bringing dendrograms to ggplot2 (which evolved into the richer ggdend objects, as implemented in dendextend).
You are welcome to:
You can see the most recent changes to the package in the NEWS.md file
sessionInfo()
#> R version 3.3.0 (2016-05-03)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 7 x64 (build 7601) Service Pack 1
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=Hebrew_Israel.1255
#> [3] LC_MONETARY=Hebrew_Israel.1255 LC_NUMERIC=C
#> [5] LC_TIME=Hebrew_Israel.1255
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] knitr_1.12.3 heatmaply_0.3.2 viridis_0.3.4 plotly_3.6.1
#> [5] ggplot2_2.1.0
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_0.12.5 whisker_0.3-2 magrittr_1.5
#> [4] munsell_0.4.3 colorspace_1.2-6 R6_2.1.2
#> [7] stringr_1.0.0 httr_1.1.0 plyr_1.8.3
#> [10] dplyr_0.4.3 tools_3.3.0 parallel_3.3.0
#> [13] grid_3.3.0 gtable_0.2.0 DBI_0.4-1
#> [16] htmltools_0.3.5 assertthat_0.1 yaml_2.1.13
#> [19] digest_0.6.9 gridExtra_2.2.1 reshape2_1.4.1
#> [22] RColorBrewer_1.1-2 formatR_1.3 tidyr_0.4.1
#> [25] base64enc_0.1-3 htmlwidgets_0.6 dendextend_1.1.9
#> [28] evaluate_0.9 rmarkdown_0.9.6 labeling_0.3
#> [31] stringi_1.0-1 scales_0.4.0 jsonlite_0.9.20