FeaturePlot 批量绘图与美化实战

来源:生信漫漫学
发布时间:1746752940

写在开头

又开始在扒拉之前整理过文献里面好看的图表了,翻到去年一篇比较早期的推文—— 肝细胞癌微血管侵袭单细胞分析研究-1

最开始整理的时候,最先看中的是文献的小清新配色,全文非常统一!

去年也完成了第一层次降维聚类分群—— GSE242889-肝细胞癌微血管侵袭 ,接下来就一起来复现一下文献中好看的图表。

文章简介

文章标题: 《Single-cell dissection of the multicellular ecosystem and molecular features underlying microvascular invasion in HCC》

发表日期和杂志: 2023年发表在Hepatology上

在线阅读链接: https://doi.org/10.1097%2FHEP.0000000000000673

单细胞数据详情: https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE242889

文章中分析代码: https://github.com/ZhoulabCPH/MVI_HCC

接下来准备基于文献提供的Figure1-2的代码,加上自己的理解复现一下文章的好看图表。

Figure1C简介

UMAP可视化显示典型细胞类型标记基因的表达

使用FeaturePlot展示了注释到的细胞亚群里面的标记基因,文献提供的代码示例:

FeaturePlot(sce.all.int,features = "CD3E",reduction = "umap",pt.size = 0.0001,max.cutoff = 1.5,
            cols = c("#FFEFD5","#E6E6FA","#87CEFA","#6495ED","#4169E1","#0000CD","#000080"))+
  scale_x_continuous("")+scale_y_continuous("")+ 
  theme_bw()+
  theme(  panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
          axis.ticks = element_blank(),axis.text = element_blank(),    
          legend.position = "none",                                       
          plot.title = element_text(hjust = 0.5,size=14))+ggtitle('T cell (CD3E)')

文章中可视化的时候,首先是调整了颜色,然后调整了坐标轴的内容,加上了以细胞亚群为主包含特异性Marker基因的名称。按照文章的内容以及代码可以尝试复现及美化

FeaturePlot批量可视化及美化

FeaturePlot可视化

首先整理可视化全部亚群相关的特异性Marker基因,使用FeaturePlot直接可视化,借用一下文章的配色

FeaturePlot(sce.all.int,features = c("CD3E","KLRD1","CD79A","LYZ",
                                     "PECAM1","ACTA2","TF","EPCAM"),
            reduction = "umap",pt.size = 0.0001,max.cutoff = 1.5,
            cols = c("#FFEFD5","#E6E6FA","#87CEFA","#6495ED","#4169E1","#0000CD","#000080"))

从结果中可以看到,需要调整的就是坐标轴样式,以及每个图对应的标题需要调整

Figure1C复现

#Figure1C复现----
library(Seurat)
library(ggplot2)
library(patchwork)

# 定义细胞类型和对应的标记基因
cell_types <- list(
  T_cell = "CD3E",
  NK_cell = "KLRD1",
  B_cell = "CD79A",
  Myeloid_cell = "LYZ",
  Endothelial_cell = "PECAM1",
  Mesenchymal_cell = "ACTA2",
  Malignant_cell = "TF",
  HPC = "EPCAM"
)

# 定义颜色
colors <- c("#FFEFD5""#E6E6FA""#87CEFA""#6495ED""#4169E1""#0000CD""#000080")

# 创建一个空列表来存储图
plots <- list()

# 为每个细胞类型创建图
for (cell_type in names(cell_types)) {
  feature <- cell_types[[cell_type]]
  p <- FeaturePlot(sce.all.int, features = feature, 
                   reduction = "umap", pt.size = 0.5, max.cutoff = 1.5,
                   cols = colors) +
    scale_x_continuous("") + scale_y_continuous("") + 
    theme_bw() +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          axis.ticks = element_blank(), axis.text = element_blank(),
          legend.position = "none",
          plot.title = element_text(hjust = 0.5, size = 10)) +
    ggtitle(paste(cell_type, " (", feature, ")", sep = ""))
  plots[[cell_type]] <- p
}

# 使用patchwork包将图组合在一起

plot_grid <- wrap_plots(plots, ncol = 4)

