Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-27T10:45:04.623Z
Answers.Text: 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
  >
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-27T10:47:34.257Z
Answers.Text: The problem is kind of obvious, did you check wheter `plot$data` contains a column called `seurat_clusters`?
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-08T20:20:20.187Z
Answers.LastActivityDate: 2021-07-10T22:15:12.437Z
Answers.LastEditDate: 2021-07-10T22:15:12.437Z
Answers.UserDisplayName: haci
Body: <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>
  
Tags: <scrnaseq><seurat><10x-genomics>
Title: color by clusters and sampled in Seurat
ThreadText: # 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)
  
  [![enter image description here](https://i.sstatic.net/ENABR.png)](https://i.sstatic.net/ENABR.png)
  
  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")
  
  ```
  
  [![enter image description here](https://i.sstatic.net/A65yx.png)](https://i.sstatic.net/A65yx.png)
  
  > **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`?  
  
  ---
  
  