user1320502
user1320502

Reputation: 2570

igraph fixed node coordinates layout

I have found this answer really useful. It helps me plot network/graphs and select the coordinates for the nodes in the plot.

However, the layout rescales the coordinates to -1 to 1. First off I tried to find out how it does this but couldn't. does it do something like this??

(coordinate - mean(coordinates))/(coordinate + mean(coordinates)

Second is there a way to keep the original coordinates? I woudl like to plat axes with the graph and so would prefer not to have top rescale everything.

Upvotes: 8

Views: 8568

Answers (2)

pengchy
pengchy

Reputation: 820

  set.seed(111)
  ig <- graph_from_data_frame(as.data.frame(matrix(sample(letters,40,rep=TRUE),nc=2)))
  set.seed(123)
  ig.layout <- layout.fruchterman.reingold(ig)
  rownames(ig.layout) <- V(ig)$name
  par(bg="white",mar=c(0,0,0,0),oma=c(0,0,0,0))
  plot.igraph(ig,layout=ig.layout,vertex.color=adjustcolor("gray",alpha.f=0.5),rescale=FALSE,xlim=c(4,11),ylim=c(4,11))
  set.seed(321)
  ig.sub <- subgraph(ig,sample(V(ig)$name,5))
  plot.igraph(ig.sub,layout=ig.layout[V(ig.sub)$name,],add=TRUE,vertex.color=adjustcolor("orange",alpha.f=0.5),rescale=FALSE)

this code output the graph, where the orange node is the added laterly.

enter image description here

Upvotes: 0

Tam&#225;s
Tam&#225;s

Reputation: 48041

The answer to your first question is in the source code of the plot.igraph function; type plot.igraph in the R prompt to get the full source code. There is a part in there which says:

layout <- layout.norm(layout, -1, 1, -1, 1)

layout.norm is another function of igraph which does the magic for you; type layout.norm to see how it works.

Now, the answer to the second question is really simple; just pass rescale=F to the arguments of plot, which makes igraph skip the entire branch in plot.igraph where layout.norm is called, so it will work with your original coordinates. You can then use xlim and ylim as usual to set the limits of the X and Y axes.

Upvotes: 10

Related Questions