You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单细胞RNA测序分析:如何绘制X轴为基因的自定义细胞群小提琴图?

实现自定义细胞群的基因X轴小提琴图

完全可以实现你要的这种小提琴图,下面给你两种可行的方案,都是基于R环境的常用单细胞分析工具:

方案一:Seurat + ggplot2 手动实现

这种方式灵活性高,适合需要自定义图形细节的场景:

  1. 加载依赖包
library(Seurat)
library(ggplot2)
library(tidyr)
  1. 筛选目标细胞群
    假设你的Seurat对象名为seurat_obj,细胞类型标注在cell_type列里,先提取细胞类型"A"的子集:
target_cells <- subset(seurat_obj, cell_type == "A")
  1. 整理表达数据为长格式
    先定义你要展示的基因列表,然后提取标准化表达量并转换为ggplot需要的长数据结构:
# 替换成你感兴趣的基因
genes_of_interest <- c("GeneX", "GeneY", "GeneZ")

# 提取标准化表达矩阵(slot选"data"对应log标准化后的数据,"scale.data"对应归一化后的数据,按需选择)
expr_matrix <- t(GetAssayData(target_cells, assay = "RNA", slot = "data"))
expr_df <- as.data.frame(expr_matrix)
expr_df$cell_id <- rownames(expr_df)

# 转换为长格式
long_expr <- pivot_longer(expr_df, cols = all_of(genes_of_interest), names_to = "Gene", values_to = "Normalized_Expression")
  1. 绘制小提琴图
ggplot(long_expr, aes(x = Gene, y = Normalized_Expression)) +
  geom_violin(fill = "#4292c6", alpha = 0.7) +
  geom_jitter(size = 0.4, alpha = 0.2, color = "darkgray") +  # 可选:添加单个细胞的表达散点
  theme_classic() +
  labs(x = "基因", y = "标准化表达量") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # 旋转X轴标签避免重叠

方案二:scCustomize 快速实现

你之前没找到适配功能可能是忽略了cells_use参数,这个包的Plot_Violin函数支持直接指定自定义细胞群,同时传入多个基因就能生成X轴为基因的小提琴图:

  1. 加载包并绘图
library(scCustomize)

# 同样替换成你的基因列表和Seurat对象
Plot_Violin(seurat_object = seurat_obj,
            genes = genes_of_interest,
            cells_use = WhichCells(seurat_obj, expression = cell_type == "A"),
            x_axis_label = "基因",
            y_axis_label = "标准化表达量",
            rotate_x_labels = TRUE,
            violin_fill_color = "#2ca25f")

两种方案都能满足你的需求:针对细胞群"A"绘制多个基因的小提琴图,X轴是基因,Y轴是标准化表达量,不需要为每个基因单独作图。

内容的提问来源于stack exchange,提问作者kuuchan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:50:32