Visualization of the distance between nodes in accordance with the weights - with R

I am trying to draw a graph where the distance between the vertices corresponds to the weights of the edges *, and I found that in graphviz there is a way to draw such a graph. Is there a way to do this in R with the igraph package (specfically with graph.adkacency)?

Thanks,

Noam

+6
source share
2 answers

This is impossible, since for each triangle a triangular equality is required in order to be able to construct such an object. Therefore, you can only approach it. You can use "forced built-in" algorithms for this. There are several in the game. I often use the Fruchtermann-Reingold algorithm.

See details

library("igraph") ?layout.fruchterman.reingold 

Edit:

Note that the distance between the nodes will somewhat correspond to the inverse of the absolute weight of the edge.

+4
source

As Sasha Epscamp mentioned, if your data is not perfect, you cannot draw a graph that does not violate some triangular inequalities. However, there are methods called Multidimensional Scaling (MDS) , aimed at minimizing such violations.

One implementation in R is equal to cmdscale from the stats package. I would recommend the example below ?cmdscale :

 > require(graphics) > > loc <- cmdscale(eurodist) > x <- loc[,1] > y <- -loc[,2] > plot(x, y, type="n", xlab="", ylab="", main="cmdscale(eurodist)") > text(x, y, rownames(loc), cex=0.8) 

Of course, you can draw x and y using any graphic packages (you specifically asked a question about igraph ).

Finally, I’m sure that you will find many other implementations if you look for “multidimensional scaling” or “MDS”. Good luck.

+3
source

Source: https://habr.com/ru/post/909384/


All Articles