title: 给UMAP图添加密度等高线 tags: [] id: '1287' categories:
library(Seurat)
library(SeuratData)
library(Matrix)
library(plyr)
library(dplyr)
library(patchwork)
library(purrr)
library(ggplot2)
library(reshape2)
options(repr.plot.width=12, repr.plot.height=12)
options(ggrepel.max.overlaps = Inf)
# pbmc <- pbmc3k
# pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
# pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
# pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
# pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
# pbmc <- ScaleData(pbmc, features = rownames(pbmc))
# pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
# pbmc <- FindNeighbors(pbmc, dims = 1:10)
# pbmc <- FindClusters(pbmc, resolution = 0.5)
# pbmc <- RunUMAP(pbmc, dims = 1:10)
# saveRDS(pbmc, 'pbmc.rds')
pbmc <- readRDS('pbmc.rds')
f_umap_density2d <- function(umapdata){
ret = ggplot() + geom_point(data = umapdata,aes(x=UMAP_1, y=UMAP_2, colour = ident))
for (o_ident in levels(umapdata$ident)){
tmpdata = subset(umapdata, ident == o_ident)
ret = ret + stat_density2d(data = tmpdata, mapping = aes(x=UMAP_1, y=UMAP_2))
}
ret
}
f_umap_density2d_ <- function(sObject){
p_umap <- DimPlot(sObject, reduction = "umap")
f_umap_density2d(p_umap$data) + p_umap$theme
}
f_umap_density2d_(pbmc)