In R I would like to create a chord diagram showing the distribution of events, but more importantly all possible combinations of concurrent events that we can find for the subects. Inside the diagram I would like to see a sector for each event and these linked together according to the event combinations

This is my dataframe

data <- data.frame(
  subject_id =c( 6, 6, 6, 6, 6, 7, 7, 7, 10, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 5, 11, 11, 11, 11, 11, 14, 14, 15, 15, 15, 15, 18, 18, 18, 18, 21, 34, 34, 37, 37, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40),
  Stomachache = c( 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0),
  Headache = c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Diarrhoea = c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1),
  Fever = c( 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Cramps = c( 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  Vomiting = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)

This is my R code:

data <- data %>%
  group_by(subject_id) %>%
  summarize(
    Stomachache= sum(Stomachache),
    Headache = sum(Headache),
    Diarrhoea = sum(Diarrhoea),
    Fever = sum(Fever),
    Cramps = sum(Cramps),
    Vomiting =sum(Vomiting)
  )
# Extract the event columns (excluding "USUBJID")
events <- as.matrix(data[, -1])

# Determine the number of sectors
num_sectors <- ncol(events)

# Create a matrix with event co-occurrence
co_occurrence <- t(events) %*% events

# Convert diagonal values to 0 (events do not co-occur with themselves)
diag(co_occurrence) <- 0

# Define the gap degrees between sectors in the chord diagram
num_sectors<-c(1,1,1,1,1)
gap_degree <-c(5,5,5,5,5)

chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  annotationTrack = "grid",
  preAllocateTracks = 1,
  preAllocateSize = 1,
  gap.degree = gap_degree
)

But I got this error: Error: Since gap.degree parameter has length larger than 1, it should have same length as the number of sectors.

This is the idea (instead of company, I would like a sector for each event)

enter image description here

1

There are 1 answers

2
Seth On
library(circlize)
library(dplyr)

data <- data %>%
  group_by(subject_id) %>%
  summarize(
    Stomachache= sum(Stomachache),
    Headache = sum(Headache),
    Diarrhoea = sum(Diarrhoea),
    Fever = sum(Fever),
    Cramps = sum(Cramps),
    Vomiting =sum(Vomiting)
  )

events <- as.matrix(data[, -1])

# Determine the number of sectors
num_sectors <- ncol(events)

# Create a matrix with event co-occurrence
co_occurrence <- t(events) %*% events

# Convert diagonal values to 0 (events do not co-occur with themselves)
diag(co_occurrence) <- 0

# Define the gap degrees between sectors in the chord diagram

gap_degree <-rep(5, times = num_sectors)

circos.par(gap.after = gap_degree)

chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  preAllocateTracks = 1
)