Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualizing distance between nodes according to weights - with R

Tags:

r

distance

igraph

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)
like image 647
noamac Avatar asked Feb 26 '12 10:02

noamac


2 Answers

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.

like image 108
Sacha Epskamp Avatar answered Oct 26 '22 03:10

Sacha Epskamp


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.

like image 32
flodel Avatar answered Oct 26 '22 04:10

flodel