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

R语言绘制事件共现弦图时gap.degree参数报错求助

解决R语言事件共现弦图绘制的报错问题

问题背景

需要绘制事件共现弦图,每个事件对应一个扇区,展示受试者的事件并发组合,但运行代码时触发报错:

Error: Since gap.degree parameter has length larger than 1, it should have same length as the number of sectors.

报错原因

代码中手动覆盖了num_sectors的正确值,同时gap_degree的长度与实际扇区数量不匹配:

  • 原始数据有6个事件列(Stomachache、Headache、Diarrhoea、Fever、Cramps、Vomiting),对应6个扇区
  • 错误地将num_sectors赋值为c(1,1,1,1,1),且gap_degree只设置了5个值,与实际6个扇区数量不符,导致报错

修正后的完整代码

# 加载依赖包
library(circlize)
library(dplyr)

# 原始数据集
data <- data.frame(
  subject_id =c( 6, 6, 6, 6, 6, 7, 7, 7, 10, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 5, 11, 11, 11, 11, 11, 14, 14, 15, 15, 15, 15, 18, 18, 18, 18, 21, 34, 34, 37, 37, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40),
  Stomachache = c( 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0),
  Headache = c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Diarrhoea = c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1),
  Fever = c( 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Cramps = c( 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Vomiting = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)

# 按受试者聚合事件发生次数
data <- data %>%
  group_by(subject_id) %>%
  summarize(
    Stomachache= sum(Stomachache),
    Headache = sum(Headache),
    Diarrhoea = sum(Diarrhoea),
    Fever = sum(Fever),
    Cramps = sum(Cramps),
    Vomiting =sum(Vomiting)
  )

# 提取事件列并转为矩阵
events <- as.matrix(data[, -1])

# 计算事件共现矩阵
co_occurrence <- t(events) %*% events

# 将对角线设为0(排除事件自身关联)
diag(co_occurrence) <- 0

# 设置扇区间隙:生成与扇区数量匹配的间隙值
gap_degree <- rep(5, ncol(events))

# 绘制弦图
chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  annotationTrack = "grid",
  preAllocateTracks = 1,
  gap.degree = gap_degree
)

# 可选:添加事件标签,增强可读性
circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) {
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")
  sector.name = get.cell.meta.data("sector.index")
  circos.text(mean(xlim), ylim[1] + .1, sector.name, facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
}, bg.border = NA)

关键修正点

  1. 删除了手动覆盖num_sectors的错误代码num_sectors<-c(1,1,1,1,1)
  2. 使用rep(5, ncol(events))生成与扇区数量(6个)完全匹配的gap_degree值,确保每个扇区间隙一致
  3. 新增可选的事件标签绘制代码,让每个扇区的事件名称更清晰直观

内容的提问来源于stack exchange,提问作者Bibi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 14:20:56