Dimensionality reduction

library(tidytof)
library(dplyr)
library(ggplot2)

A useful tool for visualizing the phenotypic relationships between single cells and clusters of cells is dimensionality reduction, a form of unsupervised machine learning used to represent high-dimensional datasets in a smaller number of dimensions.

{tidytof} includes several dimensionality reduction algorithms commonly used by biologists: Principal component analysis (PCA), t-distributed stochastic neighbor embedding (tSNE), and uniform manifold approximation and projection (UMAP). To apply these to a dataset, use tof_reduce_dimensions().

Dimensionality reduction with tof_reduce_dimensions().

Here is an example call to tof_reduce_dimensions() in which we use tSNE to visualize data in {tidytof}’s built-in phenograph_data dataset.

data(phenograph_data)

# perform the dimensionality reduction
phenograph_tsne <-
    phenograph_data |>
    tof_preprocess() |>
    tof_reduce_dimensions(method = "tsne")
#> Loading required namespace: Rtsne

# select only the tsne embedding columns
phenograph_tsne |>
    select(contains("tsne")) |>
    head()
#> # A tibble: 6 × 2
#>    .tsne1 .tsne2
#>     <dbl>  <dbl>
#> 1 -12.0    15.3 
#> 2  -3.37    7.18
#> 3   3.33   35.5 
#> 4   3.41   16.8 
#> 5 -10.7    18.3 
#> 6   0.690  22.7

By default, tof_reduce_dimensions will add reduced-dimension feature embeddings to the input tof_tbl and return the augmented tof_tbl (that is, a tof_tbl with new columns for each embedding dimension) as its result. To return only the features embeddings themselves, set augment to FALSE (as in tof_cluster).

phenograph_data |>
    tof_preprocess() |>
    tof_reduce_dimensions(method = "tsne", augment = FALSE)
#> # A tibble: 3,000 × 2
#>    .tsne1 .tsne2
#>     <dbl>  <dbl>
#>  1  17.3   -1.09
#>  2   8.47 -10.1 
#>  3  27.7  -22.7 
#>  4  16.4  -14.4 
#>  5  19.9   -5.83
#>  6  11.1  -21.4 
#>  7   8.32 -11.5 
#>  8  18.0  -24.4 
#>  9  12.2  -17.5 
#> 10   8.46  -3.67
#> # ℹ 2,990 more rows

Changing the method argument results in different low-dimensional embeddings:

phenograph_data |>
    tof_reduce_dimensions(method = "umap", augment = FALSE)
#> # A tibble: 3,000 × 2
#>    .umap1 .umap2
#>     <dbl>  <dbl>
#>  1   9.95 -2.37 
#>  2   8.75 -2.24 
#>  3   3.79  0.695
#>  4   3.66  2.09 
#>  5   9.91 -2.15 
#>  6   1.33  2.84 
#>  7   9.74 -1.85 
#>  8   3.17  1.26 
#>  9   5.48 -1.08 
#> 10   9.54 -4.72 
#> # ℹ 2,990 more rows

phenograph_data |>
    tof_reduce_dimensions(method = "pca", augment = FALSE)
#> # A tibble: 3,000 × 5
#>       .pc1     .pc2   .pc3    .pc4   .pc5
#>      <dbl>    <dbl>  <dbl>   <dbl>  <dbl>
#>  1 -2.77    1.23    -0.868  0.978   3.49 
#>  2 -0.969  -1.02    -0.787  1.22    0.329
#>  3 -2.36    2.54    -1.95  -0.882  -1.30 
#>  4 -3.68   -0.00565  0.962  0.410   0.788
#>  5 -4.03    2.07    -0.829  1.59    5.39 
#>  6 -2.59   -0.108    1.32  -1.41   -1.24 
#>  7 -1.55   -0.651   -0.233  1.08    0.129
#>  8 -1.18   -0.446    0.134 -0.771  -0.932
#>  9 -2.00   -0.485    0.593 -0.0416 -0.658
#> 10 -0.0356 -0.924   -0.692  1.45    0.270
#> # ℹ 2,990 more rows

