Visualizing distance between nodes according to weights - with R

穿精又带淫゛_ 提交于 2019-12-29 06:59:47

问题


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.



来源:https://stackoverflow.com/questions/9452221/visualizing-distance-between-nodes-according-to-weights-with-r

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