Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Created September 22, 2016 00:57
Show Gist options
  • Select an option

  • Save crazyhottommy/db343a891ff5bc59a9b2deb599f69e33 to your computer and use it in GitHub Desktop.

Select an option

Save crazyhottommy/db343a891ff5bc59a9b2deb599f69e33 to your computer and use it in GitHub Desktop.
Make a heatmap with colored dendrogram by `complexHeatmap` and `Dendsort`.
See help [here](https://bioconductor.org/packages/release/bioc/vignettes/ComplexHeatmap/inst/doc/s2.single_heatmap.html)
```r
##### a make_hc function to receive different distance_measure and linkage_method
make_hc<- function(x, distance_measure, linkage_method){
if (distance_measure == "pearson"){
## cor calculate for columns, needs to transpose x first
distance <- as.dist(1-cor(t(x), method = "pearson"))
hc<- hclust(distance, method = linkage_method)
} else {
## dist calculates for rows
distance<- dist(x, method = distance_measure)
hc<- hclust(distance, method = linkage_method)
}
return (hc)
}
## row is gene
hc_row<- make_hc(mat, "pearson", "complete")
row_dend<- as.dendrogram(hc_row)
## column is sample
hc_col<- make_hc(t(mat), "euclidean", "complete")
col_dend<- as.dendrogram(hc_col)
## color branches
library(dendextend)
row_dend<- color_branches(row_dend, k = 2)
col_dend<- color_branches(col_dend, k = 3)
Heatmap(mat, name = "foo", cluster_rows = row_dend, cluster_columns = col_dend)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment