提问人:Bibi 提问时间:10/18/2023 最后编辑:Bibi 更新时间:10/20/2023 访问量:47
错误:由于 gap.degree 参数的长度大于 1,因此它的长度应与扇区数相同
Error: Since gap.degree parameter has length larger than 1, it should have same length as the number of sectors
问:
在 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,因此它的长度应与
扇区数量。
这就是这个想法(而不是公司,我想要每个事件的部门)
答:
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
对于所有这些问题,我真的很抱歉
评论
data
chordDiagram
circlize
gap.after
preAllocateSize
num_sectors