Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

r - How to control the igraph plot layout with Fixed Positions?

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:

  1. Is this the best layout() algorithm, or can I manually assign a position for each node>
  2. How can I make sure that these nodes don't overlap in the plot (as they do here)?
  3. Can I assign one node as an "anchor" or starting point? i.e., can I make "C" the top-most or left-most node?

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...