在即将发布的 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在我联系到他后的及时帮助。