0

I am trying to reorder the column order in heatmap.2.

With the code below, I obtain the following heatmap.

# load package
library(Heatplus)
library(gplots)

# create a matrix
m1 <- matrix(data = runif(600, 1, 1000),
             nrow = 100,
             ncol = 6)

# name the columns of the matrix
colnames(m1) <- paste0(rep(c("HIGH", "MEDIUM", "LOW"), each = 2))

# create color palette
my.colors <- colorpanel (64, low = "blue", mid = "white", high = "red")

# create heat map
heatmap.2(m1,
          scale ="row",
          distfun = function(x) dist(x,method = 'euclidean'), 
          hclustfun = hclust.ward, # agglomeration method
          Colv = T,
          trace="none",
          col = my.colors, # my color palette
          main = "how to switch the columns",
          srtCol = 45)# to change angle of the label)

heatmap where the columns need to be reordered

I would like to reorder the columns (i.e. just "swap" the columns) and have columns of the same treatment next to each other. This, of course, only when the column reordering respects the hierarchy of the dendrogram on the top of the figure.

In the figure provided, this would mean move the column "HIGH" in the far left and have it on the far right, next to the other "HIGH" sample.

I'm aware of the function "reorder.dendrogram" (from this post: R - heatmap.2: reorder rows and columns) that can flip the branches, but wasn't able to make it work out.

I think on of the issue is that I'm not able to access (or find) the object plotting the upper dendrogram.

Thanks a lot! Valérian

ValZee
  • 33
  • 1
  • 8
  • Is a manual re-ordering of the columns of `m1` an option? Then you can just disable column clustering using `heatmap.2(x, dendrogram="row")` . `ComplexHeatmap` allows to split the columns by treatment to force all high treatments to be next to each other. – danlooo Sep 10 '21 at 12:31
  • @danlooo: I would like to keep the structure of the upper dendrogram. The aim is just to rearrange the columns of similar treatments as close from each other as possible, with the aim to show that duplicates are clustering together. – ValZee Sep 10 '21 at 12:41
  • " with the aim to show that duplicates are clustering togethe" then you dont need to cluster at all set column cluster as FALSE – PesKchan Sep 10 '21 at 14:18
  • @Peskchan: sorry, I was maybe not accurate enough. I would still like to convey how similar/dissimilar the samples are. I want to respect the hierarchy of the upper dendrogram while re-arranging the columns. As I understand, the horizontal distances are non-informative and the structure of the dendrogram will be respected as long as any column switch does not intercalate in another node. – ValZee Sep 14 '21 at 07:19

0 Answers0