映射到邻接矩阵输出的从属关系

问题描述 投票:0回答:1

假设我正在使用this affiliation matrix

library(igraph)

A=c(1,1,0,0) 
B=c(1,0,1,0) 
C=c(1,0,1,0) 
D=c(0,1,0,1) 
E=c(0,0,1,1) 
aff=matrix(c(A,B,C,D,E),nrow=5,byrow=TRUE) 
dimnames(aff)=list(c("A","B","C","D","E"),c("Group1","Group2","Group3","Group4"))

看起来像这样:

##   Group1 Group2 Group3 Group4
## A      1      1      0      0
## B      1      0      1      0
## C      1      0      1      0
## D      0      1      0      1
## E      0      0      1      1

您可以从中产生(使用aff %*% t(aff))以下邻接矩阵:

##   A B C D E
## A 2 1 1 1 0
## B 1 2 2 0 1
## C 1 2 2 0 1
## D 1 0 0 2 1
## E 0 1 1 1 2

这些组(例如Group1Group2等)在转换为邻接矩阵时未保留,因此在绘制时:

m2=aff %*% t(aff)
g2=graph_from_adjacency_matrix(m2, "undirected", weighted=T, diag=F)
plot(g2, edge.width=E(g2)$weight)

enter image description here

无法知道ABAC等之间存在什么共享组连接

我的问题:有什么方法可以保留此分组变量,以便可以从邻接矩阵制作图,同时允许将边像这样标记为Group3Group1?:

enter image description here

注意:我打算使用visNetwork,而不是igraph,但是这个问题似乎源于数据结构本身,而不是所使用的包,因此,为简单起见,我选择了此问题。

r neural-network igraph graph-theory adjacency-matrix
1个回答
0
投票
m3 = get.edgelist(g2)
lbls = sapply(1:NROW(m3), function(i){
    toString(names(which(aff[m3[i, 1],] == 1 & aff[m3[i, 2],] == 1)))
})
plot(g2, edge.label = lbls)
© www.soinside.com 2019 - 2024. All rights reserved.