2
$\begingroup$

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

$\endgroup$

1 Answer 1

2
$\begingroup$

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<- [email protected]$seurat_clusters

# and here is a sanity check to see if cluster labels are transferred to the "right places"
all(rownames(plot$data) == rownames([email protected]))

LabelClusters(plot, id = "seurat_clusters")

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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