<p>There was a <a href="https://github.com/satijalab/seurat/issues/2201" rel="nofollow noreferrer">discussion</a> about this in Seurat developer's github and someone actually made a package to easily add several color bars using some DoHeamap-like function. Check it <a href="https://github.com/elliefewings/DoMultiBarHeatmap" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2021-04-23T12:40:46.987Z
2021-04-23T12:40:46.987Z
mfalco
<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>

CC BY-SA 4.0
2019-07-17T12:59:27.407Z
2019-07-17T19:29:55.013Z
2019-07-17T19:29:55.013Z
fra
<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.sstatic.net/GwS2x.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/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.sstatic.net/74ice.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/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.sstatic.net/CkKIm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/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>

CC BY-SA 4.0
2019-05-09T18:56:31.153Z
@h3ab74 It is a Seurat object which has been normalised, scaled,  PCA analysed, TSNE analysed, clustered and from which differential gene expression has been calculated. All steps have been carried out with Seurat v2 methods.
Charles
CC BY-SA 4.0
2019-05-09T23:12:46.170Z
bc If you can somehow derive the expression matrix being used to create this heatmap from the seurat object, you can use the ColSideColors argument in the `heatmap.2` function, and pass a vector of colors through it to get this representation.. It would be something like `heatmap.2(exrs.mat, ColSideColors = color_array)`
h3ab74
CC BY-SA 4.0
2019-05-19T15:33:56.680Z
If you want a simple solution, update the Seurat object to v3 and make the plot. You don't have to do the whole analysis again using v3, just use `UpdateSeuratObject`.
TimStuart
CC BY-SA 4.0
2019-05-20T12:44:27.290Z
@TimStuart Thank you it worked. But is there a possibility to change the colours used on the heat map? I wish to use the blue and red colours.
Charles
CC BY-SA 4.0
2019-08-18T14:35:34.297Z
You can use annotation_raster() from ggplot2 for this. https://ggplot2.tidyverse.org/reference/annotation_raster.html. If you don't need the separation (white lines in between the groups) this should be pretty straightforward. Otherwise, seurat uses empty cells (NA) to separate the groups. This is a little trickier to implement yourself.
Mack123456
<r><scrnaseq><seurat><ggplot2><heatmap>
How do I add a colour annotation bar to the heatmap generated by DoHeatmap function of Seurat v2?
# How do I add a colour annotation bar to the heatmap generated by DoHeatmap function of Seurat v2?

(Asked by: username_0 on 2019-05-09)

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.

**Tags:** r, scrnaseq, seurat, ggplot2, heatmap
> **Comments:**
> **username_0**: @h3ab74 It is a Seurat object which has been normalised, scaled,  PCA analysed, TSNE analysed, clustered and from which differential gene expression has been calculated. All steps have been carried out with Seurat v2 methods.  
> **username_1**: bc If you can somehow derive the expression matrix being used to create this heatmap from the seurat object, you can use the ColSideColors argument in the `heatmap.2` function, and pass a vector of colors through it to get this representation.. It would be something like `heatmap.2(exrs.mat, ColSideColors = color_array)`  
> **username_2**: If you want a simple solution, update the Seurat object to v3 and make the plot. You don't have to do the whole analysis again using v3, just use `UpdateSeuratObject`.  
> **username_0**: @TimStuart Thank you it worked. But is there a possibility to change the colours used on the heat map? I wish to use the blue and red colours.  
> **username_3**: You can use annotation_raster() from ggplot2 for this. https://ggplot2.tidyverse.org/reference/annotation_raster.html. If you don't need the separation (white lines in between the groups) this should be pretty straightforward. Otherwise, seurat uses empty cells (NA) to separate the groups. This is a little trickier to implement yourself.  

---

## 2 answers

### Answer by username_5 on 2019-07-17. Score: 1

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.


---

### Answer by username_4 on 2021-04-23. Score: 0

There was a [discussion](https://github.com/satijalab/seurat/issues/2201) about this in Seurat developer's github and someone actually made a package to easily add several color bars using some DoHeamap-like function. Check it [here](https://github.com/elliefewings/DoMultiBarHeatmap).


---