# 保存组合后的图到PDF文件
ggsave("Figure1C.pdf", plot_grid, width = 12, height = 6, units = "in")

  • 首先定义细胞类型和对应的标记基因,创建一个空的列表plots来存储每个细胞类型的图
  • 使用for循环为每个细胞类型创建图,并将它们存储在plots列表中
  • 使用wrap_plots函数将这些图按照指定的网格排列,基于ncol参数或每列应该有多少图
  • 最后使用ggsave导出为PDF文件,设置好宽度和高度

一些些美化尝试

本来想着是说,能不能将细胞亚群注释也全部在结果图上面进行标识,然后发现太冗余了!

# 创建一个空列表来存储图
plots <- list()

# 为每个细胞类型创建图
for (cell_type in names(cell_types)) {
  feature <- cell_types[[cell_type]]
  p <- FeaturePlot(sce.all.int, features = feature, 
                   reduction = "umap", pt.size = 0.5, max.cutoff = 1.5,
                   cols = colors, label = TRUE) +
    scale_x_continuous("") + scale_y_continuous("") + 
    theme_bw() +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          axis.ticks = element_blank(), axis.text = element_blank(),
          legend.position = "none",
          plot.title = element_text(hjust = 0.5, size = 10)) +
    ggtitle(paste(cell_type, " (", feature, ")", sep = ""))
  plots[[cell_type]] <- p
}

# 使用patchwork包将图组合在一起
plot_grid <- wrap_plots(plots, ncol = 4)

# 保存组合后的图到PDF文件
ggsave("combined_plots_with_labels.pdf", plot_grid, width = 12, height = 6, units = "in")

虽然加上注释信息,能更好的分辨出注释好的细胞亚群,但是黑色和背景色有点重叠,不是很好的将注释信息展现出来。且展示全部亚群的话,图片看起来有点复杂,所以考虑不能不只展示对应的亚群

# 创建空列表存图
plots <- list()

# 主循环:为每个marker生成图并注释表达中心
for (cell_type in names(cell_types)) {
  gene <- cell_types[[cell_type]]

# 获取表达数据
  expr_values <- FetchData(sce.all.int, vars = gene)
  umap_coords <- Embeddings(sce.all.int, "umap")

# 找到表达值top 1%细胞作为标记
  high_expr_cells <- rownames(expr_values)[expr_values[[gene]] > quantile(expr_values[[gene]], 0.99)]
  high_expr_umap <- umap_coords[high_expr_cells, , drop = FALSE]
  center_x <- mean(high_expr_umap[, 1])
  center_y <- mean(high_expr_umap[, 2])

# 画图
  p <- FeaturePlot(sce.all.int, features = gene,
                   reduction = "umap", pt.size = 0.5, max.cutoff = 1.5,
                   cols = colors) +
    annotate("text", x = center_x, y = center_y, label = cell_type,
             size = 3.5, fontface = "bold", color = "#E95C59"
             stroke = 0.5, family = "sans"
             bg.color = "black", bg.r = 0.15)  +
    scale_x_continuous("") + scale_y_continuous("") + 
    theme_bw() +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          axis.ticks = element_blank(), axis.text = element_blank(),
          legend.position = "none",
          plot.title = element_text(hjust = 0.5, size = 10)) +
    ggtitle(paste(cell_type, " (", gene, ")", sep = ""))

  plots[[cell_type]] <- p
}

# 拼图:2行4列
plot_grid <- wrap_plots(plots, ncol = 4)

# 保存
ggsave("featureplots_labeled_by_celltype.pdf", plot_grid, width = 12, height = 6)

也可以采用文献中的标注方式,将对应的细胞亚群圈出来,大家按照自己的喜好进行可视化即可!

文末友情宣传

如果你也想做单细胞转录组数据分析,最好是有自己的计算机资源哦,比如我们的 满足你生信分析计算需求的低价解决方案 ,而且还需要有基本的生物信息学基础,也可以看看我们的 生物信息学马拉松授课 ,你的生物信息学入门课。

2025年也会继续学习分享单细胞内容,并且组建了交流群—— 承包你2025全部的单细胞转录组降维聚类分群 ,欢迎一起讨论交流学习!