问题描述
假设我们有一个邻接矩阵,可能是这样的:
A1 A2 A3
A1 0 1 0
A2 1 0 2
A3 0 2 0
您将如何使用 igraph
创建反映人与人之间联系数量的边权重?
我尝试了 E(matrix)$weight <- degree(matrix)
,但我怀疑这在技术上不正确?相反,我只想让邻接矩阵中的值反映边缘权重。
解决方法
您并不清楚您希望如何描绘重量。这列出了边上节点的权重。
g1 <- graph_from_adjacency_matrix(
adjmatrix = matrix(c(0,1,2,0),nrow = 3,ncol = 3,byrow = T,dimnames = list(c("A1","A2","A3"),("A1","A3"))
) # end matrix
) # end graph_from
plot(g1,edge.label = E(g1)$weight) # plot with edge weights
,
您可以尝试在weighted = TRUE
中启用graph_from_adjacency_matrix
,例如
g <- graph_from_adjacency_matrix(adjmat,"undirected",weighted = TRUE)
plot(g,edge.width = E(g)$weight,edge.label = E(g)$weight)
给出
数据
> dput(adjmat)
structure(c(0,.Dim = c(3L,3L),.Dimnames = list(
c("A1",c("A1","A3")))