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

getDensity(x, y, ...)

Arguments

x

numeric (equal length as y)

y

numeric

...

additional parameters passed to MASS::kde2d

Value

density values

Author

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
#> Error in eval(expr, envir, enclos): object 'so' not found
df.meta$density1 <- getDensity(df.meta$nCount_RNA, df.meta$percent.mt, n = 100)
#> Error in MASS::kde2d(x, y, ...): object 'df.meta' not found

# 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"))
#> Error in ggplot(., aes(x = nCount_RNA, y = percent.mt, color = density1)): object 'df.meta' not found