0

I am trying to use the 'networkD3' library in R to create a chord diagram for my data. I am following the logic presented in this stackoverflow post: Network chord diagram woes in R

I am particularly interested in creating a chord diagram using 'igraph' and 'networkd3', since I do not have administrative rights on my computer to install other libraries (such as "circlize").

I created some fake data in R:

library(igraph)
library(dplyr)
library(networkD3)

#create file from which to sample from
x5 <- sample(1:100, 1100, replace=T)
#convert to data frame
x5 = as.data.frame(x5)

#create first file (take a random sample from the created file)
a = sample_n(x5, 1000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 1000)

#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")

Next, I created an adjacency matrix:

#create adjacency matrix

g1 <- graph_from_adjacency_matrix(c)

The problem happens when I try to create the Chord Network from the adjacency matrix:

chordNetwork(Data = c, 
             width = 500, 
             height = 500,
            )

Error in chordNetwork(Data = g, width = 500, height = 500, ) : 
  Data must be of type matrix or data frame

Does anyone know what I am doing wrong?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

0

Both functions igraph::graph_from_adjacency_matrix and networkD3::chordNetwork require a square matrix as input. The data you are inputting is not square (i.e. same number of rows and columns). Here are two working examples based on the examples in their help files...

adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.9,0.1)), nc=10)

graph_from_adjacency_matrix(adjm)

chordNetwork(Data = adjm)


#####


hairColourData <- matrix(c(11975,  1951,  8010, 1013,
                           5871, 10048, 16145,  990,
                           8916,  2060,  8090,  940,
                           2868,  6171,  8045, 6907),
                         nrow = 4)

graph_from_adjacency_matrix(hairColourData)

chordNetwork(Data = hairColourData, 
             labels = c("red", "brown", "blond", "gray"))
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56