raivokolde / pheatmap

Pretty heatmaps
225 stars 83 forks source link

extract order of clustered column #100

Open chrismahony opened 6 months ago

chrismahony commented 6 months ago

I have created my pheatmap using:

p<-pheatmap(mat), scale = "row", show_rownames = T, show_colnames = T, border_color = NA, clustering_method = "average", clustering_distance_rows = "correlation", clustering_distance_cols = "correlation", breaks = seq(-3, 3, 0.05), color = colorRampPalette(c("purple3", "black", "yellow2"))(120), annotation_col = pData(target_demoData)[, c("Annotation_initial2", "Conditon2")] ) p

How can I extract the clustering order of the column of my samples? Essentially I have >100 samples and pheatmap nicely clusters them in to 2 broad groups, how can I extract the plotting order for downstream analysis?

When I examine p there does not seem to be this data in the object, thanks

AlexWeinreb commented 4 months ago

As ?pheatmap indicates in the "Value" section, p contains a list with a component tree_row, which is "the clustering of rows as hclust object". You can get more details in the "Value" section of ?hclust, notably:

order a vector giving the permutation of the original observations suitable for plotting, in the sense that a cluster plot using this ordering and matrix merge will not have crossings of the branches.

So the exact order is available with:

p$tree_col$order

or if you want the labels in their clustered order:

p$tree_col$labels[p$tree_col$order]