多个切片的空间转录组基因表达图(tsne降维图/空间分布图)修改颜色

当一个空间转录组的object有多个切片时,绘制基因表达图想要改变颜色,常规方法通常只会改变最后一个切片的颜色,因此需要用apply函数对多个切片进行递归,用aplot包对递归后的结果进行整合。

颜色配置使用的是RcolorBrewer包。

一、加载包

library(Seurat)
library(aplot)
#devtools::install_github('satijalab/seurat-data')
library(SeuratData)
library(RColorBrewer)
library(ggplot2)
library(dplyr)

二、下载测试数据

#下载地址
#http://seurat.nygenome.org/src/contrib/stxBrain.SeuratData_0.1.1.tar.gz
#本地安装
#install.packages("D:/workspace/简书/stxBrain.SeuratData_0.1.1.tar.gz", repos = NULL, type = "source")

三、合并多个切片,同时进行基础分析

读取测试数据,对两个切片进行合并,参考https://nbisweden.github.io/workshop-scRNAseq/labs/compiled/seurat/seurat_07_spatial.html
为节省时间未做过滤,过滤步骤建议参考上面的网址或者其他文献。
brain1 <- LoadData("stxBrain", type = "anterior1")
brain2 <- LoadData("stxBrain", type = "posterior1")
brain <- merge(brain1, brain2)
brain
# An object of class Seurat
# 31053 features across 6049 samples within 1 assay
# Active assay: Spatial (31053 features, 0 variable features)
brain <- SCTransform(brain, assay = "Spatial", verbose = TRUE, method = "poisson")
brain <- RunPCA(brain, assay = "SCT", verbose = FALSE)
brain <- FindNeighbors(brain, reduction = "pca", dims = 1:30)
brain <- FindClusters(brain, verbose = FALSE)
brain <- RunUMAP(brain, reduction = "pca", dims = 1:30)
brain <- RunTSNE(brain, reduction = "pca", dims = 1:30)

四、绘制基因表达的tsne图,也可以画umap图,看个人喜好

fea <- c("Hpca", "Ttr",'Gfap','Sox2')
#若不修改颜色
png('test.tsne.png',width = 700,height = 600)
FeaturePlot(object = brain, reduction = "tsne", features = fea, ncol = 2,combine = TRUE)
dev.off()
image
#修改颜色
fix.sc <- scale_colour_gradientn(colours = rev(brewer.pal(n = 11, name = "RdBu")))
png('test.tsne.v2.png',width = 700,height = 600)
p1 <- FeaturePlot(object = brain, reduction = "tsne",features = fea, ncol = 2,combine = FALSE)
p2 <- lapply(p1,function(x)x+fix.sc)
aplot::plot_list(gglist=p2,ncol=2)
dev.off()
image

五、绘制基因空间分布图

#若不修改颜色
fea <- c("Hpca", "Ttr")
SpatialFeaturePlot(brain, features = fea,alpha = c(0.2, 1),combine=TRUE)+
  patchwork::plot_layout(ncol = 2, nrow =2)
ggsave('test.spatial.png')
image
#若修改颜色
fix.sc <- scale_fill_gradientn(colours= c('white','yellow','red'))
p3 <- lapply(fea,function(x){
  p1 <- SpatialPlot(brain, features = x,alpha = c(0.2, 1),combine=FALSE)
  p2 <- lapply(p1,function(x)x+fix.sc) %>% aplot::plot_list(gglist=.,ncol=2)
})
aplot::plot_list(gglist=p3,ncol=1)
ggsave('test.spatial.v2.png')
image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容