'How to define a graph using a distance matrix in igraph? (R)

I have a distance matrix from N points that I'd like to use to create a fully connected graph between N nodes.

So far I've tried the graph_from_adjacency_matrix function, but if my distance matrix is:

      [,1] [,2] [,3]
[1,]    0   10    3
[2,]   10    0    2
[3,]    3    2    0

then I create the graph:

g3 =  graph_from_adjacency_matrix(dist_matrix, weighted=TRUE, mode="undirected")

But when I plot it, node 2 doesn't seem to be further from node 1 than node 3 is.

Is there a way to define the graph from this matrix and then plot it (with nodes closer to each other when the distance is smaller and vice versa)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source