How to move labels in a chord diagram in R

25 views Asked by At

I would like to have a label for each sector related to the event. For example: Stomachache, N=420.

Instead I have all the labels piled on top of the graph

this is my dataset:

USUBJID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)
A <- c(1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
B <- c(2, 0, 0, 0, 1, 1, 2, 0, 0, 0, 1, 1, 2, 1, 0, 2, 2, 2, 0, 1, 0, 0, 1, 2, 0, 2, 0, 0, 0, 0)
C <- c(0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0)
D <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
E <- c(1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0)
F <- c(2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0)

# dataframe creation
data<- data.frame(USUBJID, A, B, C, D, E, F)

These are the first 30 lines of my dataset

This is my code

# Extract the event columns (excluding "USUBJID")
events <- as.matrix(data[, -1])

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

# Define the gap degrees between sectors in the chord diagram
num_sectors <- ncol(events)

chordDiagram(
  x = co_occurrence,
  transparency = 0.5,
  annotationTrack = "grid"
)

# Add labels to the sectors above each sector
# Add labels with numerosity of each event
for (i in 1:length(sector_names)) {
  circos.text(
    x = 1,
    y = i,
    labels = sector_names[i],
    facing = "clockwise",
    niceFacing = TRUE,
    col = "black",
    adj = c(0, 0.5)
  )
  circos.text(
    x = 1,
    y = i,
    labels = paste("N =", sum(data[, i + 1] == 1)),
    facing = "clockwise",
    niceFacing = TRUE,
    col = "black",
    adj = c(0, 1)
  )
}

THAT is what I get, the labels are all on top and not readable. I would like to have a label for each sector related to the event. For example: A, N=14.

enter image description here

0

There are 0 answers