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

2019-07-20 11:20发布

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?

enter image description here

标签: r polygon igraph
0条回答
登录 后发表回答