Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Choose PCA dimensions and compare layouts

Cluster 3,948 human peripheral blood mononuclear cells (PBMCs) from the 10x Genomics 5k PBMC dataset on 10 and 30 principal components (PCs), compare the clusters with the 15-PC clusters of the PBMC analysis, and draw the 15-PC neighbor graph with three layout methods. On 10 PCs, Leiden clustering still finds 12 clusters, but two small populations lose theirs: the 25 CD16 monocytes join half of the CD14 monocytes, and a group of 29 NK cells joins a T-cell cluster. On 30 PCs, the CD16 monocytes keep their own cluster, the 29 NK cells join the other NK cluster, and most other changes move cells between T-cell clusters. Against the 15-PC clusters, the adjusted Rand index is 0.82 on 30 PCs and 0.62 on 10 PCs. UMAP, densMAP and t-SNE arrange the large clusters alike but differ in how densely they pack them and where they put the small ones.

CyteArc connects each cell to its nearest neighbors in PC space and clusters the resulting graph, so the number of PCs changes the neighbors, the graph and the clusters. A layout method such as UMAP only draws that graph in two dimensions, so its settings change the picture and leave the clusters as they are.

Open the prepared analysis

Started: 2026-11-14T15:42:33.643534Z | Wall time: 10.313 s

Open the store and its saved analysis, a PipelineRun labeled rna. The run maps each output name, such as normalized or umap, to an ArtifactRef, a reference to a saved result. Later calls take these references as input, and a call whose inputs and settings match a saved result reuses it. The run’s report records the settings that the run was called with:

cells 3948 variable genes 500 principal components 15 neighbors per cell 11 Leiden resolution 0.5 Name: saved run, dtype: object

Started: 2026-11-14T15:42:43.958960Z | Wall time: 2.792 s

The saved run uses 15 PCs, fewer than the dims=21 default of ds.reduction.pca and ds.pipeline.rna. The comparisons below keep the run’s cells, genes and normalization and change one setting at a time. Draw the saved clusters on the saved UMAP:

UMAP of the 3,948 PBMCs colored by the 12 saved Leiden clusters, with each cluster's number on the data. T-cell clusters 1, 3, 4 and 8 and NK cluster 5 form a connected group on the left, and the small NK cluster 9 sits apart, beside the lower end of cluster 3; CD14 monocyte cluster 2 forms an island on the right with cluster 10 at its tip; B-cell clusters 6 and 7 sit at the top; and the small clusters 11 and 12 lie apart above the monocytes.

Started: 2026-11-14T15:42:46.752857Z | Wall time: 0.374 s

The PBMC analysis names the clusters from marker genes: T cells in clusters 1, 3, 4 and 8, CD14 monocytes in 2, NK cells in 5 and 9, B cells in 6 and 7, CD16 monocytes in 10, pDC-like cells in 11 and platelets in 12. On the UMAP, NK cluster 9 sits beside the lower end of T-cell cluster 3, and cluster 10 at the tip of the CD14 monocytes.

Fit PCA with 10 and 30 components

Build two more clusterings from the saved normalized values, one on 10 PCs and one on 30. Each build fits PCA, indexes the PCA coordinates for an approximate nearest-neighbor search, connects each cell to its 11 nearest neighbors in a weighted graph, and clusters the graph with Leiden Traag et al., 2019 at resolution 0.5, as the saved run did. The 15-PC fit and clusters come from the saved run, so the three clusterings differ only in their number of components. show_elbow_plot=True plots the share of the variance that each of the 30 components explains and marks the elbow, where the curve flattens:

Line plot of the percentage of variance explained by each of 30 principal components, numbered 1 to 30 on the horizontal axis. The share falls from 13.6% for the first component through 7.8%, 5.8% and 4.0% to 1.5% for the fifth, then declines slowly to 0.3%; a red vertical line marks the detected elbow at the fifth component.

Started: 2026-11-14T15:42:47.128922Z | Wall time: 70.990 s

The elbow stands at the fifth component, after which the curve flattens. PCA of 500 genes is exact (the methods page describes the solver for wider inputs), so the 30-component fit starts with the same components as the 10- and 15-component fits, and one table gives the variance shares of all three:

% of variancecumulative %
component
113.613.6
27.821.4
35.827.3
44.031.3
51.532.7
100.737.5
150.439.9
300.345.1

Started: 2026-11-14T15:43:58.121459Z | Wall time: 0.018 s

The first four components explain 31.3% of the variance of the 500 scaled genes, and each later component adds 1.5% or less: the bend that the elbow marks. Whether the small populations need the components after the elbow shows only in the clusterings.

