Order of slices in ComplexHeatmap package

2k views Asked by At

I'm using the ComplexHeatmap package in R and split my heatmap by k-mean clustering (rows and columns). Clustering for the rows works fine. For the columns I get a a 4-column cluster (control) and an 8-column cluster (treated), which is good. However, for some heatmaps the control slide is on the right side , for some on the left. I would like to order them uniformely (control left, treated right). When I rearrange the slices with the column_split function, I get 3 clusters instead of 2. How can I fix this? Thank you in advance.

Heatmap(mat, name = "mat", border=TRUE, rect_gp =gpar(col="white", lwd=1),
column_km = 2, row_km = 2, row_km_repeats = 100, column_km_repeats = 100,
show_parent_dend_line = FALSE,
column_split = factor(c(rep("A",8),rep("B",4)), levels = c("B","A")))
2

There are 2 answers

0
Urumpel On

Ok, I came up with an idea, which is not a direct solution.

Instead of using the default k-means partition, I assigned a partition vector to column_split.

pa = cluster::pam(t(mat), k = 2)

Heatmap(mat, name = "mat", border=TRUE, rect_gp =gpar(col="white", lwd=1),
row_km = 2, row_km_repeats = 100, show_parent_dend_line = FALSE,
column_split = paste("pam", pa$clustering))

I would still appreciate other solutions.

0
Y. Sun On

Adding an additional argument to the Heatmap, cluster_column_slices = FALSE, will solve your problem:

Heatmap(mat, name = "mat", border=TRUE, rect_gp =gpar(col="white", lwd=1),
        column_km = 2, row_km = 2, row_km_repeats = 100, column_km_repeats = 100,
        show_parent_dend_line = FALSE, cluster_column_slices = FALSE,
        column_split = factor(c(rep("A",8),rep("B",4)), levels = c("B","A")))

In this case, you can control the order of the slices by setting the categorical variable as the factor. Setting cluster_column_slices to FALSE will stop the auto additional clustering applied to the mean of slices, as stated in the ComplexHeatmap documentation.