我已从语料库中创建了术语文档矩阵:
x <- TermDocumentMatrix (corpus )
Then I created a matrix:
Matrix <- as.matrix ( x )
Matrix [ Matrix >=1 ] <- 1
Matrix <- tcrossprod ( Matrix )
作为输出: `
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
问题是“+ edge(顶点名称)”。我想要的是下面从我为不同问题创建的图表中得到的结果。
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
答案 0 :(得分: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)
)
结果图对象的结构:
> 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