使用igraph R在整个网络上绘制子集时如何修复节点 [英] How to fix nodes when plotting a subset over a complete network using igraph R

查看:70
本文介绍了使用igraph R在整个网络上绘制子集时如何修复节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用R中提供的igraph软件包进行网络可视化时遇到问题.

I have a problem concerning network visualization using the igraph package provided in R.

假设您有一个特定的网络,其中包含节点和边的总样本.让我将此网络命名为netX:

Assume that you have a certain network, which contains the total sample of nodes and edges. Let me name this network netX:

netX <- structure(c(1, 0.48275862, 0.51724138, 0.48275862, 0.27906977, 
0.06896552, 0.34482759, 0.32352941, 0.06896552, 0.34482759, 0.03448276, 
0.06896552, 0.20689655, 0.17241379, 0.17241379, 0, 0.23333333, 
0.27586207, 0.21621622, 0.24137931, 0.48275862, 1, 0.4137931, 
0.35714286, 0.25581395, 0.25, 0.25, 0.38235294, 0.07142857, 0.28571429, 
0.21428571, 0.28571429, 0.10714286, 0.07142857, 0.21428571, 0.03571429, 
0.2, 0.32142857, 0.16216216, 0.25, 0.51724138, 0.4137931, 1, 
0.5862069, 0.34883721, 0.06896552, 0.20689655, 0.32352941, 0, 
0.27586207, 0.10344828, 0.06896552, 0.10344828, 0.10344828, 0.13793103, 
0, 0.36666667, 0.24137931, 0.21621622, 0.17241379, 0.48275862, 
0.35714286, 0.5862069, 1, 0.23255814, 0.11538462, 0.23076923, 
0.26470588, 0, 0.30769231, 0.11538462, 0.07692308, 0.11538462, 
0.15384615, 0.25, 0.03846154, 0.26666667, 0.21428571, 0.21621622, 
0.14285714, 0.27906977, 0.25581395, 0.34883721, 0.23255814, 1, 
0.18604651, 0.11627907, 0.18604651, 0, 0.18604651, 0.25581395, 
0.25581395, 0.27906977, 0.13953488, 0.20930233, 0.04651163, 0.23255814, 
0.34883721, 0.25581395, 0.30232558, 0.06896552, 0.25, 0.06896552, 
0.11538462, 0.18604651, 1, 0.13043478, 0.32352941, 0, 0.25, 0.34782609, 
0.34782609, 0.16666667, 0.13043478, 0.35714286, 0.17391304, 0.16666667, 
0.28571429, 0.16216216, 0.35714286, 0.34482759, 0.25, 0.20689655, 
0.23076923, 0.11627907, 0.13043478, 1, 0.23529412, 0, 0.29166667, 
0.04347826, 0.09090909, 0.16666667, 0.33333333, 0.21428571, 0.06666667, 
0.23333333, 0.17857143, 0.16216216, 0.21428571, 0.32352941, 0.38235294, 
0.32352941, 0.26470588, 0.18604651, 0.32352941, 0.23529412, 1, 
0.08823529, 0.29411765, 0.23529412, 0.17647059, 0.11764706, 0.14705882, 
0.26470588, 0.08823529, 0.26470588, 0.26470588, 0.32432432, 0.23529412, 
0.06896552, 0.07142857, 0, 0, 0, 0, 0, 0.08823529, 1, 0.04166667, 
0, 0, 0, 0, 0.03571429, 0, 0, 0, 0.05405405, 0.03571429, 0.34482759, 
0.28571429, 0.27586207, 0.30769231, 0.18604651, 0.25, 0.29166667, 
0.29411765, 0.04166667, 1, 0.25, 0.25, 0.20833333, 0.125, 0.25, 
0.04166667, 0.23333333, 0.17857143, 0.16216216, 0.14285714, 0.03448276, 
0.21428571, 0.10344828, 0.11538462, 0.25581395, 0.34782609, 0.04347826, 
0.23529412, 0, 0.25, 1, 0.69565217, 0.125, 0.08695652, 0.17857143, 
0, 0.16666667, 0.32142857, 0.24324324, 0.25, 0.06896552, 0.28571429, 
0.06896552, 0.07692308, 0.25581395, 0.34782609, 0.09090909, 0.17647059, 
0, 0.25, 0.69565217, 1, 0.08333333, 0.09090909, 0.17857143, 0, 
0.2, 0.35714286, 0.18918919, 0.25, 0.20689655, 0.10714286, 0.10344828, 
0.11538462, 0.27906977, 0.16666667, 0.16666667, 0.11764706, 0, 
0.20833333, 0.125, 0.08333333, 1, 0.20833333, 0.25, 0.04166667, 
0.2, 0.17857143, 0.27027027, 0.28571429, 0.17241379, 0.07142857, 
0.10344828, 0.15384615, 0.13953488, 0.13043478, 0.33333333, 0.14705882, 
0, 0.125, 0.08695652, 0.09090909, 0.20833333, 1, 0.21428571, 
0.15384615, 0.2, 0.25, 0.16216216, 0.14285714, 0.17241379, 0.21428571, 
0.13793103, 0.25, 0.20930233, 0.35714286, 0.21428571, 0.26470588, 
0.03571429, 0.25, 0.17857143, 0.17857143, 0.25, 0.21428571, 1, 
0.10714286, 0.26666667, 0.32142857, 0.37837838, 0.46428571, 0, 
0.03571429, 0, 0.03846154, 0.04651163, 0.17391304, 0.06666667, 
0.08823529, 0, 0.04166667, 0, 0, 0.04166667, 0.15384615, 0.10714286, 
1, 0.16666667, 0.10714286, 0.05405405, 0.07142857, 0.2333333, 
0.2, 0.3666667, 0.2666667, 0.2325581, 0.1666667, 0.2333333, 0.2647059, 
0, 0.2333333, 0.1666667, 0.2, 0.2, 0.2, 0.2666667, 0.1666667, 
1, 0.4, 0.2972973, 0.3, 0.2758621, 0.3214286, 0.2413793, 0.2142857, 
0.3488372, 0.2857143, 0.1785714, 0.2647059, 0, 0.1785714, 0.3214286, 
0.3571429, 0.1785714, 0.25, 0.3214286, 0.1071429, 0.4, 1, 0.1891892, 
0.4285714, 0.21621622, 0.16216216, 0.21621622, 0.21621622, 0.25581395, 
0.16216216, 0.16216216, 0.32432432, 0.05405405, 0.16216216, 0.24324324, 
0.18918919, 0.27027027, 0.16216216, 0.37837838, 0.05405405, 0.2972973, 
0.18918919, 1, 0.43243243, 0.24137931, 0.25, 0.17241379, 0.14285714, 
0.30232558, 0.35714286, 0.21428571, 0.23529412, 0.03571429, 0.14285714, 
0.25, 0.25, 0.28571429, 0.14285714, 0.46428571, 0.07142857, 0.3, 
0.42857143, 0.43243243, 1), .Dim = c(20L, 20L), .Dimnames = list(
    c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", 
    "l", "m", "n", "o", "p", "q", "r", "s", "t"), c("a", "b", 
    "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 
    "o", "p", "q", "r", "s", "t")))

