chordDiagram() with overlapping ends in R

443 Views Asked by At

I am trying to produce a modified version of a Chord Diagram in R using the chordDiagram() function in the circlize package in R.

Suppose we have this toy example (as for the documentation of the package):

require(circlize)
df = expand.grid(letters[1:3], LETTERS[1:4])
df$value = 1
df$value2 = 3 
chordDiagram(df[, 1:3])

The output will be pretty straightforward:

enter image description here

Now, the problem is: let's suppose that I need to make two "ends" overlap to represent an intersection, for example, the ribbon going from b-->A and the ribbon going from a-->A, so that they end in the same sector "merging" each other (such as in the figure below). enter image description here

Is that possible? I didn't find any example in the documentation nor I was able to perform this task by tweaking the options.

Hope that I made myself clear. Thank you for any try.

1

There are 1 best solutions below

1
On

If I understand you correctly, you want to make overlapping parts in the diagram. So for example S3 -> E2 and S2 -> E2. You create a matrix which has a format so that you get overlapping paths in the diagram. I will give you an example with the following code:

First create a matrix with connection between S and E with different values:

library(circlize)
set.seed(999)
mat = matrix(sample(18, 18), 3, 6) 
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
mat

   E1 E2 E3 E4 E5 E6
S1  4 14 13 17  5  2
S2  7  1  6  8 12 15
S3  9 10  3 16 11 18

Next create a dataframe which has a format of columns with character from to another character with values to check what the patterns are more easily:

df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

   from to value
1    S1 E1     4
2    S2 E1     7
3    S3 E1     9
4    S1 E2    14
5    S2 E2     1
6    S3 E2    10
7    S1 E3    13
8    S2 E3     6
9    S3 E3     3
10   S1 E4    17
11   S2 E4     8
12   S3 E4    16
13   S1 E5     5
14   S2 E5    12
15   S3 E5    11
16   S1 E6     2
17   S2 E6    15
18   S3 E6    18

Plot the output using mat:

chordDiagram(mat)

enter image description here

As you can see, there is some overlap in the paths of S3 -> E2 and S2 -> E2.