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

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


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 <- 
  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

  • $\begingroup$ 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 > $\endgroup$
    – Exhausted
    Jul 27 at 10:45
  • $\begingroup$ The problem is kind of obvious, did you check wheter plot$data contains a column called seurat_clusters? $\endgroup$
    – haci
    Jul 27 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.