1 Introduction

Non-linear dimensionality reduction techniques such as t-SNE (Maaten and Hinton 2008) and UMAP (McInnes, Healy, and Melville 2020) produce a low-dimensional embedding that summarises the global structure of high-dimensional data. These techniques can be particularly useful when visualising high-dimensional data in a biological setting. However, these embeddings may not accurately represent the local density of data in the original space, resulting in misleading visualisations where the space given to clusters of data does not represent the fraction of the high dimensional space that they occupy. densvis implements the density-preserving objective function described by (Narayan, Berger, and Cho 2020) which aims to address this deficiency by including a density-preserving term in the t-SNE and UMAP optimisation procedures. This can enable the creation of visualisations that accurately capture differing degrees of transcriptional heterogeneity within different cell subpopulations in scRNAseq experiments, for example.

2 Setting up the data

We will illustrate the use of densvis using simulated data. We will first load the densvis and Rtsne libraries and set a random seed to ensure the t-SNE visualisation is reproducible (note: it is good practice to ensure that a t-SNE embedding is robust by running the algorithm multiple times).

library("densvis")
library("Rtsne")
library("uwot")
library("ggplot2")
theme_set(theme_bw())
set.seed(14)
data <- data.frame(
    x = c(rnorm(1000, 5), rnorm(1000, 0, 0.2)),
    y = c(rnorm(1000, 5), rnorm(1000, 0, 0.2)),
    class = c(rep("Class 1", 1000), rep("Class 2", 1000))
)
ggplot() +
    aes(data[, 1], data[, 2], colour = data$class) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Cluster") +
    ggtitle("Original co-ordinates")

3 Running t-SNE

Density-preserving t-SNE can be generated using the densne function. This function returns a matrix of t-SNE co-ordinates. We set dens_frac (the fraction of optimisation steps that consider the density preservation) and dens_lambda (the weight given to density preservation relative to the standard t-SNE objective) each to 0.5.

fit1 <- densne(data[, 1:2], dens_frac = 0.5, dens_lambda = 0.5)
ggplot() +
    aes(fit1[, 1], fit1[, 2], colour = data$class) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Class") +
    ggtitle("Density-preserving t-SNE") +
    labs(x = "t-SNE component 1", y = "t-SNE component 2")

If we run t-SNE on the same data, we can see that the density-preserving objective better represents the density of the data,

fit2 <- Rtsne(data[, 1:2])
ggplot() +
    aes(fit2$Y[, 1], fit2$Y[, 2], colour = data$class) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Class") +
    ggtitle("Standard t-SNE") +
    labs(x = "t-SNE component 1", y = "t-SNE component 2")

4 Running UMAP

A density-preserving UMAP embedding can be generated using the densmap function. This function returns a matrix of UMAP co-ordinates. As with t-SNE, we set dens_frac (the fraction of optimisation steps that consider the density preservation) and dens_lambda (the weight given to density preservation relative to the standard UMAP objective) each to 0.5.

fit1 <- densmap(data[, 1:2], dens_frac = 0.5, dens_lambda = 0.5)
ggplot() +
    aes(fit1[, 1], fit1[, 2], colour = data$class) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Class") +
    ggtitle("Density-preserving UMAP") +
    labs(x = "UMAP component 1", y = "UMAP component 2")

If we run UMAP on the same data, we can see that the density-preserving objective better represents the density of the data,

fit2 <- umap(data[, 1:2])
ggplot() +
    aes(fit2[, 1], fit2[, 2], colour = data$class) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Class") +
    ggtitle("Standard UMAP") +
    labs(x = "UMAP component 1", y = "UMAP component 2")

Session information

sessionInfo()
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.15-bioc/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [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       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.3.6    uwot_0.1.11      Matrix_1.4-1     Rtsne_0.16      
#> [5] densvis_1.6.1    BiocStyle_2.24.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] reticulate_1.25      tidyselect_1.1.2     xfun_0.31           
#>  [4] bslib_0.3.1          purrr_0.3.4          lattice_0.20-45     
#>  [7] basilisk.utils_1.8.0 colorspace_2.0-3     vctrs_0.4.1         
#> [10] generics_0.1.2       htmltools_0.5.2      yaml_2.3.5          
#> [13] utf8_1.2.2           rlang_1.0.3          jquerylib_0.1.4     
#> [16] pillar_1.7.0         withr_2.5.0          DBI_1.1.3           
#> [19] glue_1.6.2           lifecycle_1.0.1      stringr_1.4.0       
#> [22] munsell_0.5.0        gtable_0.3.0         evaluate_0.15       
#> [25] labeling_0.4.2       knitr_1.39           fastmap_1.1.0       
#> [28] parallel_4.2.0       fansi_1.0.3          highr_0.9           
#> [31] Rcpp_1.0.8.3         scales_1.2.0         filelock_1.0.2      
#> [34] BiocManager_1.30.18  magick_2.7.3         jsonlite_1.8.0      
#> [37] FNN_1.1.3.1          farver_2.1.0         basilisk_1.8.0      
#> [40] dir.expiry_1.4.0     png_0.1-7            digest_0.6.29       
#> [43] stringi_1.7.6        bookdown_0.27        dplyr_1.0.9         
#> [46] rprojroot_2.0.3      grid_4.2.0           here_1.0.1          
#> [49] cli_3.3.0            tools_4.2.0          magrittr_2.0.3      
#> [52] sass_0.4.1           tibble_3.1.7         crayon_1.5.1        
#> [55] pkgconfig_2.0.3      ellipsis_0.3.2       assertthat_0.2.1    
#> [58] rmarkdown_2.14       R6_2.5.1             compiler_4.2.0

Maaten, Laurens van der, and Geoffrey Hinton. 2008. “Visualizing Data Using T-Sne.” Journal of Machine Learning Research 9 (Nov): 2579–2605.

McInnes, Leland, John Healy, and James Melville. 2020. “UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction.” http://arxiv.org/abs/1802.03426.

Narayan, Ashwin, Bonnie Berger, and Hyunghoon Cho. 2020. “Density-Preserving Data Visualization Unveils Dynamic Patterns of Single-Cell Transcriptomic Variability.” bioRxiv. https://doi.org/10.1101/2020.05.12.077776.