Using the row order of one pheatmap to set the order of another. How to do it?
Entering edit mode
5.0 years ago
achamess ▴ 60

Hi. I'm using pheatmap to create heatmaps. I've made a heatmap and used the clustering option (method = correlation) and the resulting heatmap has a certain row order based on the clustering. I want to make another heatmap using pheatmap that then has the same row order as the first one. Is there a way to specifically set the order of a second pheatmap based on the first?

I've been trying using the tree_row$order argument on the pheatmap object that I create. I think I can use this to set the order of the other, but then I don't know how to get pheatmap to display/write to file the changed pheatmap object.

Here's an example of some code:

create a pheatmap

hm_1<-pheatmap(mat1, cluster_rows=TRUE) hm_2<-pheatmap(mat1, cluster_rows=FALSE)

get the row order of another pheatmap and set the first to the second

hm_2$tree_row$order <-hm_1$tree_row$order

This works, but then I have this hm_2 object. How do I get it to display the resulting map again without calling the pheatmap argument?

And is there a batter way to do this?

RNA-Seq pheatmap R • 9.1k views
Entering edit mode
3.8 years ago

you can reorder your second data based on the order of the first heatmap (where clustering has been applied) in my case this was useful to display non-transformed values while plotting the heatmap based on transformed values

1) create the hm pheatmap using transformed and normalized data 2) extract row and col orders from it as you suggested

# capture order for rows from first hm row.order = ph$tree_row$order col.order = ph$tree_col$order

3) reorder the non-transformed data based on the order of the data in the heatmap extracted above = raw[row.order, col.order]

4) plot new pheatmap using the same data but overlay numbers from the raw matrix instead of the transf+norm numbers ..., ...

Hope this helps and can be recycled!

Entering edit mode
2.3 years ago
Ram 34k

Revisiting this topic so the answer is more current, I highly recommend using ComplexHeatmap. Its manual is comprehensive and covers a ton of options. The developer is active and responsive.

Carrying over row order between heatmaps can be done with

ro_1 <- row_order(heatmap_1)
heatmap_2 <- Heatmap(..., row_order = ro_1, ...)

Login before adding your answer.

Traffic: 2095 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6