带透明链接的弦图

Chord Diagram with transparent links

我有一个 8x8 的邻接矩阵,如下所示:

我只需要可视化我的和弦图中的一些链接,保留所有 8 个扇区。我试图重现此处提供的说明:

所以我编码了:

library(circlize)
mat <- read.table("/home/myself/Documents/matrix1.txt", header=TRUE)  
col = c("#B96927","#3E647D","#7B92A8","#82C0E9","#2D6D66",
        "#BFA19C","#0088BC","#97B6B0")
col[3, 3] = "#FFFFFF00"
chordDiagram(as.matrix(mat), symmetric = TRUE, col = col)
circos.info()

但是,我收到以下错误

第一个错误:

> col[3, 3] = "#FFFFFF00"
Error in col[3, 3] = "#FFFFFF00" : 
  incorrect number of subscripts on matrix

第二个错误:

> chordDiagram(as.matrix(mat), symmetric = TRUE, col = col)
Error in if (nrow(value) == length(rn) && ncol(value) == length(cn)) { : 
  missing value where TRUE/FALSE needed

我该如何解决这个问题? 任何帮助将不胜感激。谢谢。

第一个问题的答案是因为你没有col做矩阵。我假设你想要一个像

这样的颜色矩阵
matrix(col, nrow(mat), ncol(mat), FALSE, dimnames(mat)) ## or byrow = TRUE

然后你可以改变每个 link 颜色。

可能有更简单的方法来更改单个 link,但这是一种 hacky 方法:

library('circlize')
mat <- abs(cor(mtcars[, 1:8]))
col <- c("#B96927","#3E647D","#7B92A8","#82C0E9","#2D6D66","#BFA19C","#0088BC","#97B6B0")

mat2 <- mat[-1L, -ncol(mat)]
mat2[upper.tri(mat2)] <- 0

#            mpg       cyl      disp        hp       drat        wt      qsec
# cyl  0.8521620 0.0000000 0.0000000 0.0000000 0.00000000 0.0000000 0.0000000
# disp 0.8475514 0.9020329 0.0000000 0.0000000 0.00000000 0.0000000 0.0000000
# hp   0.7761684 0.8324475 0.7909486 0.0000000 0.00000000 0.0000000 0.0000000
# drat 0.6811719 0.6999381 0.7102139 0.4487591 0.00000000 0.0000000 0.0000000
# wt   0.8676594 0.7824958 0.8879799 0.6587479 0.71244065 0.0000000 0.0000000
# qsec 0.4186840 0.5912421 0.4336979 0.7082234 0.09120476 0.1747159 0.0000000
# vs   0.6640389 0.8108118 0.7104159 0.7230967 0.44027846 0.5549157 0.7445354

col2 <- matrix(col[-length(col)], nrow(mat2), ncol(mat2), dimnames = dimnames(mat2))

## these are identical so edit col2 for the links
chordDiagram(mat2, grid.col = col)
chordDiagram(mat2, col = col2, grid.col = col)

## idx is a matrix of row/col indices for links to change
idx <- which(mat2 < 0.6, arr.ind = TRUE)
## add the transparency now since changing the color matrix strips the alpha?
col2[] <- Vectorize(adjustcolor)(col2, alpha.f = 0.5)
col2[idx] <- 'transparent'
chordDiagram(mat2, col = col2, grid.col = col)