<p>Here is a solution that makes use of <code>LabelClusters()</code> from <code>Seurat</code>:</p>
<pre><code># creating a plot and assigning it to the &quot;plot&quot; variable
# coloring by sample id and turning labels off
plot &lt;- 
DimPlot(
  pbmc,
  reduction = &quot;umap&quot;,
  group.by = &quot;some_random_sample_id&quot;,
  label = FALSE,
)

# here is the trick, adding cluster labels to the &quot;data&quot; variable of the &quot;plot&quot;
# without this, the &quot;plot&quot; object would not have the clustering information but only embeddings and sample id
plot<span class="math-container">$data$</span>seurat_clusters&lt;- pbmc@meta.data$seurat_clusters

# and here is a sanity check to see if cluster labels are transferred to the &quot;right places&quot;
all(rownames(plot$data) == rownames(pbmc@meta.data))

LabelClusters(plot, id = &quot;seurat_clusters&quot;)
</code></pre>
<p><a href="https://i.sstatic.net/A65yx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A65yx.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-07-27T10:45:04.623Z
Sorry I get this error  > LabelClusters(plot, id = "seurat_clusters")
Error in LabelClusters(plot, id = "seurat_clusters") : 
  Cannot find variable seurat_clusters in plotting data
>
Zizogolu
CC BY-SA 4.0
2021-07-27T10:47:34.257Z
The problem is kind of obvious, did you check wheter `plot$data` contains a column called `seurat_clusters`?
haci
CC BY-SA 4.0
2021-07-08T20:20:20.187Z
2021-07-10T22:15:12.437Z
2021-07-10T22:15:12.437Z
haci
<p>I have a Seurat object</p>
<p>I want to have both cluster numbers and coloured cells by sample names like this figure (from a Nature paper)</p>
<p><a href="https://i.sstatic.net/ENABR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ENABR.png" alt="enter image description here" /></a></p>
<p>I have tried <code>group.by</code> argument in Seurat but only samples come not cluster numbers like below</p>
<pre><code>&gt; DimPlot(pbmc, reduction = &quot;umap&quot;,group.by = &quot;samples&quot;)
&gt;
</code></pre>
<p>[![enter image description here][2]][2]</p>
<p>Any help that to get something like the first figure? Having cluster numbers and coloured by sample names please</p>

<scrnaseq><seurat><10x-genomics>
color by clusters and sampled in Seurat
# color by clusters and sampled in Seurat

(Asked by: username_0 on 2021-07-08)

I have a Seurat object
I want to have both cluster numbers and coloured cells by sample names like this figure (from a Nature paper)

I have tried group.by argument in Seurat but only samples come not cluster numbers like below
> DimPlot(pbmc, reduction = "umap",group.by = "samples")
>

[![enter image description here][2]][2]
Any help that to get something like the first figure? Having cluster numbers and coloured by sample names please

**Tags:** scrnaseq, seurat, 10x-genomics

---

## 1 answers

### Answer by username_1 on 2021-07-08. Score: 2 | Accepted answer

Here is a solution that makes use of LabelClusters() from Seurat:
# creating a plot and assigning it to the "plot" variable
# coloring by sample id and turning labels off
plot <- 
DimPlot(
  pbmc,
  reduction = "umap",
  group.by = "some_random_sample_id",
  label = FALSE,
)

# here is the trick, adding cluster labels to the "data" variable of the "plot"
# without this, the "plot" object would not have the clustering information but only embeddings and sample id
plot$data$seurat_clusters<- pbmc@meta.data$seurat_clusters

# and here is a sanity check to see if cluster labels are transferred to the "right places"
all(rownames(plot$data) == rownames(pbmc@meta.data))

LabelClusters(plot, id = "seurat_clusters")

> **Comments:**
> **username_0**: Sorry I get this error  > LabelClusters(plot, id = "seurat_clusters") Error in LabelClusters(plot, id = "seurat_clusters") :    Cannot find variable seurat_clusters in plotting data >  
> **username_1**: The problem is kind of obvious, did you check wheter `plot$data` contains a column called `seurat_clusters`?  

---

