Answers.Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here a small example:</p>
  
  <pre><code>library(gplots)
  
  # First matrix
  set.seed(2)
  m &lt;- replicate(8, rnorm(26))
  
  rownames(m) &lt;- letters[1:26]
  
  hm &lt;- 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 &lt;- replicate(8, rnorm(26))
  
  rownames(m2) &lt;- letters[1:26]
  
  hm2 &lt;- 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 &lt;- m2[rev(hm$rowInd),]
  
  hm2_m &lt;- 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")
  )
  </code></pre>
  
  <p><a href="https://i.sstatic.net/TZeDB.jpg" rel="noreferrer"><img src="https://i.sstatic.net/TZeDB.jpg" alt="m"></a>
  <a href="https://i.sstatic.net/IDXMb.jpg" rel="noreferrer"><img src="https://i.sstatic.net/IDXMb.jpg" alt="m2"></a>
  <a href="https://i.sstatic.net/UrvQD.jpg" rel="noreferrer"><img src="https://i.sstatic.net/UrvQD.jpg" alt="m2_m"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T18:36:32.280Z
Answers.LastActivityDate: 2018-07-04T20:03:12.113Z
Answers.LastEditDate: 2018-07-04T20:03:12.113Z
Answers.UserDisplayName: benn
Answers.Body: <p>You have 2 options provided that both datasets have the same genes (rows):</p>
  
  <ol>
  <li>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 <code>heatmap.2</code></li>
  </ol>
  
  <p><code>Rowv = NULL</code></p>
  
  <p><code>dendrogram = “col”</code> or <code>dendrogram = “none”</code></p>
  
  <p><code>reorderfun = function(x) return(x)</code></p>
  
  <ol start="2">
  <li>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 <code>Rowv=as.dendrogram(hr)</code>. To have the same order in a second heatmap, you have to pass the same same dendrogram <code>hr</code> to both heatmaps. This can be computed from either dataset or another set of samples altogether.</li>
  </ol>
  
  <p>Another option is to compute separate dendrograms (from <code>hclust</code> of each dataset) for the samples and combine them using <code>merge</code>. You can combine the datasets with <code>cbind</code> and plot the datasets together as one heatmap (with 2 sets of samples clustered separately) by passing the merged dendrogram to <code>Colv</code>:</p>
  
  <pre><code>#compute same dendrograms separately
  hc1 &lt;- hclust(as.dist(t(data1), method="euclidean")), method="complete")
  hr2 &lt;- hclust(as.dist(t(data2), method="euclidean")), method="complete")
  mergedcols &lt;- merge(as.dendrogram(hr1), as.dendrogram(hr2))
  mergeddata &lt;- cbind(data1, data2)
  #compute gene dendrogram across all samples
  hr &lt;- 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")
  )
  </code></pre>
  
  <p>You should be able to show the division between these samples with the <code>colsep</code> argument.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-08T08:56:10.713Z
Answers.LastActivityDate: 2018-07-08T08:56:10.713Z
Answers.UserDisplayName: Tom Kelly ケリー・トム
Answers.Body: <p>ComplexHeatmap is built for plotting side-by-side heat maps with the same clustering - you use the <code>+</code> notation, similar to ggplot2. To use the same example data as <a href="https://bioinformatics.stackexchange.com/users/939/b-nota">@b.nota</a>:</p>
  <pre><code>library(ComplexHeatmap)
  # First matrix
  set.seed(2)
  m &lt;- replicate(8, rnorm(26))
  rownames(m) &lt;- letters[1:26]
  
  # Second matrix
  set.seed(3)
  m2 &lt;- replicate(8, rnorm(26))
  rownames(m2) &lt;- letters[1:26]
  
  Heatmap(m) + Heatmap(m2)
  </code></pre>
  <p>And the results:</p>
  <p><a href="https://i.sstatic.net/L3Ju8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/L3Ju8.png" alt="ComplexHeatmap Results" /></a></p>
  <p>There are lots of examples in the <a href="https://www.bioconductor.org/packages/release/bioc/html/ComplexHeatmap.html" rel="nofollow noreferrer">extensive documentation</a>.</p>
  <p>EDIT:</p>
  <p>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 <code>colorRamp2</code> function of the <code>circlize</code> package.</p>
  <pre><code>library(circlize)
  mycol &lt;- colorRamp2(c(-2,0,2), c(&quot;dodgerblue&quot;, &quot;black&quot;, &quot;yellow&quot;))
  Heatmap(m, col=mycol, cluster_columns = FALSE) + 
      Heatmap(m2, col=mycol, cluster_columns = FALSE)
  </code></pre>
  <p>This gives:</p>
  <p><a href="https://i.sstatic.net/JwcMy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JwcMy.png" alt="enter image description here" /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-05T14:56:19.467Z
