# plotting two heatmaps with the same order of genes

I expected the same pattern but here I am not able to compare the patterns as the order of genes does not seem the same. I mean how I can have two heat maps on which the order of genes are the same so I woulds be able to compare the block of yellow and blue colors in two heat maps.

I have used this code

require("RColorBrewer")
myCol <- colorRampPalette(c("dodgerblue", "black", "yellow"))(100)
myBreaks <- seq(-2, 2, length.out=101)
heat <- t(scale(t(sc_DEGG)))
hr <- hclust(as.dist(1-cor(t(y), method="pearson")), method="complete")
heatmap.2(
heat,
Rowv=as.dendrogram(hr),
Colv=as.dendrogram(hc),
col=myCol,
breaks=myBreaks,
main="Title",
key=T,
keysize=1.0,
scale="none",
density.info="none",
reorderfun=function(d,w) reorder(d, w, agglo.FUN=mean),
trace="none",
cexRow=0.2,
cexCol=0.8,
distfun=function(x) dist(x, method="euclidean"),
hclustfun=function(x) hclust(x, method="ward.D2")
)


I have made a pair of heat maps based on your code in complexheatmap package. The right heat map is lighter than the left one. I don't know which one has been clustered based on the another one. How I can make the right one darker so that both seem more similar?? ComplexHeatmap is built for plotting side-by-side heat maps with the same clustering - you use the + notation, similar to ggplot2. To use the same example data as @b.nota:

library(ComplexHeatmap)
# First matrix
set.seed(2)
m <- replicate(8, rnorm(26))
rownames(m) <- letters[1:26]

# Second matrix
set.seed(3)
m2 <- replicate(8, rnorm(26))
rownames(m2) <- letters[1:26]

Heatmap(m) + Heatmap(m2)


And the results: There are lots of examples in the extensive documentation.

EDIT:

Since you have a few genes with high values in the 'T' matrix, they are washing out the colour scheme for the rest of the heatmap. You could set the max to be lower, and accept that those genes will saturate - the resulting heatmap will be less 'honest' about the data, but might capture what you want to see. To change colour scheme requires a call to the colorRamp2 function of the circlize package.

library(circlize)
mycol <- colorRamp2(c(-2,0,2), c("dodgerblue", "black", "yellow"))
Heatmap(m, col=mycol, cluster_columns = FALSE) +
Heatmap(m2, col=mycol, cluster_columns = FALSE)


This gives: • Great solution!
– benn
Jul 5, 2018 at 14:56

You expect that the order of genes is the same for both heatmaps, but the chance is small that the 4000 genes will exactly be clustered in the same order.

What you can do, is use the order of genes (rows) from one heatmap and create a new heatmap with the other data set. You'll lose the tree of course because it will not be clustering anymore.

Here a small example:

library(gplots)

# First matrix
set.seed(2)
m <- replicate(8, rnorm(26))

rownames(m) <- letters[1:26]

hm <- heatmap.2(
m,
main="m",
key=T,
keysize=1.0,
scale="none",
trace="none",
density.info="none",
Colv = FALSE,
distfun=function(x) dist(x, method="euclidean"),
hclustfun=function(x) hclust(x, method="ward.D2")
)

# Second matrix
set.seed(3)
m2 <- replicate(8, rnorm(26))

rownames(m2) <- letters[1:26]

hm2 <- heatmap.2(
m2,
main="m2",
key=T,
keysize=1.0,
scale="none",
trace="none",
density.info="none",
Colv = FALSE,
distfun=function(x) dist(x, method="euclidean"),
hclustfun=function(x) hclust(x, method="ward.D2")
)

# Second matrix with row order of first
reorder_m2 <- m2[rev(hm\$rowInd),]

hm2_m <- heatmap.2(
reorder_m2,
main="m2 in m's order",
key=T,
keysize=1.0,
scale="none",
trace="none",
density.info="none",
Rowv = FALSE,
Colv = FALSE,
dendrogram="none",
distfun=function(x) dist(x, method="euclidean"),
hclustfun=function(x) hclust(x, method="ward.D2")
)


You have 2 options provided that both datasets have the same genes (rows):

1. Keep the original order of rows in the heatmap. If they’re in the same order in the dataset then they will be in the heatmap. You can suppress reordering with any of the following arguments to heatmap.2

Rowv = NULL

dendrogram = “col” or dendrogram = “none”

reorderfun = function(x) return(x)

1. You can compute the order of the genes (rows) in advance and pass them to the heatmap. The code shown in the question does this already with Rowv=as.dendrogram(hr). To have the same order in a second heatmap, you have to pass the same same dendrogram hr to both heatmaps. This can be computed from either dataset or another set of samples altogether.

Another option is to compute separate dendrograms (from hclust of each dataset) for the samples and combine them using merge. You can combine the datasets with cbind and plot the datasets together as one heatmap (with 2 sets of samples clustered separately) by passing the merged dendrogram to Colv:

#compute same dendrograms separately
hc1 <- hclust(as.dist(t(data1), method="euclidean")), method="complete")
hr2 <- hclust(as.dist(t(data2), method="euclidean")), method="complete")
mergedcols <- merge(as.dendrogram(hr1), as.dendrogram(hr2))
mergeddata <- cbind(data1, data2)
#compute gene dendrogram across all samples
hr <- hclust(as.dist(1-cor(t(mergeddata), method="pearson")), method="complete")
heatmap.2(
mergeddata,
Rowv=as.dendrogram(hr),
Colv=as.dendrogram(mergedcols),
col=myCol,
breaks=myBreaks,
main="Title",
key=T,
keysize=1.0,
scale="none",
density.info="none",
reorderfun=function(d,w) reorder(d, w, agglo.FUN=mean),
trace="none",
cexRow=0.2,
cexCol=0.8,
distfun=function(x) dist(x, method="euclidean"),
hclustfun=function(x) hclust(x, method="ward.D2")
)


You should be able to show the division between these samples with the colsep argument.