我如何对类别进行分组,使它们在使用R中的circlize包生成的和弦图中出现较大的间隙?
例如,给定以下邻接矩阵:
A B C D E F G H
A 0 0 0 0 0 0 1168 0
B 0 0 2545 278 0 0 0 337
C 0 0 0 817 0 0 0 0
D 0 0 0 0 10 0 0 0
E 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0
G 0 0 561 326 0 0 0 281
H 0 0 46 8 0 0 0 0我想创建三个组{A},{B,C}和{D,E,F,G,H},以便在使用chordDiagram()时,其参数small.gap在组内的段之间使用,big.gap在组之间使用。
请注意,这是将在生产中每天运行的代码,我不能保证所有类别之间都会发生移动。在上面的示例中,没有发生从类别F到类别F的移动,导致它在输出中被省略。使用gap.after我硬编码了所需的结果(见下图),但这不是一个可行的解决方案,因为我不知道将绘制哪些类别。我也希望解决方案不依赖于矩阵中列和行的顺序。

发布于 2020-06-01 16:51:55
在即将到来的0.4.10版本中,circlize将支持命名向量作为gap.after的输入。它已经在主分支中,所以在R中拉出它之后,可以通过编程产生正确的间隙。
library(devtools)
install_github("jokergoo/circlize")
library(circlize) # chord diagrams
mat = read.table(textConnection("
A B C D E F G H
A 0 0 0 0 0 0 1168 0
B 0 0 2545 278 0 0 0 337
C 0 0 0 817 0 0 0 0
D 0 0 0 0 10 0 0 0
E 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0
G 0 0 561 326 0 0 0 281
H 0 0 46 8 0 0 0 0"))
mat = as.matrix(mat)
big_gap = 10
small_gap = 2
# For example three groups x,y,z
sector_grouping = cbind.data.frame(
"Sec" = c("A","B","C","D","E","F","G","H"),
"Grp" = c("x","y","y","z","z","z","z","z")
)
# Check which, if any, groups lack both outgoing and incoming
empty_row = rowSums(mat)[sector_grouping$Sec] == 0
empty_column = colSums(mat)[sector_grouping$Sec] == 0
# Remove empty sectors
sector_grouping = sector_grouping[!(empty_row & empty_column),]
# Iterate and set a big gap (last sector in group) or small gap.
for(i in 1:nrow(sector_grouping)) {
sector_grouping[i,"Gap"] = ifelse(
sector_grouping[i,2]==sector_grouping[i+1,2],
small_gap,
big_gap
)
}
# The last sector needs fixing (always assumed big)
sector_grouping$Gap[is.na(sector_grouping$Gap)] = big_gap
# Build named vector
gap.after = sector_grouping$Gap
names(gap.after) = sector_grouping$Sec
circos.par(gap.after = gap.after)
chordDiagram(mat, order = LETTERS[1:8])
circos.clear()我要感谢circlize的作者@Zuguang Gu,在我联系他之后,他提供了及时的帮助。
https://stackoverflow.com/questions/62100993
复制相似问题