Skip to content

细胞组成分析:样本与分组间的细胞类型占比柱状图

作者: SeekGene
时长: 4 分钟
字数: 741 字
更新: 2026-02-27
阅读: 0 次
3' 转录组 5' + 免疫组库 ATAC + RNA 双组学 FFPE 单细胞转录组 Notebooks 全序列转录组 分析指南 甲基化 + RNA 双组学 空间转录组 绘图

加载数据

R
#加载云平台项目数据,包含Seurat的rds和对应的meta.data
data <- readRDS("/home/demo-seekgene-com/workspace/data/AY1752565399550/input.rds")
meta <- read.table("/home/demo-seekgene-com/workspace/data/AY1752565399550/meta.tsv", 
                  header = TRUE, 
                  sep = "\t", 
                  row.names = 1)
data <- AddMetaData(data, meta.data=meta)

计算每种细胞类型细胞总数和在不同样本中的占比

R

# 1. 准备数据,data是Seurat对象,CellAnnotation是一个注释结果在meta.data中的列名
cell_counts <- data@meta.data %>%
  count(CellAnnotation) %>%
  mutate(n_thousands = n / 1000)

# 2. 计算每个细胞类型中不同样本的百分比,CellAnnotation、Sample、Group都是meta.date的列名,注意这里按照自己的需求进行调整,
cell_percentages <- data@meta.data %>%
  count(CellAnnotation, Sample, Group) %>%
  group_by(CellAnnotation) %>%  # 按细胞类型分组
  mutate(percentage = n / sum(n) * 100) %>%
  ungroup()

# 3. 创建样本颜色映射
sample_info <- data@meta.data %>%
  distinct(CellAnnotation, Group) %>%
  arrange(Group, Sample)

# 为AD和Ctrl样本分配不同的颜色
ad_samples <- sample_info %>% filter(Group == "AD") %>% pull(Sample)
ctrl_samples <- sample_info %>% filter(Group == "Ctrl") %>% pull(Sample)

#定义样本颜色,等号前面是样本名,等号后面是颜色
sample_colors=c("3329"="#004983","4305"="#0040d1","4313"="#3472ff","4443"="#5b8dff",
                "4481"="#6f9bff","4482"="#96b6ff","4627"="#aac4ff","1224"="#ff2668",
                "1230"="#ff3a76","1238"="#ff4e84","3586"="#ff6292","HCT17HEX"="#ff89ad",
                "HCTZZT"="#ffb0c8","NT1261"="#ffc4d6","NT1271"="#ffebf1")
sample_order <- names(sample_colors)  # 获取颜色映射中的样本顺序
cell_percentages$Sample <- factor(cell_percentages$Sample, levels = sample_order)

ggplot2 画每种细胞类型细胞总数的柱状图

R
# 5. 创建上半部分
upper_plot <- ggplot(cell_counts, aes(x = CellAnnotation, y = n_thousands, fill = CellAnnotation)) +
  geom_bar(stat = "identity", width = 0.7) +
  scale_fill_manual(values = my36colors[1:length(unique(cell_counts$CellAnnotation))]) +
  labs(title = "Cell Count by Subclustering Type",
       x = "Cell Type", y = "Cell Count (x1000)") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(fill = NA, color = "black"))

ggplot2 画每种细胞类型在不同样本中百分比柱状图

R
# 5. 创建下半部分:百分比堆积柱状图
lower_plot <- ggplot(cell_percentages, aes(x = CellAnnotation, y = percentage, fill = Sample)) +
  geom_bar(stat = "identity", position = "stack", width = 0.7, color = "black", linewidth = 0.3) +
  scale_fill_manual(values = sample_colors,
                   name = "Donor",
                   labels = function(x) paste0(x, " (", sample_info$CellAnnotation[match(x, sample_info$Sample)], ")")) +
  labs(x = "Cell Type", y = "% From Donor") +
  theme_classic() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
    axis.text.y = element_text(size = 10),
    axis.title.x = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(size = 12, face = "bold"),
    legend.position = "right",
    legend.title = element_text(face = "bold"),
    legend.key.size = unit(0.4, "cm"),
    panel.grid.major.y = element_line(color = "grey80", linewidth = 0.2),
    panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5)
  ) +
  scale_y_continuous(labels = function(x) paste0(x, "%"),
                     expand = expansion(mult = c(0, 0.05)))

图片组合和保存

R
# 6. 组合图表(移除标题,更符合科研图表风格)
combined_plot <- upper_plot / lower_plot +
  plot_layout(heights = c(1, 1.2))

# 7. 显示图表
print(combined_plot)

# 8. 保存为高质量图片
ggsave("cell_distribution_plot.png", combined_plot, 
       width = 10, height = 8, dpi = 300, bg = "white")

# 9. 验证数据
cat("验证每个细胞类型的百分比总和:\n")
validation <- cell_percentages %>%
  group_by(CellAnnotation) %>%
  summarise(total_percent = sum(percentage), .groups = "drop")
print(validation)

cat("\n样本信息:\n")
print(sample_info)
0 条评论·0 条回复