Exampels

suppressMessages(library(dendextend))
library(d3heatmap)

mtcars

x <- mtcars # [c(2:4,7),1:4]

d3heatmap(x, k_row = 4, k_col = 2)
## Loading required namespace: colorspace

d3heatmap(x, k_row = 4, k_col = 2, scale = "column") # a more appropriate scalling

# row_dend2 <- x %>% dist %>% hclust %>% as.dendrogram %>%
#   color_branches(k = 3)
# col_dend2 <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>%
#   color_branches(k = 2)
# d3heatmap(x, Rowv = row_dend2, Colv = col_dend2)
# d3heatmap(x, Rowv = row_dend2, Colv = col_dend2, scale = "column") # a more appropriate scaling

We can also look at the correlation between the measures:

x_cor <- cor(x)

x_cor %>% d3heatmap(k_row = 3, k_col = 3) # (symm = TRUE)

# x_cor %>% d3heatmap(reorderfun = function(x, ...) x) # this doesn't reorder the rows/columns based on the means.
# Note the use of k_row/k_col to color the branches

# # reproducing the dendrogram, but with manually created the dendrograms
# cor_dend <- x_cor  %>% dist %>% hclust(method = "com") %>% as.dendrogram %>%
#   color_branches(k = 3) %>% 
#   reorder(rowMeans(x_cor)) # this is what is done inside heatmap.
# x_cor %>% d3heatmap(Rowv = cor_dend, Colv = cor_dend)

Iris

x <- datasets::iris[,-5] # [c(2:4,7),1:4]

row_dend2 <- x %>% dist %>% hclust(method = "ave") %>% as.dendrogram %>%
  color_branches(k = 3) %>% 
  reorder(rowMeans(x)) 
col_dend2 <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>%
  color_branches(k = 2) %>% 
  sort # This sorting requires dendextend and cannot be easily done without it

d3heatmap(x, Rowv = row_dend2, Colv = col_dend2,
          colors = "Greens",
          # scale = "row",
          width = 600,
          show_grid = FALSE)

Session info

sessionInfo()
## R version 3.2.2 (2015-08-14)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.11.3 (El Capitan)
## 
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] d3heatmap_0.6.1.1 dendextend_1.0.1 
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.3        png_0.1-7          digest_0.6.9      
##  [4] plyr_1.8.3         jsonlite_0.9.19    formatR_1.2.1     
##  [7] magrittr_1.5       scales_0.3.0       evaluate_0.8      
## [10] stringi_1.0-1      whisker_0.3-2      rmarkdown_0.9.2   
## [13] RColorBrewer_1.1-2 tools_3.2.2        stringr_1.0.0     
## [16] htmlwidgets_0.6    munsell_0.4.2      yaml_2.1.13       
## [19] colorspace_1.2-6   base64enc_0.1-3    htmltools_0.3.2   
## [22] knitr_1.12.3