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)
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