2015-06-06 40 views
-1

我已創建期限文獻矩陣 '邊被從語料庫創建錯誤

     Terms 
       Terms best love fun don’t games 
       best 31  0 1 2  0 
       love 0  48 1 2  6 
       fun  1  1 39 1  1 
       dont 2  2 1 46  7 
       games 0  6 1 7  54 

然後創建了一個曲線圖:

G <- graph.adjacency(Matrix, mode = "directed", weighted = TRUE) 

然後用STR函數檢查圖形對象結構,結果如下:

Str(G) 
    IGRAPH DNW- 25 452 -- 
    + attr: name (v/c), weight (e/n) 
    + edges (vertex names): 
    1 -> 3, 4, 6, 7, 8, 11, 14, 15, 20, 21, 23, 24 
    2 -> 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 24, 25 
    3 -> 1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 24, 25 
    4 -> 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 21,  
    5 -> 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18, 20, 21, 22, 23, 24, 25 

問題是「+邊(頂點名稱)」。我想要的是我爲另一個問題創建的圖表的結果。

IGRAPH DNW- 20 31 -- 
+ attr: name (v/c), weight (e/n) 
+ edges (vertex names): 
[1] 1 ->2 1 ->3 1 ->5 2 ->4 
[5] 2 ->5 3 ->6 3 ->9 4 ->8 
[9] 5 ->7 5 ->20 6 ->9 7 ->8 
[13] 7 ->9 8 ->9 9 ->10 10->11 
[17] 10->15 20->1 11->12 11->13 
+2

你提的問題是非常混亂,不太可能回答是因爲你對術語,拼寫錯誤以及缺乏一個好的[可重現的例子]的圖標一致性(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) 。這可以防止任何人分清你嘗試過的是什麼以及問題是什麼。問題的插圖和提供原始鄰接矩陣將非常有幫助。 –

+0

嗨,我編輯了我的問題。我希望這是更好的,並再次感謝您的有用評論。 – jibril12

+0

它仍然令人困惑。你如何製作圖表?你不給任何代碼,所以我們不能看到你在做什麼是錯的。你在開始時給出的矩陣來自哪裏?我們如何構建這樣的一個? – Spacedman

回答

0

我認爲你面臨的問題是你試圖創建的圖形本質上是無向的。此外,在創建您的問題時(現在比首次發佈時好多了,謝謝),提供代碼來重新創建您使用的數據作爲示例是非常好的。我已經包括了一個辦法,在這裏做一個模型:

m <- read.table(header=F, text=" 
31 0 1 2  0 
0  48 1 2  6 
1  1 39 1  1 
2  2 1 46  7 
0  6 1 7  54 
") 

m <- as.matrix(m) 
n <- c("best", "love", "fun", "don't", "games") 
rownames(m) <- n 
colnames(m) <- n 


library(igraph) 

## Create graph from adjacency matrix as an "undirected" graph: 
g <- graph.adjacency(m, mode = "undirected", weighted = TRUE) 

plot.igraph(g, 
    vertex.label=V(g)$name, 
    vertex.size=80, 
    vertex.color="lightgreen", 
    vertex.label.cex=1.0, 
    vertex.label.color="black", 
    vertex.label.family="sans", 
    edge.color="black", 
    edge.width=sqrt(E(g)$weight) 
) 

graph

,所得圖形對象的結構:

> E(g) 
Edge sequence: 

[1] best -- best 
[2] fun -- best 
[3] don't -- best 
[4] love -- love 
[5] fun -- love 
[6] don't -- love 
[7] games -- love 
[8] fun -- fun 
[9] don't -- fun 
[10] games -- fun 
[11] don't -- don't 
[12] games -- don't 
[13] games -- games 
+0

嗨福雷斯特,謝謝你的工作。 – jibril12