Chord diagram with only one path between any two events

89 views Asked by At

I would like to have a chord diagram with only one path between any two events, this is my R code:

library(stringr)
library(circlize)

Subject <- 1:137
set.seed(123)

# Create 'EventA' variable with binomial distribution
EventA <- rbinom(137, size = 1, prob = 0.68)
EventB <- rbinom(137, size = 1, prob = 0.62)
EventC <- rbinom(137, size = 1, prob = 0.81)
EventD <- rbinom(137, size = 1, prob = 0.55)
EventE <- rbinom(137, size = 1, prob = 0.71)
EventF <- rbinom(137, size = 1, prob = 0.77)
data <- data.frame(Subject, EventA, EventB, EventC, EventD, EventE, EventF)

data$Subject <- str_sub(data$Subject, -5, -1)
#############################################
events <- as.matrix(data[, -1])

num_sectors <- ncol(events)

co_occurrence <- t(events) %*% events

diag(co_occurrence) <- 0

event_names <- c("EventA", "EventB", "EventC", "EventD", "EventE", "EventF")

event_numerosity <- colSums(data[,c(-1,-8)])

event_labels <- paste(event_names, "N = ", event_numerosity)

colnames(co_occurrence) <- rownames(co_occurrence) <- event_labels

print(co_occurrence)

grid.col <- setNames(rainbow(length(unlist(dimnames(co_occurrence)))), union(rownames(co_occurrence), colnames(co_occurrence)))

chordDiagram(co_occurrence, grid.col = grid.col) 

With this code I get this graph

enter image description here

If possible, could it be done with a single path between any two events?

1

There are 1 answers

7
Allan Cameron On

I think the problem is that your co-occurrence matrix is symmetrical, so it shows the same flow in both directions for each pair. Simply remove either the upper or lower triangle from the matrix to remove one set of flows:

co_occurrence[upper.tri(co_occurrence)] <- 0
chordDiagram(co_occurrence, grid.col = grid.col, annotationTrack = c("name","grid"))

enter image description here