rchord-diagramcirclize

Chord Diagram with transparent links


I have a 8x8 adjacency matrix which looks as follows:

matrix

I need to visualize only some links within my Chord Diagram, keeping all the 8 sectors. I tried to reproduce the instructions provided here: R Circlize, Chord graph with empty sectors

So I coded:

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()

However, I get the following errors

First error:

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

Second error:

> 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

How can I fix this? Any help would be greatly appreciated. Thanks.


Solution

  • The answer to the first question is because you have not made col a matrix. I assume you want a color matrix like

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

    Then you can change each link color.

    There may be a simpler way to change a single link, but this is a hacky way:

    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)
    

    enter image description here enter image description here