#Create random data
data <- replicate(20, rnorm(50))
rownames(data) <- paste("Gene", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
out <- pheatmap(data,
show_rownames=T, cluster_cols=T, cluster_rows=T, scale="row",
cex=1, clustering_distance_rows="euclidean", cex=1,
clustering_distance_cols="euclidean", clustering_method="complete", border_color=FALSE)
#Re-order original data (genes) to match ordering in heatmap (top-to-bottom)
rownames(data[out$tree_row[["order"]],])
[1] "Gene 2" "Gene 12" "Gene 9" "Gene 33" "Gene 29" "Gene 43" "Gene 24"
[8] "Gene 44" "Gene 3" "Gene 21" "Gene 46" "Gene 26" "Gene 20" "Gene 45"
[15] "Gene 18" "Gene 38" "Gene 22" "Gene 1" "Gene 17" "Gene 7" "Gene 6"
[22] "Gene 41" "Gene 30" "Gene 31" "Gene 13" "Gene 16" "Gene 11" "Gene 50"
[29] "Gene 34" "Gene 37" "Gene 15" "Gene 25" "Gene 27" "Gene 39" "Gene 19"
[36] "Gene 35" "Gene 4" "Gene 49" "Gene 10" "Gene 28" "Gene 8" "Gene 14"
[43] "Gene 32" "Gene 42" "Gene 36" "Gene 48" "Gene 23" "Gene 47" "Gene 5"
[50] "Gene 40"
#Re-order original data (samples) to match ordering in heatmap (left-to-right)
colnames(data[,out$tree_col[["order"]]])
[1] "Sample 3" "Sample 6" "Sample 1" "Sample 17" "Sample 8" "Sample 10"
[7] "Sample 15" "Sample 4" "Sample 18" "Sample 2" "Sample 11" "Sample 9"
[13] "Sample 13" "Sample 12" "Sample 7" "Sample 19" "Sample 14" "Sample 16"
[19] "Sample 5" "Sample 20"
-------------------------
If you want something like gene-to-cluster assignment, you can 'cut' your row dendrogram into a pre-selected number of groups as follows:
#2 groups
sort(cutree(out$tree_row, k=2))
Gene 1 Gene 2 Gene 3 Gene 9 Gene 12 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22
1 1 1 1 1 1 1 1 1 1
Gene 24 Gene 26 Gene 29 Gene 33 Gene 38 Gene 43 Gene 44 Gene 45 Gene 46 Gene 4
1 1 1 1 1 1 1 1 1 2
Gene 5 Gene 6 Gene 7 Gene 8 Gene 10 Gene 11 Gene 13 Gene 14 Gene 15 Gene 16
2 2 2 2 2 2 2 2 2 2
Gene 19 Gene 23 Gene 25 Gene 27 Gene 28 Gene 30 Gene 31 Gene 32 Gene 34 Gene 35
2 2 2 2 2 2 2 2 2 2
Gene 36 Gene 37 Gene 39 Gene 40 Gene 41 Gene 42 Gene 47 Gene 48 Gene 49 Gene 50
2 2 2 2 2 2 2 2 2 2
#5 groups
sort(cutree(out$tree_row, k=5))
Gene 1 Gene 3 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22 Gene 24 Gene 26 Gene 38
1 1 1 1 1 1 1 1 1 1
Gene 44 Gene 45 Gene 46 Gene 2 Gene 9 Gene 12 Gene 29 Gene 33 Gene 43 Gene 4
1 1 1 2 2 2 2 2 2 3
Gene 19 Gene 35 Gene 49 Gene 5 Gene 8 Gene 10 Gene 14 Gene 23 Gene 28 Gene 32
3 3 3 4 4 4 4 4 4 4
Gene 36 Gene 40 Gene 42 Gene 47 Gene 48 Gene 6 Gene 7 Gene 11 Gene 13 Gene 15
4 4 4 4 4 5 5 5 5 5
...
You can also cut the tree at a pre-defined tree height, and extract the gene-to-cluster assignments at that height:
plot(out$tree_row)
abline(h=7, col="red", lty=2, lwd=2)
#Cut the row (gene) dendrogram at a Euclidean distance dis-similarity of 8
sort(cutree(out$tree_row, h=7))
Gene 1 Gene 3 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22 Gene 24 Gene 26 Gene 38
1 1 1 1 1 1 1 1 1 1
Gene 44 Gene 45 Gene 46 Gene 2 Gene 9 Gene 12 Gene 29 Gene 33 Gene 43 Gene 4
1 1 1 2 2 2 2 2 2 3
Gene 19 Gene 35 Gene 49 Gene 5 Gene 8 Gene 10 Gene 14 Gene 23 Gene 28 Gene 32
3 3 3 4 4 4 4 4 4 4
Gene 36 Gene 40 Gene 42 Gene 47 Gene 48 Gene 6 Gene 7 Gene 13 Gene 16 Gene 30
4 4 4 4 4 5 5 5 5 5
Gene 31 Gene 41 Gene 11 Gene 15 Gene 25 Gene 27 Gene 34 Gene 37 Gene 39 Gene 50
5 5 6 6 6 6 6 6 6 6
Dear Sir, I have 6 clusters of samples from pheatmap want to extract the dendrogram and color code each cluster dendrogram via color . is there any way to extract and depcit color coded dendroagram of the column only? your help is appreciated?
heatmap <- pheatmap(matrix,cluster_rows = TRUE, cluster_cols = TRUE, clustering_distance_rows = "canberra", clustering_distance_cols = "canberra", clustering_method = "average", color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100), annotation_col=annotation_col,annotation_colors=ann_colors, fontsize = 8, show_colnames = F, cutree_cols = 6, labels_row = as.expression(row_names), width = 8, height = 6, treeheight_row=0)
Clusters <- cutree(heatmap$tree_col, k=6)[heatmap$tree_col[["order"]]]