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

学习交流

相关推荐
善木科研11 小时前
读文献先读图:GO弦图怎么看?
机器学习·数据分析·r语言
Tiger Z12 小时前
R 语言科研绘图第 55 期 --- 网络图-聚类
开发语言·r语言·贴图
十三画者18 小时前
【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习
python·机器学习·数据挖掘·数据分析·r语言·数据可视化
lishaoan773 天前
实验设计与分析(第6版,Montgomery著,傅珏生译) 第10章拟合回归模型10.9节思考题10.12 R语言解题
回归·r语言·线性回归·残差分析·实验设计与数据分析·回归显著性
南瓜胖胖3 天前
【R语言编程绘图-mlbench】
开发语言·机器学习·r语言
天桥下的卖艺者3 天前
R语言使用随机过采样(Random Oversampling)平衡数据集
开发语言·r语言
Biomamba生信基地4 天前
R语言基础| 创建数据集
开发语言·r语言·生信·医药
lishaoan774 天前
实验设计与分析(第6版,Montgomery)第5章析因设计引导5.7节思考题5.19 R语言解题
r语言·方差分析·实验设计与分析·残差分析·正态假设·交互作用
weixin_493202635 天前
R语言错误处理方法大全
开发语言·r语言
lishaoan775 天前
实验设计与分析(第6版,Montgomery)第4章随机化区组,拉丁方, 及有关设计4.5节思考题4.1~4.4 R语言解题
r语言·统计分析·方差分析·实验设计与分析·随机化区组