Answers.Text: Great solution!
Answers.UserDisplayName: benn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-05T14:40:01.943Z
Answers.LastActivityDate: 2021-05-23T12:25:06.370Z
Answers.LastEditDate: 2021-05-23T12:25:06.370Z
Answers.UserDisplayName: sjcockell
Body: <p>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.</p>
  <p>I have used this code</p>
  <pre><code>require(&quot;RColorBrewer&quot;)
  myCol &lt;- colorRampPalette(c(&quot;dodgerblue&quot;, &quot;black&quot;, &quot;yellow&quot;))(100)
  myBreaks &lt;- seq(-2, 2, length.out=101)
  heat &lt;- t(scale(t(sc_DEGG)))
  hr &lt;- hclust(as.dist(1-cor(t(y), method=&quot;pearson&quot;)), method=&quot;complete&quot;)
  heatmap.2(
      heat, 
      Rowv=as.dendrogram(hr), 
      Colv=as.dendrogram(hc), 
      col=myCol, 
      breaks=myBreaks, 
      main=&quot;Title&quot;, 
      key=T, 
      keysize=1.0, 
      scale=&quot;none&quot;, 
      density.info=&quot;none&quot;, 
      reorderfun=function(d,w) reorder(d, w, agglo.FUN=mean), 
      trace=&quot;none&quot;, 
      cexRow=0.2, 
      cexCol=0.8, 
      distfun=function(x) dist(x, method=&quot;euclidean&quot;), 
      hclustfun=function(x) hclust(x, method=&quot;ward.D2&quot;)
  )
  </code></pre>
  <p>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??</p>
  <p><a href="https://i.sstatic.net/j6GG6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/j6GG6.png" alt="complexheatmap with different brightness" /></a></p>
  
Tags: <r><heatmap>
Title: plotting two heatmaps with the same order of genes
ThreadText: # plotting two heatmaps with the same order of genes
  
  (Asked by: username_0 on 2018-07-04)
  
  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 with different brightness](https://i.sstatic.net/j6GG6.png)](https://i.sstatic.net/j6GG6.png)
  
  **Tags:** r, heatmap
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2018-07-05. Score: 11 | Accepted answer
  
  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](https://bioinformatics.stackexchange.com/users/939/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:
  
  [![ComplexHeatmap Results](https://i.sstatic.net/L3Ju8.png)](https://i.sstatic.net/L3Ju8.png)
  
  There are lots of examples in the [extensive documentation](https://www.bioconductor.org/packages/release/bioc/html/ComplexHeatmap.html).
  
  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:
  
  [![enter image description here](https://i.sstatic.net/JwcMy.png)](https://i.sstatic.net/JwcMy.png)
  
  > **Comments:**
  > **username_1**: Great solution!  
  
  ---
  
  ### Answer by username_1 on 2018-07-04. Score: 6
  
  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")
  )
  
  ```
  
  [![m](https://i.sstatic.net/TZeDB.jpg)](https://i.sstatic.net/TZeDB.jpg)
  [![m2](https://i.sstatic.net/IDXMb.jpg)](https://i.sstatic.net/IDXMb.jpg)
  [![m2_m](https://i.sstatic.net/UrvQD.jpg)](https://i.sstatic.net/UrvQD.jpg)
  
  
  ---
  
  ### Answer by username_2 on 2018-07-08. Score: 2
  
  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)`
  
  2. 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.
  
  
  ---
  
  