I am trying to draw a network visualization to resemble a flow diagram. I'm fairly close with the following code, but I have a couple questions:
Thanks so much!!
library("igraph")
L3 <- LETTERS[1:8]
d <- data.frame(start = sample(L3, 16, replace = T), end = sample(L3, 16, replace = T),
weight = c(20,40,20,30,50,60,20,30,20,40,20,30,50,60,20,30))
g <- graph.data.frame(d, directed = T)
V(g)$name
E(g)$weight
ideg <- degree(g, mode = "in", loops = F)
col=rainbow(12) # For edge colors
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 = layout.reingold.tilford)
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))
A less complicated method than the above if you want to assign the node locations yourself is to add columns labelled x and y in your datasheet with the x and y coordinates for the respective nodes in those columns. e.g.
library('igraph')
nodes <- c('a','b','c','d')
x <- c(0,1,2,3)
y <- c(0,1,2,3)
from <- c('a','b','c')
to <- c('b','c','d')
NodeList <- data.frame(nodes, x ,y)
EdgeList <- data.frame(from, to)
a<- graph_from_data_frame(vertices = NodeList, d= EdgeList, directed = FALSE)
plot(a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With