Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom visualization showing cell density in an embedding #6

Open
ycl6 opened this issue Mar 8, 2024 · 0 comments
Open

Custom visualization showing cell density in an embedding #6

ycl6 opened this issue Mar 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ycl6
Copy link
Owner

ycl6 commented Mar 8, 2024

A request received recently to show cell density in a TSNE/UMAP plot, will be nice to have a function in scRUtils to produce different variations of this.

    suppressPackageStartupMessages({
        library(scater)
        library(scRNAseq)
    })

    sce <- ZeiselBrainData()
    
    sce <- logNormCounts(sce)
    sce <- runPCA(sce)

    set.seed(1000)
    sce <- runTSNE(sce, perplexity=50, dimred="PCA", n_dimred=20)
    tsne = as.data.frame(reducedDim(sce, "TSNE"))
    colnames(tsne) = c("Dim1","Dim2")
    tsne$tissue = sce$tissue

    # Version 1
    a = ggplot(tsne, aes(x = Dim1, y = Dim2)) +
        geom_density_2d_filled(n = 50) + facet_wrap(vars(tissue), ncol = 1) +
        scale_fill_brewer("Cell\nDensity",palette = "Spectral", direction = -1)

    # Version 2
    b = ggplot(tsne, aes(x = Dim1, y = Dim2)) + geom_point(size = 1, alpha = 0.2) +
        geom_density_2d(aes(color = after_stat(level), linewidth = after_stat(level)), n = 50) + 
        facet_wrap(vars(tissue), ncol = 1) + scale_linewidth("Cell\nDensity", range = c(0.5, 1.5)) +
        scale_color_distiller("Cell\nDensity", palette = "Spectral", direction = -1)

    cowplot::plot_grid(a, b, labels = c('V1', 'V2'))

Created on 2024-03-08 with reprex v2.1.0

@ycl6 ycl6 added the enhancement New feature or request label Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant