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

如何用circlize拆分数据绘制分类型癌症RNA表达环形热图?

解决方案:用circlize绘制分癌症类型的环形RNA表达热图

一、基于自有数据构建环形热图(拆分5个癌症区域)

1. 数据准备与导入

先确认你的数据结构:

  • expr_matrix:行是基因、列是样本的数值型表达矩阵
  • sample_groups:长度和样本数一致的向量,每个元素对应样本所属的癌症类型(共5类)

用基础R函数读取数据(根据你的文件格式调整):

# 读取表达矩阵,行设为基因名
expr_matrix <- read.csv("your_expression_data.csv", row.names = 1, header = TRUE)
expr_matrix <- as.matrix(expr_matrix) # 转成circlize兼容的矩阵格式

# 读取样本分组文件(假设是两列:样本ID、癌症类型)
sample_info <- read.csv("sample_groups.csv", header = TRUE)
sample_groups <- sample_info$cancer_type
# 强制校验:分组顺序必须和表达矩阵的样本列顺序完全匹配
stopifnot(colnames(expr_matrix) == sample_info$sample_id)

2. 拆分环形区块并初始化

根据各癌症类型的样本数,分配环形区块的宽度:

library(circlize)

# 统计每个癌症类型的样本量
group_counts <- table(sample_groups)
# 用样本量作为环形区块的宽度比例
sector_widths <- as.numeric(group_counts)

# 初始化环形设置
circos.clear() # 清空之前的环形配置
circos.par(gap.degree = rep(2, 5), # 每个区块间留2度间隙
           start.degree = 90) # 调整起始角度,让标签更易读
circos.initialize(factors = sample_groups, # 按分组拆分环形
                  xlim = cbind(rep(1, length(sample_groups)), rep(2, length(sample_groups))),
                  sector.width = sector_widths)

3. 绘制环形热图

用circos.heatmap()实现分组对应:

# 自定义表达量颜色映射
expr_col <- colorRamp2(c(min(expr_matrix), mean(expr_matrix), max(expr_matrix)), 
                       c("blue", "white", "red"))

# 绘制环形热图,按分组匹配区块
circos.heatmap(expr_matrix, 
               split = sample_groups, # 绑定分组与环形区块
               col = expr_col,
               rownames.side = "outside", # 基因名放在环形外侧
               show.sector.labels = TRUE) # 显示癌症类型标签

# 添加颜色图例
lgd <- Legend(title = "RNA Expression", col_fun = expr_col)
draw(lgd, x = unit(0.9, "npc"), y = unit(0.5, "npc"))

二、从ComplexHeatmap转换为环形热图(保留原有分类)

如果你已经用ComplexHeatmap生成了带分组的常规热图,直接复用现有数据即可——两者需要的矩阵和分组结构完全一致。

假设你的ComplexHeatmap代码如下:

library(ComplexHeatmap)

# 分组注释
ha <- HeatmapAnnotation(cancer_type = sample_groups)
# 常规分组成热图
ht <- Heatmap(expr_matrix, 
              top_annotation = ha, 
              column_split = sample_groups, 
              show_column_names = FALSE)
draw(ht)

直接把上述代码里的expr_matrix和sample_groups代入第一部分的circlize代码,用split = sample_groups参数就能自动把原有的分组对应到环形的不同区块,完全保留分类逻辑。

关键注意事项

  • 务必保证sample_groups的顺序和expr_matrix的样本列顺序完全一致,否则分组会错乱
  • 若基因过多导致标签重叠,可通过rownames.cex参数调小字体,或只筛选差异基因绘制
  • 环形间隙、起始角度可通过gap.degree和start.degree参数调整可视化效果

内容的提问来源于stack exchange,提问作者Kathleen Yasmin Almeida

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 23:15:27