mynet
是一个具有93个顶点和三个顶点属性的网络对象:sex
,indegree
和outdegree
。另一个网络对象simnet
是网络的模拟版本。节点和度分布相同,但是一些边缘已重新布线。
我将它们并排绘制...
par(mfrow=c(1,2))
plot(mynet, vertex.col="sex", main="mynet")
plot(simnet, vertex.col="sex", main="simnet")
...并获得以下结果:
如果我可以在两个图中固定节点位置,这将更加有用,因为它将使边缘的差异非常明显。有没有办法使用基本plot()
函数来做到这一点?如果没有,最简单的方法是不手动为每个节点输入坐标?
答案 0 :(得分:1)
有一种方法可以通过在打印前设置布局并为两个图使用相同的布局来做到这一点。我们可以使用节点名称来完成此操作,因为这些名称在每个图之间都是相同的节点。该方法有点笨拙,但似乎可行。下面的示例代码:
library(igraph)
# Make some fake networks
set.seed(42)
df1 <- data.frame(e1 = sample(1:5, 10, replace = T),
e1 = sample(1:5, 10, replace = T))
df2 <- data.frame(e1 = sample(1:5, 10, replace = T),
e1 = sample(1:5, 10, replace = T))
# the original
g1 <- graph_from_data_frame(df1, directed = F)
# the 'simulations'
g2 <- graph_from_data_frame(df2, directed = F)
# set up the plot
par(mfrow=c(1,2))
# we set the layout
lo <- layout_with_kk(g1)
# this is a matrix of positions. Positions
# refer to the order of the nodes
head(lo)
#> [,1] [,2]
#> [1,] -0.03760207 0.08115827
#> [2,] 1.06606602 0.35564140
#> [3,] -1.09026110 0.28291157
#> [4,] -0.90060771 -0.72591181
#> [5,] 0.67151585 -1.82471026
V(g1)
#> + 5/5 vertices, named, from 418e4e6:
#> [1] 5 2 4 3 1
# If the layout has names for the rows then we can
# use those names to fiddle with the order
row.names(lo) <- names(V(g1))
# plot with layout
plot(g1, layout = lo)
# plot with layout but reorder the layout to match the order
# in which nodes appear in g2
plot(g2, layout = lo[names(V(g2)), ])
由reprex package(v0.2.1)于2018-11-15创建