进一步假设我有这个网络的某个子集,我称之为netY:

Assume further that I have a certain subset of this network, which I call netY:

sub_netX <- c("a", "d", "h", "l", "o")
netY <- netX[sub_netX,sub_netX]

netY
           a          d         h          l         o
a 1.00000000 0.48275862 0.3235294 0.06896552 0.1724138
d 0.48275862 1.00000000 0.2647059 0.07692308 0.2500000
h 0.32352941 0.26470588 1.0000000 0.17647059 0.2647059
l 0.06896552 0.07692308 0.1764706 1.00000000 0.1785714
o 0.17241379 0.25000000 0.2647059 0.17857143 1.0000000

我的目标是覆盖两个网络,以便可以在后台看到完整的网络netX,在前景看到完整的子集netY.但是首先,我们需要加载igraph程序包,确定潜在绘图的布局,并存储X和Y坐标.

My target is to overlay both networks, so that one can see the complete network netX in the background and the subset netY in the forground. But first of all we need to load the igraph package, determine the layout of the potential plot and store the X and Y coordinates.

library(igraph)

inetX <- graph.adjacency(netX, mode = "undirected", weighted = TRUE, diag=F)
inetY <- graph.adjacency(netY, mode = "undirected", weighted = TRUE, diag=F)

lay <- layout.fruchterman.reingold(inetX)
V(inetX)$x <- lay[, 1]
V(inetX)$y <- lay[, 2] 

此外,我认为获取该子集的索引并不是真正必要,但是以后进行索引编制可能会很方便:

Furthermore I think it is not really neccessary to get the index of the subset, but it can be handy for indexing later:

idx <- which(V(inetX)$name %in% c("a", "d", "h", "l", "o"))

我们还可以预先存储节点颜色:

We can also store the node colors upfront:

V(inetX)$color <- "grey"
V(inetY)$color <- "red"

但是,当我尝试在一个简单的图中覆盖两个网络时,发生的事情是,尽管坐标固定,但子集inetY的节点并未完全显示在inetX的相同节点的位置.

But what is happening when I try to overlay both networks in a simple plot is that the nodes of the subset inetY are not exactly displayed at the position of the same nodes of inetX, despite fixed coordinates.

plot.igraph(inetX, layout = lay, vertex.size = 7)
plot.igraph(inetY, layout = lay[idx,], vertex.size = 4, add = TRUE)

由于我的信誉太低,我无法上传图像,但是我希望您可以按照计算机上的步骤进行操作.

I can't upload images, as my reputation is too low, but I hope that you can follow the steps on your machines.

我对大家的问题是,为什么会发生这种情况?从覆盖两个网络时如何固定节点坐标的意义上讲,我在做错什么?

My question to you all is, why does this happen? What am I doing wrong in the sense of how can I fix node coordinates when I overlay two networks?

非常感谢您!

推荐答案

原因是igraph绘图函数默认将布局重新缩放为[-1,1] x [-1,1].您可以在plot()中使用rescale=FALSE参数,然后不执行重新缩放.

The reason is that igraph plotting functions rescale the layout to [-1,1] x [-1,1] by default. You can use the rescale=FALSE argument to plot() and then no rescaling is performed.

请注意,在这种情况下,您需要通过设置xlimylim来手动设置图的限制:

Note that in this case you'll need to set the limits of the plot by hand, by setting xlim and ylim:

xlim <- range(lay[,1])
ylim <- range(lay[,2])
plot.igraph(inetX, layout = lay, vertex.size = 20, 
            xlim = xlim, ylim = ylim, rescale = FALSE)
plot.igraph(inetY, layout = lay[idx,], vertex.size = 14, 
            add = TRUE, rescale = FALSE)

这篇关于使用igraph R在整个网络上绘制子集时如何修复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