getDensity: Get local density (z) of bivariate relationship (x,y)

View source: R/utility_functions.R

getDensityR Documentation

Get local density (z) of bivariate relationship (x,y)

Description

Get local density (z) of bivariate relationship (x,y)

Usage

getDensity(x, y, ...)

Arguments

x

numeric (equal length as y)

y

numeric

...

additional parameters passed to MASS::kde2d

Value

density values

Author(s)

Nicholas Mikolajewicz

Kamil Slowikowski (https://slowkow.com/notes/ggplot2-color-by-density/)

Examples

# get data and compute densities
df.meta <- so@meta.data # so is seurat object
df.meta$density1 <- getDensity(df.meta$nCount_RNA, df.meta$percent.mt, n = 100)

# generate scatter plot with overlayed density values
plt.handle1 <- df.meta %>% ggplot(aes(x = nCount_RNA, y = percent.mt, color = density1)) + geom_point() + theme_miko(legend = T) +
   xlab("UMI/cell") + ylab("Mitochondrial Content (%)") +
   labs(title = paste0("r = ", rho1p, "; rho = ", rho1s)) + scale_color_viridis("Density") +
   theme(legend.position="bottom", legend.key.width=unit(legend.width,"cm"))


NMikolajewicz/scMiko documentation built on June 28, 2023, 1:41 p.m.