绘制网络时修复节点
fixing nodes when plotting networks
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()
函数来做到这一点?如果不是,最简单的方法是什么而不需要手动输入每个节点的坐标?
有一种方法可以通过在绘图前设置布局并为两个绘图使用相同的布局来实现这一点。我们可以使用节点的名称来做到这一点,因为这些是每个图形之间的相同节点。该方法有点老套,但似乎有效。示例代码如下:
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
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()
函数来做到这一点?如果不是,最简单的方法是什么而不需要手动输入每个节点的坐标?
有一种方法可以通过在绘图前设置布局并为两个绘图使用相同的布局来实现这一点。我们可以使用节点的名称来做到这一点,因为这些是每个图形之间的相同节点。该方法有点老套,但似乎有效。示例代码如下:
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