rplotchord-diagramcirclize

How to plot a circlize chord diagram from a matrix


I have a matrix of rows of genes with counts how many drugs they have in certain disease catorgies.

I am trying to make a chord diagram in R, to see all my diseases in sections and then per disease how many genes have drugs in which and which overlap.

However, with my matrix when I try to run chordDiagram(df) I get a chord diagram that has sections not only for the diseases but the genes aswell.

A snippet of my data looks like this:

df <- structure(c(0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 2, 
0, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 35, 0, 0, 0, 0, 0, 
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1), .Dim = c(5L, 
11L), .Dimnames = list(c("CASZ1", "CASQ2", "VANGL1", "CLCN6", 
"MTHFR"), c("heart failure", "cardiomyopathy", "coronary artery disease", 
"cardiovascular disease", "hypertension", "atrial fibrillation", 
"arrhythmia", "myocardial infarction", "cardiac arrest", "heart valve disease", 
"Other conditions")))

How can I get a chord diagram whose sections are only the diseases/my column names?


Solution

  • chordDiagram expects an adjacency matrix, which you don't really have. You can convert your matrix into an edge list based on conditions linked by common genes, then convert that into an adjacency matrix suitable for chordDiagram

    do.call('rbind', lapply(apply(df, 1, \(x) colnames(df)[x > 0]), function(x) {
      if(length(x) < 2) return(NULL)
      if(length(x) == 2) return(t(x))
      return(t(combn(x, 2)))
    })) |>
      igraph::graph.edgelist() |>
      igraph::as_adj(sparse = FALSE) |>
      circlize::chordDiagram()
    

    enter image description here