单细胞数据分析中实现 ElbowPlot 函数
前期分析参考:https://www.jianshu.com/p/4f7aeae81ef1
001、
library(dplyr) library(Seurat) library(patchwork) pbmc.data <- Read10X(data.dir = "C:/Users/75377/Desktop/r_test2/hg19") pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200) pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-") VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3) plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt") plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA") plot1 + plot2 pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5) pbmc <- NormalizeData(pbmc) pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000) top10 <- head(VariableFeatures(pbmc), 10) top10 plot1 <- VariableFeaturePlot(pbmc) plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE) plot1 plot1 all.genes <- rownames(pbmc) pbmc <- ScaleData(pbmc, features = all.genes) pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc)) print(pbmc[["pca"]], dims = 1:5, nfeatures = 5) VizDimLoadings(pbmc, dims = 1:2, reduction = "pca") DimPlot(pbmc, reduction = "pca") DimHeatmap(pbmc, dims = 1, cells = 500, balanced = TRUE) DimHeatmap(pbmc, dims = 1:15, cells = 500, balanced = TRUE) pbmc <- JackStraw(pbmc, num.replicate = 100) pbmc <- ScoreJackStraw(pbmc, dims = 1:20)
002、ElbowPlot 函数的实现
00a、使用plot函数
dat <- pbmc[["pca"]]@stdev[1:20] ## 绘图数据 dat dat <- data.frame(a = 1:20, b = dat) plot(dat$a, dat$b) ## 绘图
00b、使用ggplot2
dat <- pbmc[["pca"]]@stdev[1:20] dat dat <- data.frame(a = 1:20, b = dat) ## 绘图数据 library(ggplot2) library(cowplot) ggplot(data = dat) + ## 绘图 geom_point(mapping = aes_string(x = 'a', y = 'b')) + labs(x = "PC", y = "stdev_top20") + theme_cowplot()