3

I want to create a visualization of a graph at 4 steps, i.e. different points in time. The positions of my vertices (nodes) should always stay the same (use the positions of the full graph). All I want is to remove some vertices from the R igraph graph. What seems to be an issue is that the vertices names change.

# Erdos
par(mfrow=c(1,3))
g <- erdos.renyi.game(20, 1/20)
locs <- layout.fruchterman.reingold(g)
V(g)$name <- V(g)
# In the original file, vector names look like this (not "1,2,3,4...):
V(g)$name <- as.vector(c(8,9,3,5,13,6,7,1,2,18,11,12,16,14,15,4,17,10,20,19))
V(g)$name

plot(g, 
     layout=locs, 
     main="Original")

# Remove a few vertices
removals1 <- c("12","2","9","11","4")
g2 <- delete.vertices(g,removals1)
plot(g2, 
     layout=locs[-as.numeric(removals1),], 
     main="Removals")

# Remove some more
removals2 <- c("15","14","7","8","5","19","10")
g3 <- delete.vertices(g2,removals2)
plot(g3, 
     layout=locs[-as.numeric(c(removals1,removals2)),], 
     main="More Removals")

I would be really happy to find a solution here. Maybe, there are also far more elegant solution as the one above. Thanks!

Rnaldinho
  • 421
  • 2
  • 6
  • 15

2 Answers2

4

Instead of using the delete which was somehow moving the vertices around (I couldn't even completly overlay the graph, labels not withstanding), it is better to use induced_subgraph. I do not why this is the case but it seems to work.

# Erdos
g <- erdos.renyi.game(20, 1/20)
locs <- layout.fruchterman.reingold(g)
V(g)$name <- V(g)
# In the original file, vector names look like this (not "1,2,3,4...):
V(g)$name <- as.vector(c(8,9,3,5,13,6,7,1,2,18,11,12,16,14,15,4,17,10,20,19))
V(g)$name

par(mfrow=c(1,3))
plot(g, 
     layout=locs, 
     main="Original")

# Remove a few vertices
removals1 <- c("12","2","9","11","4")
g2 <- induced_subgraph(g, V(g)[-as.numeric(removals1)])
plot(g2, 
     layout=locs[-as.numeric(removals1),], 
     main="Removals")

# Remove some more
removals2 <- c("15","14","7","8","5","19","10")
g3 <- induced_subgraph(g, V(g)[-as.numeric(c(removals1, removals2))])
plot(g3, 
     layout=locs[-as.numeric(c(removals1,removals2)),], 
     main="More Removals")

enter image description here

emilliman5
  • 5,816
  • 3
  • 27
  • 37
1

@emiliman5's answer is spot on; however, I want to show a slightly different solution and the problem with the original.

The line locs[-as.numeric(removals1),] is not removing vertices c("12","2","9","11","4"), but rather rows c("12","2","9","11","4").

Compare:

> head(cbind(as_ids(V(g)),locs))
     [,1] [,2]               [,3]              
[1,] "8"  "42.1520624498397" "29.0822309512088"
[2,] "9"  "42.9864581422991" "28.6882159221222"
[3,] "3"  "42.9653898313169" "30.9232356041607"
[4,] "5"  "46.6704380162041" "29.7404624492056"
[5,] "13" "47.4190242396939" "28.5469829852443"
[6,] "6"  "46.6173689817953" "25.6916967155951"

To:

> head(cbind(as_ids(V(g2)),locs[-as.numeric(removals1),]))
     [,1] [,2]               [,3]              
[1,] "8"  "42.1520624498397" "29.0822309512088"
[2,] "3"  "42.9653898313169" "30.9232356041607"
[3,] "5"  "47.4190242396939" "28.5469829852443"
[4,] "13" "46.6173689817953" "25.6916967155951"
[5,] "6"  "44.3293887668239" "30.6957434444784"
[6,] "7"  "47.4947062707832" "27.0391131188028"

Notice that the coordinates for node 5 are different. Check to see if the x-coordinates and y-coordinates match after subsetting:

> r1 <- cbind(as_ids(V(g)),locs)
> r2 <-cbind(as_ids(V(g2)),locs[-as.numeric(removals1),])
> r1[match(r2[,1],r1[,1]),2] == r2[,2]
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
> r1[match(r2[,1],r1[,1]),3] == r2[,3]
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

As an alternative to the induced_subgraph solution, subset the locs matrix by the index of vertex ids as such:

plot(g2, 
     layout=(locs[-which(as_ids(V(g)) %in% removals1),]), 
     main="Removals")

The line which(as_ids(V(g)) %in% removals1) gets the row index of the specified vertices:

> which(as_ids(V(g)) %in% removals1)
[1]  2  9 11 12 16

Vertex "9" is in row 2, vertex "2" is in row 9, etc.

For the third graph:

removals2 <- c("15","14","7","8","5","19","10")
g3 <- delete.vertices(g2,removals2)
plot(g3, 
     layout=locs[-locs[-which(as_ids(V(g)) %in% c(removals1, removals2)),], 
     main="More Removals")
paqmo
  • 3,649
  • 1
  • 11
  • 21