我正在尝试使用R中的'networkD3‘库为我的数据创建chord图。我遵循了这篇stackoverflow文章中提出的逻辑:Network chord diagram woes in R
我对使用'igraph‘和'networkd3’创建chord图特别感兴趣,因为我在我的计算机上没有安装其他库(如"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, width = 500, height = 500, ) :
Data must be of type matrix or data frame有人知道我做错了什么吗?
谢谢
发布于 2020-11-03 05:07:26
函数igraph::graph_from_adjacency_matrix和networkD3::chordNetwork都需要正方形矩阵作为输入。您输入的数据不是正方形(即相同的行数和列数)。以下是基于帮助文件中的示例的两个工作示例...
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"))https://stackoverflow.com/questions/64639877
复制相似问题