The layout in igraph is defined in a matrix with 2 columns and a row for each node. The first column indicates its x position and the second its y position, and scale is not relevant (it is always rescaled to fit a -1 to 1 plotting area. You can get this layout before plotting by just calling the layout function on the graph:
l <-layout.reingold.tilford(g)
l
[,1] [,2]
[1,] 0 0
[2,] -1 3
[3,] 0 1
[4,] 0 3
[5,] 0 2
[6,] 0 4
[7,] 1 3
This way you can change it in any way you want manually, and then send it to the plot:
plot.igraph(g,
vertex.label = V(g)$name, vertex.label.color = "gray20",
vertex.size = ideg*25 + 40, vertex.size2 = 30,
vertex.color = "gray90", vertex.frame.color = "gray20",
vertex.shape = "rectangle",
edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
edge.curved = T,
layout = l)
It also seems that you can set the argument params
to control the layout abit. This is a list containing an argument root
that apparently can be used to set the root of the graph. Assign this a number of the node (renember that igraph uses C like indexes for nodes, first one is 0). So setting the root at "C":
l <- layout.reingold.tilford(g,params=list(root=2))
EDIT: Also the RGraphViz
has some nice tree-layouts in it that might be worth checking out.
EDIT 2:
This is a modified snippet of the source codes from my package, which uses a same kind of layout matrix to define placement of nodes in a graph, that you might find useful:
gridLayout <- function(x)
{
LmatX <- seq(-1,1,length=ncol(x))
LmatY <- seq(1,-1,length=nrow(x))
loc <- t(sapply(1:max(x),function(y)which(x==y,arr.ind=T)))
layout <- cbind(LmatX[loc[,2]],LmatY[loc[,1]])
return(layout)
}
What this function does is transform a matrix specifying the layout in a grid (similar to layout()
) to a two-column layout with x and y positions. Define a matrix of zeros and for each node integer from 1 to the total number of nodes ( this is the igraph ID + 1 ).
For example, for a silly 4 node graph:
grid <- matrix(c(
0,0,1,0,0,
2,0,3,0,4),nrow=2,byrow=TRUE)
library("igraph")
g <- graph.adjacency(matrix(1,4,4))
plot(g,layout=gridLayout(L))