Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

networking - How to fix nodes when plotting a subset over a complete network using igraph R

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

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")))

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

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"

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?

Thank you very much in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...