Measure how much the clusterings agree

The adjusted Rand index compares two clusterings of the same cells regardless of how their clusters are numbered. It is 1 for identical clusterings and near 0 for clusterings that agree no more than chance:

10 vs 15 PCs 0.62 10 vs 30 PCs 0.63 15 vs 30 PCs 0.82 Name: adjusted Rand index, dtype: float64

Started: 2026-11-14T15:43:58.140719Z | Wall time: 0.070 s

Raising the number of PCs from 15 to 30 changes the clusters less (0.82) than lowering it to 10 (0.62), and the 10- and 30-PC clusterings agree at 0.63. To see which cells move, ds.clusters.relabel_by_overlap names each cluster after the 15-PC cluster that contributes most of its cells; the saved clusters, named after themselves, become 1a, 2a and so on. A letter tells apart clusters with the same main source, such as 1a and 1b. A 15-PC cluster that is the main source of no cluster appears after a hyphen, in the name of the cluster that takes most of its cells: 3-9a comes mostly from cluster 3 and takes most of cluster 9. Each clustering is named on its own, so 1b on 10 PCs and 1b on 30 PCs are different clusters. Count how the cells of each 15-PC cluster spread over the 10-PC clusters:

10 PCs1a1b2-10b2a3-9a4a5a6a7a8a11a12a
15 PCs
169351158
2343361
333218016
41039425
51282
6124411
71238
827139
929
1025
1115
1212

Started: 2026-11-14T15:43:58.212862Z | Wall time: 0.192 s

Leiden finds 12 clusters on 10 PCs too, but not the same 12. The 10-PC clustering splits T-cell cluster 1 between 1a (693 of its cells) and 1b (511), and the CD14 monocytes of cluster 2 between 2a (361) and 2-10b (343). Two small populations lose their clusters: all 25 CD16 monocytes of cluster 10 join 2-10b, and all 29 NK cells of cluster 9 join 332 cells of T-cell cluster 3 in 3-9a, while 180 other cells of cluster 3 join cluster 4 in 4a. Size alone does not decide which populations keep a cluster: the 15 pDC-like cells and 12 platelets, smaller still, keep theirs. B-cell clusters 6 and 7 and NK cluster 5 also keep nearly all their cells. Count the cells again for 30 PCs:

30 PCs1a1b2a3a4a5-9a6a7a8a10a11a12a
15 PCs
111691714125
27022
313381818
41033701
52281
6256
72237
8271138
929
10223
1115
1212

Started: 2026-11-14T15:43:58.406465Z | Wall time: 0.018 s

On 30 PCs, 23 of the 25 CD16 monocytes keep a cluster of their own, 10a, and the 29 cells of cluster 9 join the other NK cluster in 5-9a. Most other changes fall among the T cells: 181 cells of cluster 3 join cluster 4 in 4a, as 180 did on 10 PCs; 103 cells of cluster 4 and 27 of cluster 8 join cluster 1; and 17 cells of cluster 1 form a cluster of their own, 1b. Going from 10 to 15 components adds little variance, from 37.5% to 39.9%, yet it separates the CD16 monocytes and the NK cells of cluster 9 from the clusters they join on 10 PCs: components 11 to 15, well past the elbow, carry what sets these two populations apart.

Compare the clusterings on the saved UMAP

Draw the three clusterings on the saved UMAP, so that only the cluster labels change between panels. Clusters named 1a to 12a keep the colors of saved clusters 1 to 12 from the other figures, and 1b, 2-10b, 3-9a and 5-9a get new colors:

Three panels of the saved PBMC UMAP colored by Leiden clusters built on 10, 15 and 30 principal components, each cluster labeled on the data with a name built from the 15-PC clusters that supply its cells; the 15-PC panel shows the saved clusters as 1a to 12a. On 10 PCs, cluster 1's area splits into 1a above and a pink 1b below, the monocyte island splits into a light blue 2a and a gray 2-10b that includes cluster 10's tip, and an olive 3-9a covers the lower part of cluster 3 and the small group of cluster 9. On 30 PCs, the tip stays 10a, a cyan 5-9a covers NK cluster 5 and the group of cluster 9, and 17 cells scattered through the left half of cluster 1 form 1b, visible as a few pink dots. On both, 4a extends over the upper part of cluster 3.

Started: 2026-11-14T15:43:58.425916Z | Wall time: 0.990 s

