Search code examples
rchord-diagram

Chord Plot for a Correlation matrix. R


I want to create a Chord plot for a correlation matrix. Where the links represent the correlations in a color gradient from -1 to 1 (blue to red). I want just to represent those links that have an actual correlation.

This is an example dataset:

c1<-c(0,0,0.5,-0.2,0.1,-0.8)
c2<-c(0.2,0.8,-0.5,0,0,0)
c3<-c(0,0,-0.2,0,0,0.1)
mat<-rbind(c1,c2,c3)
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

A simple way to represent this in a chord plot would be like:

windows()
chordDiagram(df, big.gap = 30)
circos.clear()

However, it represents all the links, even if the value is 0. How can I create a color gradient for the value, from -1 to 1 with the 0 being white?

Thanks


Solution

  • Assuming you are using the circlize package you can adjust the colours manually based on a range by writing a function and then inputting it in the col argument of the chordDiagram() function:

    library(circlize)
    cols = colorRamp2(c(-1,0,1),c("blue","white","red"),transparency = 0.3)
    chordDiagram(mat,col=cols,big.gap=30)
    

    I used the matrix to plot the chord diagram but the data frame should produce the same results. However, I don't understand what you mean when you say all links are represented even if the value is 0 since for example, S1 to E1 is 0 but there is no link between the two enter image description here