tgrueter
tgrueter

Reputation: 33

Deleting nodes in R and holding the layout of the graph fixed

I try to create pictures of a network layout (to finaly illustrate how a network evovles over time). My apprach is to create a final network (see net0) and then deleting node by node (and its edges).

How can I ensure that the layout of my graph stays the same? (and does not rescale!) Final goal: Sequence of graphs with different number of nodes but the exact same layout as defined in the final graph net0

My Approach:

  1. Adding the same layout to the reduced network graph gives me the same layout but the previously deleted node does not disapaear
  2. take only a subset of the original layout, but then my graph rescales.

Code:

library(igraph)
data <- structure(c( "A","A","D","D","F","G","B","B","D","G","D","G","G","F","H","H","G","D","I","I"),.Dim = c(10L, 2L), .Dimnames = list(NULL,c("source","target")))
layout <-structure(c(-3.26,-5.50,-2.33,0.48,-3.37,-7.44,-10.00,-8.32,-5.09,-5.19,-3.15,-2.22,-1.9,-4.00),.Dim = c(7L, 2L), .Dimnames = list(NULL, c("coordinate.1", "coordinate.2")))


net0 <- graph.edgelist(data, directed=FALSE) 
par(pin=c(9, 4.7), mai=c(0.5, 0.1, 0.15, 0.1))

plot(net0, layout=layout)

net <- delete.edges(net0, c(9,10))
net <- delete.vertices(net, c("I")) 
plot(net, layout=layout)

net <- delete.edges(net0, c(9,10)) 
net <- delete.vertices(net, c("I")) 
plot(net, layout=layout[1:6,])

Upvotes: 2

Views: 667

Answers (1)

Gabor Csardi
Gabor Csardi

Reputation: 10825

Add the layout as x and y vertex attributes:

V(net0)$x <- layout[,1]
V(net0)$y <- layout[,2]

These are used automatically by plot(), see ?layout.auto, the default layout.

Upvotes: 7

Related Questions