-1

I have created Term Document matrix from a corpus:

x  <-  TermDocumentMatrix (corpus ) 
Then I created a matrix:
Matrix <- as.matrix ( x )
Matrix [ Matrix >=1 ] <- 1
Matrix <- tcrossprod ( Matrix )

As an output: `

                     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

Then I created a graph:

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

Then used the STR function to check the graph object structure and the result are as follows:

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

The problem is the “+ edges (vertex names)”. What I want is results below from a graph that I created for a different problem.

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
jibril12
  • 83
  • 1
  • 8
  • 2
    Your question is extremely confusing and unlikely to be answered because of your iconsistent use of terms, typos, and lack of a good, [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This prevents anyone from sorting out what it is you've tried and what the problem is. Illustrations of the problem and providing your original adjacency matrix would be extremely helpful. – Forrest R. Stevens Jun 06 '15 at 17:57
  • Hi I have edited my question. I hope this is better and thank again for your helpful comment. – jibril12 Jun 06 '15 at 18:59
  • Its still confusing. How are you making your graph? You don't give any code, so we can't see what you are doing that is wrong. Where does that matrix you give at the start come from? How can we construct one like that? – Spacedman Jun 06 '15 at 22:38
  • Hi thank you again, I have completly changed my question and took your advice. I hope this makes more sense – jibril12 Jun 07 '15 at 08:25

1 Answers1

0

I think the problem you're facing is that the graph you're trying to create is by its nature undirected. Also, when creating your question (it's much better now than when it was first posted, thank you) it's very nice to supply the code to recreate the data that you're using as an example. I've included one way to do it here as a model:

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

And the structure of the resulting graph object:

> 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
Forrest R. Stevens
  • 3,435
  • 13
  • 21