Is it possible to map a matrix to a igraph object?

こ雲淡風輕ζ 提交于 2019-12-24 01:10:00

问题


I have a matrix A that define ordered segments of a self-intersecting polygon:

A <- t(matrix(c(
         0,  0  ,
         1,  0  ,
         1, -2  ,
        -2, -2  ,
        -2, -1  ,
         0, -1  ,
         0, -4  ,
        -1, -4  ,
        -1, -2  ,
         2, -2  ,
         2, -3  ,
         0, -3  ,
         0, 0), nrow = 2));

par(mfrow=c(1,3)) 

plot(A, col='red', type= 'l', xlim=c(min(A[,1]),max(A[,1])),
      ylim=c(min(A[,2]),max(A[,2])), xlab='x', ylab='y'); 
points(A, col='black', pch = 22); 
grid()  

I heed to map the matrix A to an undirected graph where a point (x,y) corresponds to a vertex and a segment between the "neighboring" points corresponds to an edge. The neighboring points (by distance but not id's number) connected by red lines on the right fugure.

Edit. After the user20650's comment I have mapped the matrix to the undirected graph (middle graph on the figure). The undirected graph looks like an expected result. But with the edge.curved=TRUEoption (rigth fugure) we see the edges (3,4), (6,7), (9,10) and (12, 13).

library(igraph)
g <- make_empty_graph(n=nrow(A)); 
g <- g + path(seq_len(nrow(A))); 
plot(as.undirected(g), layout=as.matrix(A))
plot(g, layout=as.matrix(A), edge.curved=TRUE)

The length of edges must be equal to 1. Based on the condition we should add 5 vetries to the graph g and corresponded edges.

I can delete the edge (3,4) and add edges (3,9) and (9,4) and so on for pairs (12, 13), (9,10) and (6,7).

Question. Is there exist a way to such mapping?

来源:https://stackoverflow.com/questions/55826738/is-it-possible-to-map-a-matrix-to-a-igraph-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!