在R中创建和弦网络

问题描述

我正在尝试使用R中的'networkD3'库为我的数据创建一个和弦图。我遵循此stackoverflow帖子中介绍的逻辑:Network chord diagram woes in R

我对使用'igraph'和'networkd3'创建和弦图特别感兴趣,因为我在计算机上没有安装其他库的管理权限(例如“ circlize”)。

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

接下来,我创建了一个邻接矩阵:

#创建邻接矩阵

g1 <- graph_from_adjacency_matrix(c)

当我尝试根据邻接矩阵创建Chord网络时,会发生问题:

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

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

有人知道我在做什么错吗?

谢谢

解决方法

函数igraph::graph_from_adjacency_matrixnetworkD3::chordNetwork都需要一个 square 矩阵作为输入。您输入的数据不是正方形(即行和列的数量相同)。这是两个基于帮助文件中示例的工作示例...

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...