Method specifications for tof_reduce_*() functions

tof_reduce_dimensions() provides a high-level API for three lower-level functions: tof_reduce_pca(), tof_reduce_umap(), and tof_reduce_tsne(). The help files for each of these functions provide details about the algorithm-specific method specifications associated with each of these dimensionality reduction approaches. For example, tof_reduce_pca takes the num_comp argument to determine how many principal components should be returned:

# 2 principal components
phenograph_data |>
    tof_reduce_pca(num_comp = 2)
#> # A tibble: 3,000 × 2
#>       .pc1     .pc2
#>      <dbl>    <dbl>
#>  1 -2.77    1.23   
#>  2 -0.969  -1.02   
#>  3 -2.36    2.54   
#>  4 -3.68   -0.00565
#>  5 -4.03    2.07   
#>  6 -2.59   -0.108  
#>  7 -1.55   -0.651  
#>  8 -1.18   -0.446  
#>  9 -2.00   -0.485  
#> 10 -0.0356 -0.924  
#> # ℹ 2,990 more rows
# 3 principal components
phenograph_data |>
    tof_reduce_pca(num_comp = 3)
#> # A tibble: 3,000 × 3
#>       .pc1     .pc2   .pc3
#>      <dbl>    <dbl>  <dbl>
#>  1 -2.77    1.23    -0.868
#>  2 -0.969  -1.02    -0.787
#>  3 -2.36    2.54    -1.95 
#>  4 -3.68   -0.00565  0.962
#>  5 -4.03    2.07    -0.829
#>  6 -2.59   -0.108    1.32 
#>  7 -1.55   -0.651   -0.233
#>  8 -1.18   -0.446    0.134
#>  9 -2.00   -0.485    0.593
#> 10 -0.0356 -0.924   -0.692
#> # ℹ 2,990 more rows

see ?tof_reduce_pca, ?tof_reduce_umap, and ?tof_reduce_tsne for additional details.

Visualization using tof_plot_cells_embedding()

Regardless of the method used, reduced-dimension feature embeddings can be visualized using {ggplot2} (or any graphics package). {tidytof} also provides some helper functions for easily generating dimensionality reduction plots from a tof_tbl or tibble with columns representing embedding dimensions:

# plot the tsne embeddings using color to distinguish between clusters
phenograph_tsne |>
    tof_plot_cells_embedding(
        embedding_cols = contains(".tsne"),
        color_col = phenograph_cluster
    )


# plot the tsne embeddings using color to represent CD11b expression
phenograph_tsne |>
    tof_plot_cells_embedding(
        embedding_cols = contains(".tsne"),
        color_col = cd11b
    ) +
    ggplot2::scale_fill_viridis_c()

Such visualizations can be helpful in qualitatively describing the phenotypic differences between the clusters in a dataset. For example, in the example above, we can see that one of the clusters has high CD11b expression, whereas the others have lower CD11b expression.

Session info

