Visualizing distance between nodes according to we

2020-02-11 05:01发布

问题:

I'm trying to draw a graph where the distance between vertices correspond to the edge weights* and I've founde that in graphviz there is a way to draw such graph. Is there a way to do this in R with the igraph package (specfically with graph.adkacency)?

Thanks,

Noam

  • (as once have been asked: draw a graph where the distance between vertices correspond to the edge weights)

回答1:

This is not possible as you need triangle equality for every triangle to be able to plot such an object. So you can only approximate it. For this you can use "force embedded" algorithms. There are a few in igraph. The one I often use is the Fruchterman-Reingold algorithm.

See for details:

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

Edit:

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



回答2:

Like Sacha Epskamp mentioned, unless your data is perfect, you cannot draw a graph that would not violate some triangular inequalities. However, there are techniques named Multidimensional scaling (MDS) targeted at minimizing such violations.

One implementation in R is cmdscale from the stats package. I'd recommend the example at the bottom of ?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 plot x and y using any graphics packages (you were inquiring about igraph specifically).

Finally, I'm sure you'll find plenty of other implementations if you search for "multidimensional scaling" or "MDS". Good luck.