R语言:单细胞pcoa降维和去批次

#生成随机颜色

> randomColor <- function() {

paste0("#",paste0(sample(c(0:9, letters[1:6]), 6, replace = TRUE),collapse = ""))

}

生成100个随机颜色

> randomColors <- replicate(100,randomColor())

> seurat=readRDS("seurat.rds")#读取数据

#归一化

> all.genes <- rownames(seurat)

> seurat <- ScaleData(seurat, features = all.genes)

PCA降维

> seurat <- Seurat::RunPCA(seurat, features = VariableFeatures(object = seurat))

> seurat <- Seurat::RunTSNE(seurat,dims = 1:20)

> pdf(file = "降维tsne图.pdf",width =7.5,height = 5.5)

> DimPlot(seurat, reduction = "tsne",pt.size = 0.5)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right") #top为图列位置最上方,除此之外还有right、left、bottom(意思同英文)

> dev.off()

> pdf(file = "降维pca图.pdf",width =7.5,height = 5.5)

> DimPlot(seurat, reduction = "pca",pt.size = 0.5)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right")

> dev.off()

> colaa=distinctColorPalette(100)

> pdf(file = "降维tsne单样品分布图.pdf",width =12,height = 10)

> coords <- as.data.frame(seurat@reductions$tsne@cell.embeddings[, c(1, 2)])

> names(coords) <- c("tSNE_1", "tSNE_2")

添加聚类信息

> coordscluster \<- seurat@meta.dataType

绘制 t-SNE 图

> ggplot(coords, aes(x = tSNE_1, y = tSNE_2, color = cluster)) +

geom_point(size = 0.5) +

ggtitle("This is the plot title") +

theme(legend.position = "bottom")

> dev.off()

#harmony 去批次

> seurat <- RunHarmony(seurat, group.by.vars = "Type")

鉴定高变基因(由于去除了存在批次的细胞,高变基因可能会发生改变,因此需要重新鉴定高变基因)

> seurat <- FindVariableFeatures(seurat, selection.method = "vst", nfeatures = 2000)

提取前10的高变基因

> top10 <- head(VariableFeatures(seurat), 10)

展示高变基因

> plot1 <- VariableFeaturePlot(seurat)

> plot1

> plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)

> pdf(file = "去批次后鉴定高变基因.pdf",width =7,height = 6)

> plot2

> dev.off()

#降维可视化

> pdf(file = "harmony去批次pca图.pdf",width =7.5,height = 5.5)

> DimPlot(seurat, reduction = "harmony",pt.size = 0.5)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right")

> dev.off()

> seurat <- Seurat::RunTSNE(seurat,dims = 1:20,reduction ='harmony')

> pdf(file = "去批次后tsne图.pdf",width =7.5,height = 5.5)

> DimPlot(seurat, reduction = "tsne",pt.size = 0.5)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right")

> dev.off()

> pdf(file = "去批次后tsne单样本分布图.pdf",width =12,height = 7.5)

添加聚类信息

> coordscluster \<- seurat@meta.dataType

绘制 t-SNE 图

> ggplot(coords, aes(x = tSNE_1, y = tSNE_2, color = cluster)) +

geom_point(size = 0.5) +

ggtitle("This is the plot title") +

theme(legend.position = "bottom")

> dev.off()

#绘制去批次后tsne单样本分布图

> ggplot(coords, aes(x = tSNE_1, y = tSNE_2, color = cluster)) +

geom_point(size = 0.5) +

ggtitle("This is the plot title") +

theme(legend.position = "bottom")

学习交流

相关推荐
一声沧海笑8 小时前
dplyr、tidyverse和ggplot2初探
信息可视化·数据分析·r语言
waterHBO12 小时前
R语言 基础笔记
开发语言·笔记·r语言
Red Red21 小时前
GEO数据库提取疾病样本和正常样本|GEO数据库区分疾病和正常样本|直接用|生物信息|生信
开发语言·数据库·笔记·学习·r语言·c#·生物信息
邢博士谈科教3 天前
比传统机器学习更先进的深度学习神经网络的二分类建模全流程教程
数据挖掘·r语言·数据可视化
环能jvav大师4 天前
基于R语言的统计分析基础:使用键盘输入数据
开发语言·学习·数据分析·r语言·人机交互
Red Red4 天前
GEO数据的下载和处理|GEO数据转换为Gene symbol|GEO注释文件提取symbol|查看样本标签|查看GEO数据疾病或正常|生物信息基础
数据库·笔记·学习·r语言·生物信息·geo数据库
不是伍壹5 天前
【R语言】删除数据框中所有行中没有大于200的数值的行
开发语言·r语言
hongyanwin5 天前
商业预测 初识R
r语言·预测
maizeman1265 天前
R语言统计分析——用回归做ANOVA
回归·r语言·方差分析·anova·线性模型
武艺晴小朋友你好5 天前
基于scRNA-data,运用pySCENIC寻找细胞群里面活跃的调节子
r语言·数据可视化·r语言-4.2.1