R绘制具有相同结构的随机图

R plot random graph with same structure

我正在使用 igraph-库在 R 中创建一个随机图。

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

这将创建以下图:

如您所见,它创建了两个不同的图 - 即使给定相同的节点和边。我怎样才能让 R 绘制相同的图,这样我就可以在具有相同顺序的情况下突出显示一些 edges/nodes。

目标是创建一个随机网络,其中两个节点通过边连接的概率有一定程度(上例是 p=0.25 对应 n=12 个节点)。然后,每次我绘制该图时,都会将节点绘制在同一点上(即使节点大小不同)。

我该怎么做?请注意,我不限于 g <- erdos.renyi.game(12, 0.25) - 它只是很好地完成了随机网络的工作。

igraph 中的默认设置 layout= layout_nicely 重新计算每个图

您可以尝试将布局指定为矩阵或函数来获取坐标

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.....

例如

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

给你

layout_components

您可以找到完整列表 here

更新

您还可以通过获取一张图的坐标来确定点的位置,例如:

par(mfrow=c(2,2))
for( i in 1:4){
  g <- erdos.renyi.game(12, 0.25)
 if( i ==1) coords <- layout_components(g) # if first -- get coordinates
  g$layout <- coords
  plot(g)
}