8

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

Community
  • 1
  • 1
noamac
  • 123
  • 1
  • 5

2 Answers2

4

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.

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
3

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.

flodel
  • 87,577
  • 21
  • 185
  • 223
  • 2
    Also note that there is a function called `layout.mds` in `igraph` which seems to perform MDS on a full distance matrix to obtain the layout coordinates. – Tamás Feb 26 '12 at 18:37