text: user3968: I am using Seurat v2 for professional reasons (I am aware of the availablity of Seurat v3). I am clustering and analysing single cell RNA seq data. How do I add a coloured annotation bar to the heatmap generated by the DoHeatmap function from Seurat v2? I want to be able to demarcate my cluster numbers on the heatmap over a coloured annotation bar. I want to have the an image like this:
   (taken from Satija V3 tutorial).
  My original code:
  library(Seurat)
  DoHeatmap(object = object, genes.use = top20_pos$gene)
  
  produces the following image which is not what I want:
  
  I am aware that DoHeatmap function returns a ggplot2 object but after searching the internet for a long time, I have not been able to find  which geom layer I can add to achive my objective.
  On the other hand, I have tried to generate a separate coloured annotation bar from another package called ComplexHeatmap with the intention of cropping it to fit the cluster demarcations on my heatmap generated with DoHeatmap but this has proven to be very time-consuming and inaccurrate:
  library(ComplexHeatmap)
  
  df = data.frame(type = c(rep("a", 18), rep("b", 3), rep("c", 2), rep("d", 1), rep("e", 1)))
  ha = HeatmapAnnotation(df = df, col = list(type = c("a" =  "blue", "b" = "red", "c" = "green", "d" = "cyan", "e" = "magenta")))
  draw(ha, 1:25)
  
  which produces:
  
  I will be very grateful for any hint on how to achieve my goal automatically and accurately.
  Thank you in advance for your help.
  
  user3631: I am not sure if it's a v3 or also v2 thing, but have you tried by setting group.by and label?
  From ?Seurat::DoHeatmap:
  group.by   A vector of variables to group cells by; pass 'ident' to group by cell identity classes
  label      Label the cell identies above the color bar
  
  If you prefer external tools, you can use pheatmap and setting annotation_col e annotation_colors.
  First define a data.frame (df) containing the data you want in the bar (clusters in your case)
  Description Condition
  liver       Tumour
  lung        Normal
  
  Then you have to assign the colours
  anno <- as.data.frame(df)
  anno_colori <- list(
     Condition = c(Normal = '#FFC000', Tumour = '#0070C0'),
     Description = c(liver = '#D55E00', lung = '#66CC99')
  )
  
  pheatmap(mat,
       annotation_col = anno,
       annotation_colors = anno_colori,
  )
  
  The code above will add 2 bars on the top of your heatmap, one for Condition and another for Description. 
  
original_text: 3968: <p>I am using <strong>Seurat v2</strong> for professional reasons (I am aware of the availablity of <strong>Seurat v3</strong>). I am clustering and analysing single cell RNA seq data. How do I add a coloured annotation bar to the heatmap generated by the <strong>DoHeatmap</strong> function from Seurat v2? I want to be able to demarcate my cluster numbers on the heatmap over a coloured annotation bar. I want to have the an image like this:</p>
  
  <p><a href="https://i.stack.imgur.com/GwS2x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GwS2x.png" alt="this"></a> (taken from <a href="https://satijalab.org/seurat/v3.0/pbmc3k_tutorial.html" rel="nofollow noreferrer">Satija V3 tutorial</a>).</p>
  
  <p>My original code:</p>
  
  <pre><code>library(Seurat)
  DoHeatmap(object = object, genes.use = top20_pos$gene)
  </code></pre>
  
  <p>produces the following image which is not what I want:
  <a href="https://i.stack.imgur.com/74ice.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/74ice.png" alt="enter image description here"></a></p>
  
  <p>I am aware that <strong>DoHeatmap</strong> function returns a <strong>ggplot2 object</strong> but after searching the internet for a long time, I have not been able to find  which <strong>geom</strong> layer I can add to achive my objective.</p>
  
  <p>On the other hand, I have tried to generate a separate coloured annotation bar from another package called <strong>ComplexHeatmap</strong> with the intention of cropping it to fit the cluster demarcations on my heatmap generated with <strong>DoHeatmap</strong> but this has proven to be very time-consuming and inaccurrate:</p>
  
  <pre><code>library(ComplexHeatmap)
  
  df = data.frame(type = c(rep("a", 18), rep("b", 3), rep("c", 2), rep("d", 1), rep("e", 1)))
  ha = HeatmapAnnotation(df = df, col = list(type = c("a" =  "blue", "b" = "red", "c" = "green", "d" = "cyan", "e" = "magenta")))
  draw(ha, 1:25)
  </code></pre>
  
  <p>which produces:
  <a href="https://i.stack.imgur.com/CkKIm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CkKIm.png" alt="enter image description here"></a></p>
  
  <p>I will be very grateful for any hint on how to achieve my goal automatically and accurately.</p>
  
  <p>Thank you in advance for your help.</p>
   3631: <p>I am not sure if it's a v3 or also v2 thing, but have you tried by setting <code>group.by</code> and <code>label</code>?</p>
  
  <p>From <code>?Seurat::DoHeatmap</code>:</p>
  
  <pre><code>group.by   A vector of variables to group cells by; pass 'ident' to group by cell identity classes
  label      Label the cell identies above the color bar
  </code></pre>
  
  <p>If you prefer external tools, you can use <code>pheatmap</code> and setting <code>annotation_col</code> e <code>annotation_colors</code>.</p>
  
  <p>First define a data.frame (df) containing the data you want in the bar (clusters in your case)</p>
  
  <pre><code>Description Condition
  liver       Tumour
  lung        Normal
  </code></pre>
  
  <p>Then you have to assign the colours</p>
  
  <pre><code>anno &lt;- as.data.frame(df)
  anno_colori &lt;- list(
     Condition = c(Normal = '#FFC000', Tumour = '#0070C0'),
     Description = c(liver = '#D55E00', lung = '#66CC99')
  )
  
  pheatmap(mat,
       annotation_col = anno,
       annotation_colors = anno_colori,
  )
  </code></pre>
  
  <p>The code above will add 2 bars on the top of your heatmap, one for <code>Condition</code> and another for <code>Description</code>. </p>
  