问题描述
使用R中的'networkd3'和'igraph'库,我试图在R中创建一个和弦网络,如下所示:https://www.rdocumentation.org/packages/networkD3/versions/0.4/topics/chordNetwork
我模拟了一些假数据,然后将这些数据转换为邻接矩阵/图形,然后创建和弦网络。但是,将原始数据转换为所需的格式似乎存在问题。
以下是我的代码:
library(igraph)
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)
#create dataframe
c = data.frame(a,b)
#rename column names
colnames(c) <- c("a","b")
c = as.matrix(c)
# change dimensions of the matrix,from: https://stackoverflow.com/questions/26458182/converting-to-rectangular-matrix-to-a-square-matrix-in-r
d <- dim(c)
cn <- colnames(c)
#here is the error
reformat = rbind(c,matrix(0,diff(d),ncol(c),dimnames = list(cn[(d[1]+1):d[2]])))
#Error in matrix(0,dimnames = list(cn[(d[1] + 1):d[2]])) :
invalid 'nrow' value (< 0)
#create graph
g1 <- graph_from_adjacency_matrix(reformat)
#create chord network (with the values from "a" and "b" appearing around the circle)
chordNetwork(Data = g1,width = 500,height = 500,labels = colnames(g1)
)
有人知道我在做什么错吗?还是用这种图创建和弦网络是不可能的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)