简体   繁体   English

在 R 中使用 circlize 的大小间隙

[英]Big and small gaps using circlize in R

How do I group categories so that they appear with big gaps in a chord diagram generated using the circlize package in R?如何对类别进行分组,以便它们在使用 R 中的 circlize package 生成的和弦图中出现大间隙?

For example, given the following adjacency matrix:例如,给定以下邻接矩阵:

  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

I would like to create the three groups {A}, {B,C}, and {D,E,F,G,H}, so that when using chordDiagram() its parameter small.gap is used between segments within a group and big.gap is used between groups.我想创建三个组 {A}、{B、C} 和 {D、E、F、G、H},以便在使用chordDiagram()时,它的参数small.gap在组内的段之间使用组间使用big.gap

Note that this is code that will be run daily in production, and I cannot guarantee that movements always will occur between all categories.请注意,这是将在生产中每天运行的代码,我不能保证所有类别之间总是会发生变化。 In the example above, no movements occur from or to the category F, resulting in it being omitted from the output.在上面的示例中,F 类没有发生任何移动,因此从 output 中省略了它。 Using gap.after I hard-coded the desired result (see image), but this is not a feasible solution since I do not know which categories will be drawn or not.使用gap.after我硬编码了所需的结果(见图),但这不是一个可行的解决方案,因为我不知道将绘制哪些类别。 I would also prefer that a solution is not dependent on the ordering of the columns and rows in the matrix.我还希望解决方案不依赖于矩阵中列和行的顺序。 在此处输入图像描述

In the upcoming version 0.4.10, circlize will support a named vector as input for gap.after .在即将发布的 0.4.10 版本中,circlize 将支持命名向量作为gap.after的输入。 It is already in the master branch, so after pulling it in R it is possible to programmatically produce the correct gaps.它已经在主分支中,因此在将其拉入 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()

I'd like to thank the author @Zuguang Gu of circlize for the prompt help after I reached out to him.感谢circlize的作者@Zuguang Gu 在我联系到他后的及时帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM