2014-01-23 168 views
13

我試圖用igraph包繪製(稀疏)加權圖。我目前有一個鄰接矩陣,但不能讓graph.adjacency函數識別邊的權重。igraph創建加權鄰接矩陣

考慮下面的隨機對稱矩陣:

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文檔的閱讀,我應該做到以下幾點:

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

但是,它不承認邊權:

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) 

enter image description here

如何獲得igraph識別邊緣權重?

+2

這似乎並不成爲問題,但請注意,您在上面打印的矩陣不是對稱的。 (嘗試'isSymmetric(m)',然後比較'm [5,3]'和'm [3,5]'的值。) –

+0

@ JoshO'Brien,它似乎是一個簡單的複製和粘貼錯誤,A和E列中的數字有8個小數位,而其餘的有7個。除此之外它是對稱的:'isSymmetric(round(m,6))== TRUE'。有趣的是,'igraph'保留了小數位數最多的版本,並且在數字末尾加上了小數點後7位的差異時加上了0。 – TWL

+2

權重在那裏,'weight(e/n)'意味着有一個稱爲'weight'的邊緣屬性,它是數字。請參閱'?print.igraph'。但是它們不是默認繪製的,你需要將它們添加爲'edge.label'。 –

回答

25

權重在那裏,weight (e/n)意味着有一個稱爲weight的邊緣屬性,它是數字。見?print.igraph。但是它們不是默認繪製的,你需要將它們添加爲edge.label。

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

graph plot screenshot

爲繪製,請務必閱讀?igraph.plotting

3

可以與E(ig)$weight提取邊緣的權重,並將其分配到在繪圖功能的edge.width參數:

plot(ig, edge.width=E(ig)$weight)

參見?igraph.plotting [link]以供參考。

另請注意,在本例中,權重將對應於邊緣的寬度,因此它們應該是>= 0

+0

邊緣權重可以是負值,AFAIK沒有任何問題。 –

+0

@GaborCsardi,我並不是說邊緣權重不能是負數。在我的答案中,他們應該積極地被視爲**邊緣寬度**。因此,我發現-1票有點倉促。儘管如此,爲了避免混淆,我編輯了答案。 – TWL

+0

我明白了,對不起,誤會了。 –

8

儘管我喜歡igraph,但我發現qgraph軟件包更容易繪製加權網絡。

使用鄰接矩陣,您也可以使用qgraph庫中的qgraph()來繪製它。它會自動將負邊緣顏色變爲紅色,正邊緣變成綠色。

install.packages('qgraph') 
require(qgraph) 
qgraph(m) 
qgraph(m,edge.labels=TRUE) #if you want the weights on the edges as well 

qgraph建立在igraph上,但只是爲你做了一切。

+1

供參考:你**可以**在igraph有負邊緣。另外,'qgraph'只是調用'igraph' AFAIK。 –

+0

我意識到qgraph調用igraph。用它來繪製加權網絡更容易,因爲它在後臺爲你做了一切。 –

+0

當然,我同意這很有用。 -1是因爲你暗示igraph無法處理消極的邊緣,這是錯誤的信息。我編輯的 –

7

@ TWL的解決方案可以很容易地推廣,以表示邊的寬度作爲權重的函數,包括負權重。訣竅是通過將最小權重值(可選地代表最小權重寬度的偏移量)相加來轉換所有權重。例如:

# reproducible example: 
set.seed(12345) 
a <- matrix(runif(5*5, min=-10, max=10), ncol=5) 
diag(a) <- 0 # remove loops. 
>a 
      [,1]  [,2]  [,3]  [,4]  [,5] 
[1,] 0.0000000 -6.6725643 -9.309291 -0.7501069 -0.9254385 
[2,] 7.5154639 0.0000000 -6.952530 -2.2371204 -3.4649518 
[3,] 5.2196466 0.1844867 0.000000 -1.9502972 9.3083065 
[4,] 7.7224913 4.5541051 -9.977268 0.0000000 4.1496375 
[5,] -0.8703808 9.7947388 -2.175933 9.0331751 0.0000000 

# create igraph object. 
g <- graph.adjacency(a, mode="undirected", weighted=TRUE) 
plot(g) 

# assign edge's width as a function of weights. 
E(g)$width <- E(g)$weight + min(E(g)$weight) + 1 # offset=1 
plot(g) 

enter image description here