user3079834 user3079834 - 1 month ago 6
R Question

R plot random graph with same structure

I'm creating a random graph in

R
with the
igraph
-library.

library(igraph)
g <- erdos.renyi.game(12, 0.25)
par(mfrow=c(1,2))
plot(g)
plot(g)


This creates the following plot:

random graph

As you can see, it creates two different plots - even given the same nodes and edges. How can I make R plot the same plots, so I can highlight some edges/nodes while having the same order.

The goal is to create a random network with some degree of probability that two nodes are connected by an edge (above example is
p=0.25
for
n=12
nodes). Then this graph is plotted with the nodes on the same spot (even if the node size variies) everytime I plot it.

How do I do this? Note that I'm not limited to
g <- erdos.renyi.game(12, 0.25)
- it just did the job with the random network quite well.

Answer

As default in igraph layout= layout_nicely which recalculated each plot

You can try to specify layout as matrix or as function to get coordinates

layout

Either a function or a numeric matrix. It specifies how the vertices will be placed on the plot.

If it is a numeric matrix, then the matrix has to have one line for each vertex, specifying its coordinates. The matrix should have at least two columns, for the x and y coordinates, and it can also have third column, this will be the z coordinate for 3D plots and it is ignored for 2D plots.....

For example

g <- erdos.renyi.game(12, 0.25)
g$layout <- layout_as_star
par(mfrow=c(1,2))
plot(g)
plot(g)

Give you enter image description here

or layout_componentsenter image description here

Full list you can find here

Comments