igraph创建加权邻接矩阵 [英] igraph creating a weighted adjacency matrix

查看:455
本文介绍了igraph创建加权邻接矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用igraph包绘制一个(稀疏的)加权图.我目前有一个邻接矩阵,但是无法使用graph.adjacency函数来识别边缘权重.

I'm trying to use the igraph package to draw a (sparse) weighted graph. I currently have an adjacency matrix, but cannot get the graph.adjacency function to recognise the edge weights.

考虑以下随机对称矩阵:

Consider the following random symmetric matrix:

m <- read.table(row.names=1, header=TRUE, text=
"           A          B          C          D           E         F
A 0.00000000  0.0000000  0.0000000  0.0000000  0.05119703 1.3431599
B 0.00000000  0.0000000 -0.6088082  0.4016954  0.00000000 0.6132168
C 0.00000000 -0.6088082  0.0000000  0.0000000 -0.63295415 0.0000000
D 0.00000000  0.4016954  0.0000000  0.0000000 -0.29831267 0.0000000
E 0.05119703  0.0000000 -0.6329541 -0.2983127  0.00000000 0.1562458
F 1.34315990  0.6132168  0.0000000  0.0000000  0.15624584 0.0000000")
m <- as.matrix(m)

要进行绘图,首先我必须将此邻接矩阵转换为正确的igraph格式.对于graph.adjacency,这应该相对简单.根据我对 graph.adjacency 的文档的阅读,以下:

To plot, first I must get this adjacency matrix into the proper igraph format. This should be relatively simple with graph.adjacency. According to my reading of the documentation for graph.adjacency, I should do the following:

library(igraph)
ig <- graph.adjacency(m, mode="undirected", weighted=TRUE)

但是,它不能识别边缘权重:

However, it doesn't recognise the edge weights:

str(ig)
# IGRAPH UNW- 6 8 -- 
# + attr: name (v/c), weight (e/n)
# + edges (vertex names):
# [1] A--E A--F B--C B--D B--F C--E D--E E--F
plot(ig)

如何让igraph识别边缘权重?

How do I get igraph to recognise the edge weights?

推荐答案

权重在这里,weight (e/n)表示存在一个称为权重的边属性,它是数字.参见?print.igraph.但是默认情况下不会绘制它们,您需要将它们添加为edge.label.

The weights are there, weight (e/n) means that there is an edge attribute called weight, and it is numeric. See ?print.igraph. But they are not plotted by default, you need to add them as edge.label.

plot(ig, edge.label=round(E(ig)$weight, 3))

要进行绘图,请确保已阅读?igraph.plotting.

For plotting, make sure you read ?igraph.plotting.

这篇关于igraph创建加权邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