How to fix nodes when building a subset in a complete network using igraph R

I have a problem with network rendering using the igraph package provided in R.

Suppose you have a specific network that contains a complete selection of nodes and edges. Let me name this netX network:

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

Suppose further that I have a specific subset of this network that 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 goal is to overlay both networks so that you can see the full network netX in the background and a subset of netY in forground. But first of all, we need to download the igraph package, determine the layout of the potential graph and save 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] 

In addition, I think that it is not necessary to get the subset index, but it may be convenient for indexing later:

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

node:

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

, , , , inetY inetX, .

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

, , , .

, ? , node, ?

!

+4
1

, [-1,1] x [-1,1]. rescale=FALSE plot(), .

, , xlim 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

+4

Source: https://habr.com/ru/post/1545192/


All Articles