错误:由于 gap.degree 参数的长度大于 1,因此它的长度应与扇区数相同

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

提问人:Bibi 提问时间:10/18/2023 最后编辑:Bibi 更新时间:10/20/2023 访问量:47

问:

在 R 中,我想创建一个和弦图来显示事件的分布,但更重要的是,我们可以为子事件找到所有可能的并发事件组合。在图中,我希望看到每个事件的扇区,并根据事件组合将这些扇区链接在一起

这是我的数据帧

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)
)

这是我的 R 代码:

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)
  )
# Extract the event columns (excluding "USUBJID")
events <- as.matrix(data[, -1])

# Determine the number of sectors
num_sectors <- ncol(events)

# Create a matrix with event co-occurrence
co_occurrence <- t(events) %*% events

# Convert diagonal values to 0 (events do not co-occur with themselves)
diag(co_occurrence) <- 0

# Define the gap degrees between sectors in the chord diagram
num_sectors<-c(1,1,1,1,1)
gap_degree <-c(5,5,5,5,5)

chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  annotationTrack = "grid",
  preAllocateTracks = 1,
  preAllocateSize = 1,
  gap.degree = gap_degree
)

但是我得到了这个错误:错误:由于gap.degree参数的长度大于1,因此它的长度应与 扇区数量。

这就是这个想法(而不是公司,我想要每个事件的部门)

enter image description here

r 错误处理 和弦

评论

0赞 Seth 10/18/2023
嗨,比比。有几件事可以帮助澄清这个问题:1)用于创建示例的代码将不起作用,因为变量的长度不同。2)功能从何而来?浏览库的文档时,我没有看到该函数或参数。3) 在哪里使用?您的代码定义了两次,但我看不出它在做什么。datachordDiagramcirclizegap.afterpreAllocateSizenum_sectors
0赞 Bibi 10/19/2023
您好@Seth,感谢您的回答。1)我刚刚更正了数据集代码,现在它们的长度相同,对此我很抱歉。2)它是circlize库,关于gap.after或preAllocateSize,它们很可能是错误的。我在一个网站上找到了它们。3)关于num_sectors,我很困惑!我希望每个事件(疾病)都有一个扇区,所以总共有 6 个扇区

答:

0赞 Seth 10/19/2023 #1
library(circlize)
library(dplyr)

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])

# Determine the number of sectors
num_sectors <- ncol(events)

# Create a matrix with event co-occurrence
co_occurrence <- t(events) %*% events

# Convert diagonal values to 0 (events do not co-occur with themselves)
diag(co_occurrence) <- 0

# Define the gap degrees between sectors in the chord diagram

gap_degree <-rep(5, times = num_sectors)

circos.par(gap.after = gap_degree)

chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  preAllocateTracks = 1
)

评论

0赞 Bibi 10/19/2023
非常感谢,它看起来真的很漂亮。您能告诉我如何在扇区之外添加事件编号,或者完全删除这些编号吗?(对我来说,出现的数字太高,与事件不对应)。那么,是否有可能制作一个和弦图来显示所有事件的分布,并且仅针对 event=“STOMACHACHE”,并发事件?
0赞 Bibi 10/19/2023
对于所有这些问题,我真的很抱歉