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

<!–html_preserve–>

<!–/html_preserve–>

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

<!–html_preserve–>

<!–/html_preserve–>

# 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)

<!–html_preserve–>

<!–/html_preserve–>

# 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)

<!–html_preserve–>

<!–/html_preserve–>

Session info

sessionInfo()
## R version 3.2.0 (2015-04-16)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10.4 (Yosemite)
## 
## 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  dendextend_1.0.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.11.6.2      png_0.1-7          digest_0.6.8      
##  [4] plyr_1.8.3         jsonlite_0.9.16    formatR_1.2       
##  [7] magrittr_1.5       evaluate_0.7       scales_0.2.5      
## [10] stringi_0.5-5      whisker_0.3-2      RColorBrewer_1.1-2
## [13] tools_3.2.0        stringr_1.0.0      htmlwidgets_0.5   
## [16] munsell_0.4.2      yaml_2.1.13        base64enc_0.1-2   
## [19] colorspace_1.2-6   htmltools_0.2.6    knitr_1.10.18