Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you removing a vertex from igraph without changing the plotting locations

Tags:

r

igraph

I have a graph g including 100 vertices, I want to remove a number of vertices from this graph to get g1, but when I do this, I want the positions of each vertex in g to be preserved. Is it possible to do this?

par(mfrow=c(1,2))
g <- erdos.renyi.game(100, 1/100)
comps <- clusters(g)$membership
colbar <- rainbow(max(comps)+1)
V(g)$color <- colbar[comps+1]
V(g)$size<-seq(0.05,5,0.05)
plot(g, layout=layout.fruchterman.reingold, vertex.label=NA)

g1<-g - c("1","2","7","10")
plot(g1, layout=layout.fruchterman.reingold, vertex.label=NA)

A work around I have thought of, would be for me to colour in white the vertices and edges that I no longer want to see, but before embarking on this route, I was wondering whether there was something that was less of a hack.

like image 216
blJOg Avatar asked Feb 11 '23 02:02

blJOg


2 Answers

You can save the layout locations from plotting g:

locs <- layout.fruchterman.reingold(g)
plot(g, layout=locs, vertex.label=NA)

enter image description here

Then you can re-use them when plotting g1, removing the locations for the nodes that were removed:

g1<-g - c("1","2","7","10")
plot(g1, layout=locs[-c(1, 2, 7, 10),], vertex.label=NA)

enter image description here

like image 89
josliber Avatar answered Apr 25 '23 13:04

josliber


You could store the locations as vertex attributes x and y, and they will continue to be there after deleting vertices.

locs <- layout_with_fr(g)
V(g)$x <- locs[, 1]
V(g)$y <- locs[, 2]
g1 <- delete_vertices(g, c(1, 2, 7, 10))
plot(g1, vertex.label=NA)

As you can see, you will also no longer need the layout argument when plotting.

like image 27
Chris Watson Avatar answered Apr 25 '23 13:04

Chris Watson