在R中使用circlize的大小差距

Big and small gaps using circlize in R

如何对类别进行分组,以便它们在使用 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 没有发生任何移动,因此从输出中省略了它。使用 gap.after 我硬编码了所需的结果(见图),但这不是一个可行的解决方案,因为我不知道哪些类别将被绘制或不被绘制。我还希望解决方案不依赖于矩阵中列和行的顺序。

在即将发布的 0.4.10 版本中,circlize 将支持命名向量作为 gap.after 的输入。它已经在 master 分支中,因此在将其拉入 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的作者@顾祖光联系到他后及时的帮助