8

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.

Community
  • 1
  • 1
user1320502
  • 2,510
  • 5
  • 28
  • 46

2 Answers2

10

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.

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • 1
    great thank you for your help, checking the source now. The axes limits seem to be tied together though i.e. `ylim=c(0,6)` makes the `xlim` range to 6 too. which is annoying but this is a leap forward, thanks again. – user1320502 Jun 29 '12 at 09:00
  • 2
    @user1320502: Set `asp=FALSE` to avoid the default 1:1 aspect ratio. – Gabor Csardi Oct 22 '12 at 00:58
  • Why there is nothing output when I set `rescale=FALSE`? – pengchy Dec 15 '16 at 01:39
  • Thanks! I have implemented this method with `rescale=FALSE`, and the `xlim, ylim` must be set simultaneously, because the default `xlim and ylim` is `0,1` – pengchy Dec 15 '16 at 01:47
0
  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

pengchy
  • 732
  • 2
  • 14
  • 26