应各位老师的需求复现一篇文章的中的某个图
具体复现图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))