This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to regroup columns in R using heatmap.2 or pheatmap ?

Using following heatmap.2 command

heatmap.2(highly_variable_lcpm,labRow = y2$genes$genes, Rowv = TRUE, col=rev(morecols(20)),trace="none",tracecol = "black",main="Top 20 most variable genes across samples",cex.main=0.6, ColSideColors=group.col,scale="row",margins=c(10,9))

I got the following graph ![heatmap][1][1]: https://ibb.co/iiN257 The colors in column header indicate similar group which are 5 different colors (groups). How to make the samples in each group together so as to show group-wise variation (which is supervised clustering). Thanks

edger heatmap.2 pheatmap

If your input samples are in the correct order, use dendrogram="row" to turn off the column-wise clustering

or, if your input matrix is ordered try Rowv=False

The row is not in order so adding

Colv=FALSE, dendrogram="row"

did not really help . E.g. my samples are as A A A B B B A A A C C C N N N. I need to find a way to bring all 6 "A" together.

Have you considered then reordering them

That was my question , how ?

Re-order them in your actual data-matrix, i.e., before you even apply the heatmap.2 function, and then use Colv and dendrogram as per Buffo

Something like:

sample.order <- A A A B B B A A A C C C N N N *pseudocode
highly_variable_lcpm.ordered <- highly_variable_lcpm[,sample.order]

heatmap.2(highly_variable_lcpm.ordered, ...)

1 answer

Reorder the matrix by column names:

data <- data[ , order( colnames( data ) ) ]

Then plot the heatmap with Colv=FALSE, dendrogram="row".

I wouldn't try to force the groupings, though, if your treatments are not clustering, this reflects the structure of the data and is important to the interpretation of the results.

Log in to answer this question.