Multiple individual chord diagrams in one figure in R

Advertisements

I have the following correlation values in R namely “dat` as follows:

dat <- as.matrix(cbind(c(1.0000000, 0.5161944, 0.5190630, 0.3717464, 0.5719125),
                       c(0.5161944, 1.0000000, 0.2549985, 0.8469740, 0.7761113),
                       c(0.5190630, 0.2549985, 1.0000000, 0.2130771, 0.2930945),
                       c(0.3717464, 0.8469740, 0.2130771, 1.0000000, 0.5652874),
                       c(0.5719125, 0.7761113, 0.2930945, 0.5652874, 1.0000000))
)
colnames(dat) <- c(paste0("X",1:ncol(dat)))
rownames(dat) <- c(paste0("X",1:ncol(dat)))
dat[lower.tri(dat)] <- 0 

and I have created the following chord diagram:

MyBreaks <- c(0, 0.5, 1)
MyColors <- c("red", "green")


# Create labels
labels <- rownames(dat)

# Create function to assign colors based on range
getColor <- function(val, i, j) {
  if (val >= 0 && val < 0.5) {
    return(MyColors[1])
  } else  {
    return(MyColors[2])
  }
}


zero_mat <- (dat == 0)
col_vec <- sapply(as.vector(dat), getColor, i = row(dat), j = col(dat))

col_vec[zero_mat] <- "#00000000"

# Create chord diagram
chordDiagram(dat, order = labels, col = col_vec, 
             annotationTrack = c("name", "grid"))

which results in:

How can I create a second figure that contains 2 chord diagrams, one with each colour category, i.e one chord displaying only the green lines and one displaying only the red lines?

>Solution :

You could conditionally replace the unwanted color with a blank transparent value using ifelse. Set par(mfrow = c(1, 2)) beforehand so you have both circles in one figure.

library(circlize)

par(mfrow = c(1, 2))
chordDiagram(dat, order = labels, 
                           col = ifelse(col_vec == "green", "#00000000", col_vec),
                           annotationTrack = c("name", "grid")) 

chordDiagram(dat, order = labels, 
                           col = ifelse(col_vec == "red", "#00000000", col_vec),
                           annotationTrack = c("name", "grid"))

Leave a Reply Cancel reply