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@[email protected][, c(1, 2)])

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

添加聚类信息

> coordscluster \<- [email protected]Type

绘制 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 \<- [email protected]Type

绘制 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")

学习交流

相关推荐
KY_chenzhao4 小时前
用R语言+随机森林玩转遥感空间预测-基于R语言机器学习遥感数据处理与模型空间预测技术及实际项目案例分析
随机森林·机器学习·r语言·生态·遥感·空间预测
没有梦想的咸鱼185-1037-16635 小时前
【生命周期分析(Life Cycle Assessment: LCA)】基于OpenLCA、GREET、R语言的生命周期评价方法、模型构建及典型案例应用
数据分析·r语言
没有梦想的咸鱼185-1037-16631 天前
全球森林数据如何分析?基于R语言森林生态系统结构、功能与稳定性分析与可视化
开发语言·随机森林·数据分析·r语言
Tiger Z2 天前
R 语言科研绘图 --- 桑基图-汇总
开发语言·r语言·贴图
Expecto03 天前
因子分析——数学原理及R语言代码
算法·r语言·统计学·多元统计分析
人类群星闪耀时4 天前
R语言数据挖掘:从“挖井”到“淘金”
开发语言·数据挖掘·r语言
Jet45055 天前
第100+40步 ChatGPT学习:R语言实现多轮建模
学习·chatgpt·r语言·多轮建模
Tiger Z5 天前
R 语言科研绘图第 45 期 --- 桑基图-和弦
开发语言·r语言·贴图
敢敢のwings6 天前
论文速读《Embodied-R: 基于强化学习激活预训练模型具身空间推理能力》
开发语言·r语言
清同趣科研7 天前
R绘图|3分钟复现瑞士“苏黎世大学”Nature全球地图——基于R包ggplot2+sf等
开发语言·r语言