On the saved UMAP, the 10-PC clusters cut cluster 1 into an upper part, 1a, and a lower part, 1b, and the monocyte island into a lower part, 2a, and an upper part, 2-10b, that includes cluster 10’s tip. On both 10 and 30 PCs, the cells of cluster 3 that join 4a are its upper part, next to cluster 4. The 17 cells of the 30-PC cluster 1b lie scattered through the left half of cluster 1 rather than in one patch.

Change how tightly UMAP packs cells

UMAP places cells so that neighbors in the graph lie close together. min_dist sets how close the layout lets cells sit, relative to spread, which sets the overall scale of distances. Every layout from here on draws the saved 15-PC graph from the saved starting positions. Read the saved settings from the UMAP artifact, which records the parameters that made it, then lay out the graph with min_dist=0.1:

Saved UMAP: min_dist=1.0, spread=2.0

Started: 2026-11-14T15:43:59.418473Z | Wall time: 5.225 s

Two UMAPs of the same 15-PC graph colored by the 12 saved clusters, with each cluster's number on the data: min_dist 1 on the left and 0.1 on the right. With 0.1, every cluster contracts into a denser patch with wider gaps between groups: cluster 8 detaches from cluster 1, clusters 6 and 7 form separate islands, T-cell clusters 4 and 3 form a narrow chain below cluster 1, NK cluster 5 sits apart at the bottom with cluster 9 at its edge, and the monocytes form a narrow band, with cluster 10 a separate speck above it.

Started: 2026-11-14T15:44:04.645157Z | Wall time: 0.542 s

With min_dist=0.1 instead of the saved 1.0, every cluster contracts into a denser patch and the gaps between groups widen: cluster 8 detaches from cluster 1, B-cell clusters 6 and 7 separate, NK cluster 5 separates from T-cell cluster 3, and the small cluster 9 moves from beside cluster 3 to the edge of cluster 5. The graph and the clusters are the same in both panels, so the wider gaps make groups look more distinct without any change in how the cells are connected.

Compare UMAP, densMAP and t-SNE

densMAP adds a term to the UMAP objective that ties each cell’s spread in the layout to the distances to its neighbors in PCA space, so dense regions stay dense and cells with distant neighbors spread out.

Started: 2026-11-14T15:44:05.188762Z | Wall time: 6.790 s

CyteArc’s t-SNE lays out the same connectivity graph, rather than computing neighbors of its own from the PCA coordinates. Computing a new t-SNE layout needs the optional sgtsnepi package from the tsne extra (Optional t-SNE); reopening a saved one does not.

Started: 2026-11-14T15:44:11.981101Z | Wall time: 1.277 s

Draw the three layouts with the saved clusters:

UMAP, densMAP and t-SNE layouts of the same 15-PC graph colored by the 12 saved clusters, with each cluster's number on the data. All three place T-cell clusters 1, 4 and 3 next to each other, NK cluster 5 below them, B-cell clusters 6 and 7 together at the top and the monocytes to the right. densMAP packs the large clusters into a small area and scatters single cells, most visibly the platelets of cluster 12, across the empty space. t-SNE spreads the large clusters into evenly filled areas, draws clusters 9, 10, 11 and 12 as small dots, and places cluster 9 at the edge of NK cluster 5, below cluster 10.

Started: 2026-11-14T15:44:13.260444Z | Wall time: 0.905 s

All three layouts arrange the large clusters alike: T-cell clusters 1, 4 and 3 border each other with cluster 8 at the edge of cluster 1, NK cluster 5 lies below cluster 3, B-cell clusters 6 and 7 sit together, and the CD14 monocytes form a group of their own. The layouts differ in density and in where the small clusters go. densMAP packs the large clusters tightly and spreads cells whose neighbors are far away in PCA space: the platelets of cluster 12 scatter across the empty part of the panel. t-SNE spreads the large clusters into evenly filled areas and shrinks the small clusters to dots. Cluster 9 moves between the same two populations in the layouts as in the clusterings: it joins T-cell cluster 3 on 10 PCs and NK cluster 5 on 30 PCs, and it sits beside cluster 3 in UMAP and densMAP but at the edge of cluster 5 in t-SNE and in UMAP with min_dist=0.1. A small cluster’s place in one layout can therefore show only one of the populations it resembles.

Continue with the graph and the clusters

Build the neighbor graph one stage at a time to check it and compare neighbor counts, then compare Leiden resolutions and the Paris hierarchy on the same cells.

References
  1. Traag, V. A., Waltman, L., & van Eck, N. J. (2019). From Louvain to Leiden: guaranteeing well-connected communities. Scientific Reports, 9(1). 10.1038/s41598-019-41695-z