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.
You can save the layout locations from plotting g
:
locs <- layout.fruchterman.reingold(g)
plot(g, layout=locs, vertex.label=NA)
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With