文章浮现之单细胞VDJ的柱状图

应各位老师的需求复现一篇文章的中的某个图

在这里插入图片描述

具体复现图5的整个思路图,这里没有原始数据,所以我使用虚拟生产的metadata进行画图

在这里插入图片描述
不废话直接上代码,先上python的代码的结果图

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

# 数据(这里使用示例数据,您需要替换为实际数据)
data = {
    'IGHD':  [20, 15, 25, 20, 15, 25, 10, 5, 10, 5, 2, 5, 2, 1, 5],
    'IGHM':  [65, 70, 45, 65, 70, 45, 75, 80, 65, 70, 55, 25, 10, 5, 10],
    'IGHA1': [5, 5, 10, 5, 5, 10, 5, 5, 10, 10, 15, 30, 30, 35, 25],
    'IGHA2': [2, 2, 5, 2, 2, 5, 2, 2, 5, 5, 10, 10, 10, 10, 15],
    'IGHG1': [3, 3, 5, 3, 3, 5, 3, 3, 5, 5, 8, 15, 30, 30, 25],
    'IGHG2': [2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 5, 10, 10, 10, 10],
    'IGHG3': [2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 3, 3, 5, 5, 5],
    'IGHG4': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 5]
}

# 设置颜色
colors = ['#ADD8E6', "#4169E1", '#90EE90', '#32CD32', '#FFC0CB', '#FF0000', '#800000', '#FFA500']

# 创建图表
fig, ax = plt.subplots(figsize=(12, 6))

# 绘制堆叠柱状图
bottom = np.zeros(15)
for cell_type, percentage in data.items():
    ax.bar(range(15), percentage, bottom=bottom, width=0.8, label=cell_type, color=colors[list(data.keys()).index(cell_type)])
    bottom += percentage

# 设置x轴标签
x_labels = ['HD', 'EBnor', 'EBpro'] * 5
ax.set_xticks(range(15))
ax.set_xticklabels(x_labels, rotation=45)

# 添加组标签
group_labels = ['Btr', 'Bn', 'Bm', 'AtM', 'ASC']
for i, label in enumerate(group_labels):
    ax.text(i*3 + 1, 105, label, ha='center', fontweight='bold')

# 设置y轴
ax.set_ylim(0, 110)
ax.set_ylabel('Fraction of cells (%)')

# 添加图例
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')

# 调整布局
plt.tight_layout()

# 显示图表
plt.show()

貌似R画的更好
在这里插入图片描述

library(ggplot2)
library(dplyr)
library(tidyr)

# 创建示例数据
data <- data.frame(
  Group = rep(c("Btr", "Bn", "Bm", "AtM", "ASC"), each = 3),
  Condition = rep(c("HD", "EBnor", "EBpro"), 5),
  IGHD = c(20,15,25, 20,15,25, 10,5,10, 5,2,5, 2,1,5),
  IGHM = c(65,70,45, 65,70,45, 75,80,65, 70,55,25, 10,5,10),
  IGHA1 = c(5,5,10, 5,5,10, 5,5,10, 10,15,30, 30,35,25),
  IGHA2 = c(2,2,5, 2,2,5, 2,2,5, 5,10,10, 10,10,15),
  IGHG1 = c(3,3,5, 3,3,5, 3,3,5, 5,8,15, 30,30,25),
  IGHG2 = c(2,2,3, 2,2,3, 2,2,3, 2,5,10, 10,10,10),
  IGHG3 = c(2,2,1, 2,2,1, 2,2,1, 2,3,3, 5,5,5),
  IGHG4 = c(1,1,1, 1,1,1, 1,1,1, 1,2,2, 3,4,5)
)

# 转换数据格式并归一化
data_long <- data %>%
  pivot_longer(cols = IGHD:IGHG4, names_to = "CellType", values_to = "Percentage") %>%
  group_by(Group, Condition) %>%
  mutate(Percentage = Percentage / sum(Percentage) * 100) %>%
  ungroup()

# 设置细胞类型的顺序
cell_type_order <- c("IGHG4", "IGHG3", "IGHG2", "IGHG1", "IGHA2", "IGHA1", "IGHM", "IGHD")
data_long$CellType <- factor(data_long$CellType, levels = cell_type_order)

# 创建图表
ggplot(data_long, aes(x = Condition, y = Percentage, fill = CellType)) +
  geom_bar(stat = "identity", position = "stack") +
  facet_grid(~ Group, scales = "free_x", space = "free_x") +
  scale_fill_manual(values = c("#FFA500", "#8B4513", "#FF0000", "#FF69B4", 
                               "#32CD32", "#90EE90", "#4169E1", "#ADD8E6")) +
  labs(y = "Fraction of cells (%)", x = NULL) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    strip.background = element_blank(),
    strip.text = element_text(size = 12, face = "bold")
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 100))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值