基于分类变量聚类环形热图绘制报错咨询
解决环形热图中
circos.link参数错误的方案 常见错误原因及排查步骤
样本/扇区名称不匹配:
circos.link要求sector.index1和sector.index2必须与环形布局初始化时定义的扇区名称完全一致(包括大小写、特殊字符、空格)。- 验证样本名一致性:
# 确认clin.info的样本列与meth、exp列名完全对齐 all(colnames(meth) == clin.info$sample_id) all(colnames(exp) == clin.info$sample_id) - 若存在不匹配,先统一样本名(比如去除特殊字符、统一大小写)。
- 验证样本名一致性:
位置范围无效:
start/end参数必须落在对应扇区的坐标范围内(比如热图每行对应一个基因,范围就是1到基因总数)。- 查看当前扇区的坐标范围:
circos.info()$sectors
- 查看当前扇区的坐标范围:
轨道索引错误:如果指定
track.index,必须是已创建的轨道编号(从1开始计数),不能超出已存在的轨道数量。
结合亚型聚类的修正代码示例
假设你的数据结构为:meth(甲基化矩阵,行=基因、列=样本)、exp(表达矩阵,行=基因、列=样本)、clin.info(含sample_id和subtype列),以下是核心修正代码:
library(circlize) library(ComplexHeatmap) # 1. 按subtype排序样本,统一数据顺序 subtype_sort_idx <- order(clin.info$subtype) meth_sorted <- meth[, subtype_sort_idx] exp_sorted <- exp[, subtype_sort_idx] clin_sorted <- clin.info[subtype_sort_idx, ] # 2. 初始化环形布局,扇区为样本名 circos.initialize(sectors = colnames(meth_sorted), xlim = c(1, nrow(meth_sorted))) # 3. 绘制甲基化热图轨道 circos.trackPlotRegion(ylim = c(0, 1), track.height = 0.1, bg.border = NA, panel.fun = function(x, y) { curr_sample <- get.cell.meta.data("sector.index") meth_vals <- meth_sorted[, curr_sample] circos.rect(1:nrow(meth_sorted)-0.5, 0, 1:nrow(meth_sorted)+0.5, 1, col = colorRamp2(c(min(meth), 0, max(meth)), c("blue", "white", "red"))(meth_vals), border = NA) }) # 4. 绘制表达热图轨道 circos.trackPlotRegion(ylim = c(0, 1), track.height = 0.1, bg.border = NA, panel.fun = function(x, y) { curr_sample <- get.cell.meta.data("sector.index") exp_vals <- exp_sorted[, curr_sample] circos.rect(1:nrow(exp_sorted)-0.5, 0, 1:nrow(exp_sorted)+0.5, 1, col = colorRamp2(c(min(exp), 0, max(exp)), c("green", "white", "orange"))(exp_vals), border = NA) }) # 5. 绘制亚型分组连接(正确调用circos.link) subtype_groups <- split(seq_along(clin_sorted$subtype), clin_sorted$subtype) for (group in subtype_groups) { # 连接同亚型的首尾样本(可根据需求调整连接逻辑) circos.link(sector.index1 = colnames(meth_sorted)[group[1]], start1 = 1, end1 = nrow(meth_sorted), sector.index2 = colnames(meth_sorted)[group[length(group)]], start2 = 1, end2 = nrow(meth_sorted), col = adjustcolor(unique(clin_sorted$subtype[group]), alpha.f = 0.3)) } circos.clear()
额外注意事项
- 若热图按基因聚类而非样本,需调整
circos.initialize的扇区定义(比如扇区设为基因名)和坐标范围。 - 确保
circos.link的扇区名称与circos.initialize中定义的完全一致,不能传入未定义的字符串。 - 若使用
ComplexHeatmap的circos.heatmap函数,需注意其扇区命名规则,避免与基础circlize函数参数混淆。
内容的提问来源于stack exchange,提问作者Anon
相关产品推荐
相关产品推荐

