[R] Minimum Spanning Tree

Gábor Csárdi csardi at rmki.kfki.hu
Wed Apr 8 23:43:30 CEST 2009


On Wed, Apr 8, 2009 at 10:20 PM, jpearl01 <joshearl1 at hotmail.com> wrote:
>
> That's like a miracle!  The only thing that would make this graph perfect is
> if the lengths of the edges were in the same ratio as the actual edge
> lengths from the matrix.  Is it possible to alter that?

Not really. The thing is that the nodes are ordered into layers based
on the distance (in steps) from the root. This is how the
Reingold-Tilford layouting algorithm works. If you start changing the
lengths of the edges, then the nodes could move on top of each other.

You would need to modify the algorithm itself for this, I don't know
how difficult that would be.

What you can do is modifying the width of the edges, I know that is
not as good, but it is still something:

library(igraph)

tab <- read.csv("http://www.nabble.com/file/p22957493/sp_matrix.csv")
tab <- tab[,-1]

g <- graph.adjacency(as.matrix(tab), weighted=TRUE)
V(g)$label <- V(g)$name

mst <- minimum.spanning.tree(g)

rescale <- function(x,from,to) {    # linearly rescale a vector
  r <- range(x)
  (x-r[1]) / (r[2]-r[1]) * (to-from) + from
}

E(mst)$width <- rescale(E(mst)$weight,1,5)   # width between 1 and 5

lay <- layout.reingold.tilford(mst, root=which.max(degree(mst))-1)
lay <- cbind(lay[,2], lay[,1])    # rotate
x11(width=15, height=8)
plot(mst, layout=lay, vertex.size=25, vertex.size2=10,
     vertex.shape="crectangle", asp=FALSE,
     vertex.label.cex=0.7, vertex.color="white", edge.arrow.mode=0)

G.

> Thank you!!
> ~josh
>
[...]

-- 
Gabor Csardi <Gabor.Csardi at unil.ch>     UNIL DGM




More information about the R-help mailing list