0

次のような8x8の隣接行列があります。

マトリックス

コード ダイアグラム内の一部のリンクのみを視覚化する必要があり、8 つのセクターすべてを保持します。ここで提供されている手順を再現しようとしました: R Circlize, Chord graph with emptyectors

だから私はコーディングしました:

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

2 番目のエラー:

> 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

どうすればこれを修正できますか? どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

0

最初の質問に対する答えはcol、マトリックスを作成していないためです。次のようなカラーマトリックスが必要だと思います

matrix(col, nrow(mat), ncol(mat), FALSE, dimnames(mat)) ## or byrow = TRUE

次に、各リンクの色を変更できます。

単一のリンクを変更するもっと簡単な方法があるかもしれませんが、これはハックな方法です:

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)

ここに画像の説明を入力 ここに画像の説明を入力

于 2019-09-10T16:41:53.143 に答える