sessionInfo()
#> R version 4.6.1 (2026-06-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] tidyr_1.3.2                 stringr_1.6.0              
#>  [3] HDCytoData_1.33.1           flowCore_2.25.1            
#>  [5] SummarizedExperiment_1.43.0 Biobase_2.73.2             
#>  [7] GenomicRanges_1.65.4        Seqinfo_1.3.2              
#>  [9] IRanges_2.47.5              S4Vectors_0.51.10          
#> [11] MatrixGenerics_1.25.0       matrixStats_1.5.0          
#> [13] ExperimentHub_3.3.2         AnnotationHub_4.3.2        
#> [15] BiocFileCache_3.3.0         dbplyr_2.6.0               
#> [17] BiocGenerics_0.59.12        generics_0.1.4             
#> [19] forcats_1.0.1               ggplot2_4.0.3              
#> [21] dplyr_1.2.1                 tidytof_1.7.0              
#> [23] rmarkdown_2.32             
#> 
#> loaded via a namespace (and not attached):
#>   [1] RColorBrewer_1.1-3   sys_3.4.3            jsonlite_2.0.0      
#>   [4] shape_1.4.6.1        magrittr_2.0.5       farver_2.1.2        
#>   [7] vctrs_0.7.3          memoise_2.0.1        sparsevctrs_0.3.6   
#>  [10] BiocBaseUtils_1.15.1 htmltools_0.5.9      S4Arrays_1.13.2     
#>  [13] curl_8.0.0           SparseArray_1.13.4   sass_0.4.10         
#>  [16] parallelly_1.48.0    bslib_0.12.0         httr2_1.3.0         
#>  [19] lubridate_1.9.5      cachem_1.1.0         buildtools_1.0.0    
#>  [22] igraph_2.3.4         lifecycle_1.0.5      iterators_1.0.14    
#>  [25] pkgconfig_2.0.3      Matrix_1.7-6         R6_2.6.1            
#>  [28] fastmap_1.2.0        future_1.76.0        digest_0.6.39       
#>  [31] AnnotationDbi_1.75.2 RSpectra_0.16-2      RSQLite_3.53.3      
#>  [34] labeling_0.4.3       filelock_1.0.3       cytolib_2.25.0      
#>  [37] yardstick_1.4.0      timechange_0.4.0     httr_1.4.9          
#>  [40] polyclip_1.10-7      abind_1.4-8          compiler_4.6.1      
#>  [43] bit64_4.8.6          withr_3.0.3          doParallel_1.0.17   
#>  [46] S7_0.2.2             viridis_0.6.5        DBI_1.3.0           
#>  [49] ggforce_0.5.0        MASS_7.3-66          lava_1.9.3          
#>  [52] embed_1.2.2          rappdirs_0.3.4       DelayedArray_0.39.8 
#>  [55] tools_4.6.1          otel_0.2.0           future.apply_1.20.2 
#>  [58] nnet_7.3-21          glue_1.8.1           grid_4.6.1          
#>  [61] Rtsne_0.17           recipes_1.4.0        gtable_0.3.6        
#>  [64] tzdb_0.5.0           class_7.3-24         data.table_1.18.6.1 
#>  [67] hms_1.1.4            utf8_1.2.6           tidygraph_1.3.1     
#>  [70] XVector_0.53.0       RcppAnnoy_0.0.23     ggrepel_0.9.8       
#>  [73] BiocVersion_3.24.0   foreach_1.5.2        pillar_1.11.1       
#>  [76] RcppHNSW_0.7.0       splines_4.6.1        tweenr_2.0.3        
#>  [79] lattice_0.23-1       survival_3.8-12      bit_4.6.0           
#>  [82] RProtoBufLib_2.25.0  tidyselect_1.2.1     maketools_1.3.2     
#>  [85] Biostrings_2.81.9    knitr_1.52           gridExtra_2.3.1     
#>  [88] xfun_0.61            graphlayouts_1.2.5   hardhat_1.4.3       
#>  [91] timeDate_4052.112    stringi_1.8.9        yaml_2.3.12         
#>  [94] evaluate_1.0.5       codetools_0.2-20     ggraph_2.2.2        
#>  [97] tibble_3.3.1         BiocManager_1.30.27  cli_3.6.6           
#> [100] uwot_0.2.5           rpart_4.1.27         jquerylib_0.1.4     
#> [103] Rcpp_1.1.2           globals_0.19.1       png_0.1-9           
#> [106] parallel_4.6.1       gower_1.0.2          readr_2.2.0         
#> [109] blob_1.3.0           listenv_1.1.0        glmnet_5.1          
#> [112] viridisLite_0.4.3    ipred_0.9-16         ggridges_0.5.7      
#> [115] scales_1.4.0         prodlim_2026.03.11   crayon_1.5.3        
#> [118] purrr_1.2.2          rlang_1.3.0          KEGGREST_1.53.6