Search code examples
rplotcorrelationfigurechord-diagram

Multiple individual chord diagrams in one figure in R


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: enter image description here

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?

Additional comment: If I had more categories, What should I readjust in the code to make a par(mfrow = c(2,2))? For example

MyBreaks <- c(0, 0.5, 0.6, 0.8, 1)
    MyColors <- c("red", "green", "yellow", "blue")
    
    
    # Create labels
    labels <- rownames(dat)
    
    # Create function to assign colors based on range
getColor <- function(val, i, j) {
  if (val >= 0 && val < 0.25) {
    return(MyColors[1])
  } else if (val >= 0.25 && val < 0.5) {
    return(MyColors[2])
  } else if (val >= 0.5 && val < 0.75) {
    return(MyColors[3])
  } else {
    return(MyColors[4])
  }
}
    
    
    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"))

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"))
    

    enter image description here