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

学习交流

相关推荐
TDengine (老段)34 分钟前
TDengine 语言连接器(R语言)
大数据·数据库·物联网·r语言·时序数据库·tdengine·iotdb
maizeman1261 天前
R语言——分布和公式
开发语言·r语言·公式·随机数·分布
Tiger Z1 天前
R 语言科研绘图第 39 期 --- 饼状图-旭日
开发语言·程序人生·r语言·贴图
清同趣科研2 天前
扩增子分析|基于R语言microeco包进行微生物群落网络分析(network网络、Zi-Pi关键物种和subnet子网络图)
r语言
周周记笔记3 天前
探索R语言:在线学习资源汇总
开发语言·r语言
拓端研究室TRL4 天前
Python与R语言用XGBOOST、NLTK、LASSO、决策树、聚类分析电商平台评论信息数据集
开发语言·python·算法·决策树·r语言
Chh07154 天前
[特殊字符] 第十四讲 | 空间异质性检验与地统计局部指标(LISA)应用
arcgis·数据分析·r语言
Chh07154 天前
[特殊字符] 第十三讲 | 地统计模拟与空间不确定性评估
分类·数据分析·r语言·学习方法
Chh07154 天前
[特殊字符] 第十二讲 | 地统计学基础与克里金插值法(Kriging)建模实践
分类·数据分析·回归·r语言