igraph demonstration

References

Load packages

## network analysis
library(igraph)
## d3.js output
library(d3Network)

Data Preparation and Checking (Facebook example from Stanford)

## Read in edges information
igraphDat <- read.graph(file = "./igraph.d/facebook/0.edges", directed = FALSE)
## Simplify to remove duplications and from-self-to-self loops
igraphDat <- simplify(igraphDat, remove.multiple = TRUE, remove.loops = TRUE)
## Check edge sequence
head(E(igraphDat))
## Edge sequence:
##             
## [1] 49 --  2
## [2] 54 --  2
## [3] 55 --  2
## [4] 74 --  2
## [5] 89 --  2
## [6] 93 --  2
## Check vertex sequence
V(igraphDat)
## Vertex sequence:
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27
##  [28]  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81
##  [82]  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
## [136] 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
## [190] 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## [217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
## [244] 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## [271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
## [298] 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## [325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
## Give empty names as numbers are not useful here
V(igraphDat)$label <- ""

Default graph

## Simple plot
plot(igraphDat)

plot of chunk unnamed-chunk-4

## Use tkplot() for GUI manipulation
## tkplot(igraphDat)

Network Characteristics

## Check the degree of connectedness for each node
degree(igraphDat)
##   [1]  0 16  9 16  9 12  5 19  7 56  9  0  0 30 14  0  8 12  0 15 14 64 10 16 15 68 67  4 12 12 16 22  5  1  1  1 10
##  [38]  0  8 14 43 23  1  0  5 11  4  1 21  3 10  6  1 30  7 16 77 14  3 18  7  2 25  5  6 11 14 75  8  9  1  2 23  9
##  [75]  0 13  2  5  8 11 22  2 33  6 12 13  5 12 19  7  1  7 20  7 21  5  8  2 48 12  8 18  5 15 31 13  7  2 12 36  4
## [112] 13  2 39  0 20 16  5 35 61  3 11 62 17  3  3  6 15 27  6 15  6 15 17 18  9 21 15  1  8 10 27 42 11 14  1  9  5
## [149] 19 13 10  6  4  1  1  2 11  2 24 13  1 24  7  5  2 10  3  6 10 37 45  7 40  5  3 16 13 10 12  2 19  9  2  1 17
## [186] 25 43 15 47  6  3  2  4  4 18  8 12 15  1 46 56  3  3 56 21  1  3  2  6  0  0 29 17 38 16  0  1  7  8  5  3  7
## [223] 10 26 27  9 13 14  2  5  8 20 24  1  1  4 36  6 22 58  2  1 23  7  1  4 13  2 20 23  4 13 64  2 16  1  1 17 14
## [260]  7  7 37  3  6  4 26 17  1 10  5  3 72 44  8 13  9 17 64  9  1 42 15  1  4 15 46  1  0  3  3 13 35  0  2  2  9
## [297]  6 24 10 19  6  2 19 20 54  1  8  3 23  9 12  6 25 36 12 55  1  6 10  7 20  2 71 38 25 38 18  3  8 29 15 19 42
## [334]  7 27  0  2  8  6 26  5 11 33 17  8 15 26  6
## Degree distribution when the totality is considered 1
degree.distribution(igraphDat)
##  [1] 0.043103 0.083333 0.066092 0.051724 0.031609 0.045977 0.051724 0.045977 0.043103 0.037356 0.034483 0.020115
## [13] 0.034483 0.034483 0.022989 0.037356 0.025862 0.022989 0.014368 0.020115 0.017241 0.011494 0.008621 0.014368
## [25] 0.011494 0.011494 0.011494 0.011494 0.000000 0.005747 0.005747 0.002874 0.000000 0.005747 0.000000 0.005747
## [37] 0.008621 0.005747 0.008621 0.002874 0.002874 0.000000 0.008621 0.005747 0.002874 0.002874 0.005747 0.002874
## [49] 0.002874 0.000000 0.000000 0.000000 0.000000 0.000000 0.002874 0.002874 0.008621 0.000000 0.002874 0.000000
## [61] 0.000000 0.002874 0.002874 0.000000 0.008621 0.000000 0.000000 0.002874 0.002874 0.000000 0.000000 0.002874
## [73] 0.002874 0.000000 0.000000 0.002874 0.000000 0.002874
## Check the transitivity of a graph (probability that the adjacent vertices of a vertex are connected)
(transitivityDat <- transitivity(igraphDat, type = "localaverage",isolates = "zero"))
## [1] 0.4863
## Betweenness: number of geodesics (shortest paths) going through a vertex.
## A higher number means an more important node.
betweenness(igraphDat)
##   [1]     0.0000   238.2182     9.7186   282.2708   630.0000   451.5956     0.5000   102.2769   319.0000   541.3632
##  [11]     0.7269     0.0000     0.0000    79.6464    32.3235     0.0000    12.0362   340.9381     0.0000  9826.7005
##  [21]    20.3090  1818.7108    61.5902  7155.2399     5.9028  4690.3484   525.8304     1.3673    24.2600   195.0246
##  [31]    39.7783   935.7806     0.0000     0.0000     0.0000     0.0000     0.2500     0.0000    45.8816    70.0777
##  [41]  2929.6311  2248.6299     0.0000     0.0000     0.0000     5.1020     0.0000     0.0000   419.3746   643.0000
##  [51]    44.4824  1123.3200     0.0000   615.8833    23.8704    14.0933  1083.2322   412.9480     9.8918    12.4001
##  [61]     1.2987   315.0000    86.3685     0.0000     0.3667    45.3740     1.2789   972.9818     2.0000     6.4281
##  [71]     0.0000     0.0000    71.3438    57.7233     0.0000     7.4531     0.0000    16.6643     0.0000     0.5933
##  [81]   699.8302   322.0000   224.3822  1123.3200   213.7039    14.6949     0.0000    29.2971   295.1703   950.1667
##  [91]     0.0000   319.0000    20.9872    65.8577   558.9705     0.6667   263.2321   160.5000   207.5559   166.0315
## [101]    36.6803    23.1706     0.0000    46.6763   134.4911    30.2194    18.1960     0.0000    39.6549   234.8099
## [111]     0.0000   337.3558     0.0000   172.9316     0.0000  2480.1136  1541.1857     6.8932   287.8276  4249.1787
## [121]   156.1632   121.7173  1250.2383     7.8046  1041.5536     1.5104     8.9807   137.0250    69.5789     4.4984
## [131]   548.3993     0.0000   161.9434   359.4496    20.8427     0.0000   209.7243   364.8476     0.0000     0.1315
## [141]    12.7140    71.0202   164.2429   364.6469  1285.9976     0.0000    68.8143     0.5000   126.4900    21.3819
## [151]    71.1037     8.5555  2826.0000     0.0000     0.0000    47.1698    32.7002    20.8018   295.3215   376.4580
## [161]     0.0000   137.2308     8.3981   255.7516     0.2788     5.1420   322.0000     0.9000    50.0364   466.5232
## [171]  1598.0785   437.9777   341.0873   495.5771     0.5000 13520.8114     8.9383    39.5803   125.9937     1.0000
## [181]    39.2515   630.0000     0.5000     0.0000   206.8102    88.3594   302.9439    19.5085   340.5897     0.3667
## [191]   369.9625     0.0000  1597.0000     6.0000    14.6607     0.0000   128.4568   151.1749     0.0000   301.2872
## [201]   407.5365     0.0000   518.3720   637.6945   893.0967     0.0000     4.8683     0.0000     0.6763     0.0000
## [211]     0.0000    77.9060    24.0474   983.1753   768.7499     0.0000     0.0000    10.3329     0.0000   322.0000
## [221]     0.0000     4.2279   325.4324    20.6323   110.7948    37.8303    21.6566   622.9322     0.0000   186.0943
## [231]  2631.7041   164.0613    37.4555     0.0000     0.0000     1.7771   200.1506  1123.3200    42.5438   805.7518
## [241]   177.3315     0.0000   269.5418   373.0957     0.0000  1890.0000    50.9359     1.8348   126.8003   284.0611
## [251]     1.5195   129.6118   585.5965   160.5000     4.7864     0.0000     0.0000    69.4509    26.7245   319.0000
## [261]   462.2304   137.2083     0.0000     0.2000     0.0000    56.6915     5.7975     0.0000     1.6940   843.2958
## [271]     3.2109   822.7818  1184.7825     0.0000    36.2571   630.0000    11.0521 14607.8251    38.6303     0.0000
## [281]   731.4700   178.4754     0.0000    66.4618   186.0007   752.2129     0.0000     0.0000     0.1729    63.9529
## [291]     9.9900   458.2105     0.0000     0.0000    16.6888     5.9502     0.2000    30.0288     0.3458    40.6681
## [301]     5.1121   322.0000    71.9952    31.3242   744.4319     0.0000     0.0000  2204.2917   150.9729     0.0000
## [311]   137.8007    19.2863  1438.4500   388.2233    26.5246  1782.0149     0.0000    53.8668     7.6027   950.1667
## [321]   318.0030     0.0000  2371.4152   377.6750  1020.4217   197.8143    53.5098     0.0000     0.0000  1754.7162
## [331]     8.4566    70.0425   643.2043     3.0764   267.3763     0.0000     0.0000    67.5577     5.7033  3596.7402
## [341]     9.1343     3.5495   161.7096   471.1370     9.0575    24.8400   255.6755     3.8667
## Closeness: centrality measures how many steps is required to access every other vertex from a given vertex.
closeness(igraphDat)
##   [1] 0.000008281 0.000106462 0.000100806 0.000106338 0.000101102 0.000106090 0.000099098 0.000106542 0.000099741
##  [10] 0.000108354 0.000106918 0.000008281 0.000008281 0.000107689 0.000101102 0.000008281 0.000105719 0.000102987
##  [19] 0.000008281 0.000105652 0.000100371 0.000108554 0.000106735 0.000106315 0.000104668 0.000108968 0.000108530
##  [28] 0.000105630 0.000101071 0.000105876 0.000106496 0.000107887 0.000097333 0.000008305 0.000099771 0.000101153
##  [37] 0.000102817 0.000008281 0.000105854 0.000107411 0.000108519 0.000103423 0.000008305 0.000008281 0.000100210
##  [46] 0.000105988 0.000104482 0.000100735 0.000106826 0.000096824 0.000106883 0.000107296 0.000096787 0.000106383
##  [55] 0.000106000 0.000107193 0.000108660 0.000106975 0.000102176 0.000105775 0.000105932 0.000102881 0.000106724
##  [64] 0.000105441 0.000105108 0.000106326 0.000106180 0.000108743 0.000104526 0.000106056 0.000101492 0.000103842
##  [73] 0.000107170 0.000106033 0.000008281 0.000106485 0.000105285 0.000105441 0.000097982 0.000106667 0.000106056
##  [82] 0.000102114 0.000106986 0.000107296 0.000106895 0.000106394 0.000104482 0.000106135 0.000106895 0.000102312
##  [91] 0.000008329 0.000099741 0.000105020 0.000100190 0.000106530 0.000099088 0.000106360 0.000101513 0.000108190
## [100] 0.000105009 0.000105452 0.000104866 0.000104482 0.000106735 0.000107677 0.000107458 0.000105697 0.000101719
## [109] 0.000105574 0.000108003 0.000096665 0.000100321 0.000102187 0.000108015 0.000008281 0.000103114 0.000103745
## [118] 0.000105307 0.000107619 0.000108707 0.000106135 0.000106101 0.000108483 0.000106078 0.000106598 0.000105452
## [127] 0.000105574 0.000105753 0.000107032 0.000105363 0.000107538 0.000102103 0.000107504 0.000106815 0.000107342
## [136] 0.000102807 0.000106758 0.000100412 0.000102176 0.000104037 0.000101061 0.000107147 0.000108026 0.000104559
## [145] 0.000103713 0.000008329 0.000106213 0.000099098 0.000106998 0.000100857 0.000105831 0.000100939 0.000104351
## [154] 0.000104635 0.000096034 0.000102817 0.000105955 0.000103082 0.000107331 0.000106952 0.000102712 0.000107666
## [163] 0.000100725 0.000105496 0.000104899 0.000105809 0.000100867 0.000097352 0.000106803 0.000107991 0.000108507
## [172] 0.000105119 0.000107840 0.000103082 0.000102197 0.000108061 0.000106667 0.000104548 0.000106202 0.000008329
## [181] 0.000105186 0.000101102 0.000098319 0.000103263 0.000105966 0.000107112 0.000107980 0.000103983 0.000108190
## [190] 0.000105108 0.000105507 0.000104275 0.000099900 0.000099711 0.000104987 0.000097982 0.000105876 0.000105764
## [199] 0.000097694 0.000107677 0.000108401 0.000096656 0.000103950 0.000108448 0.000107193 0.000096787 0.000103778
## [208] 0.000105597 0.000106929 0.000008281 0.000008281 0.000106918 0.000106417 0.000108190 0.000103338 0.000008281
## [217] 0.000097116 0.000105241 0.000097982 0.000099098 0.000099900 0.000106225 0.000106508 0.000106849 0.000107262
## [226] 0.000104537 0.000101061 0.000105485 0.000102912 0.000106338 0.000104921 0.000107193 0.000107170 0.000008305
## [235] 0.000104756 0.000105341 0.000107181 0.000107296 0.000106315 0.000108413 0.000105031 0.000093897 0.000106213
## [244] 0.000100251 0.000008305 0.000102934 0.000106587 0.000103466 0.000107724 0.000106168 0.000105988 0.000105541
## [253] 0.000108436 0.000101513 0.000104745 0.000093897 0.000008305 0.000106986 0.000106326 0.000099741 0.000106225
## [262] 0.000106712 0.000099900 0.000104493 0.000096665 0.000106781 0.000104789 0.000102796 0.000105798 0.000105563
## [271] 0.000105652 0.000108637 0.000108342 0.000097982 0.000106349 0.000101102 0.000106610 0.000109745 0.000104537
## [280] 0.000097182 0.000108284 0.000105697 0.000008305 0.000105630 0.000105831 0.000108260 0.000098863 0.000008281
## [289] 0.000106000 0.000102459 0.000107273 0.000108026 0.000008281 0.000102187 0.000106045 0.000105496 0.000104493
## [298] 0.000107101 0.000105932 0.000104428 0.000104548 0.000104112 0.000105753 0.000106474 0.000108389 0.000099820
## [307] 0.000097982 0.000106372 0.000107677 0.000102807 0.000100261 0.000106406 0.000103135 0.000107377 0.000106011
## [316] 0.000108637 0.000102585 0.000105809 0.000106033 0.000102312 0.000105708 0.000104460 0.000108849 0.000108061
## [325] 0.000107701 0.000108015 0.000101153 0.000099069 0.000097982 0.000107400 0.000105042 0.000106758 0.000108413
## [334] 0.000100140 0.000107759 0.000008281 0.000105452 0.000100210 0.000105787 0.000107770 0.000105419 0.000106372
## [343] 0.000107840 0.000103018 0.000105563 0.000106440 0.000105731 0.000105319
## Average path length between any two given nodes
(averagePathLength <- average.path.length(igraphDat))
## [1] 3.752
## Community structure detection based on edge betweenness
(communityEdgeBetwn <- edge.betweenness.community(igraphDat))
## Graph community structure calculated with the edge betweenness algorithm
## Number of communities (best split): 51 
## Modularity (best split): 0.4161 
## Membership vector:
##   [1]  1  2  3  4  5  4  6  4  7  4  4  8  9  4  3 10  4  3 11  3  3  4  4 12  2  4  4 13  3  4  4  4  3 14 15 12 16
##  [38] 17  4  4  4  3 14 18  3  4 12 19  2 20  4 21 20  2 22  4  4  2 23  4  4  7  4  4 24  4  4  4 12  4 25 25  4  2
##  [75] 26  4 27  4  5  4  2 23  4 21  4  4 12  4  4  6 28  7  2  3  2  6  4 25  4 12 24  2 12  4  4  4  4 23 16  4  7
## [112]  3  3  4 29  3  3  4  4  4 30  4  4  4 12  4  2 16  4  4  2 12  4  4  4 16  4  3  3 16  3  4  4 12  3 28  4  6
## [149]  4  3 24  3  5 31  6 12  4 12  4 16 32  4  3 24 33  4 15  3  4  4  4 24  4 15  3 12  4 12  4 28  2  5 25 34 16
## [186]  4  4  2  4 24 35  2 20  7  2  5  2 16 15  4  4  7 15  4  2 20 36 37  4 38 39  4  4  4  3 40  3 24  5  6  3  4
## [223]  4  4  4 12  3 12 41  4 25  4  4 42 43  4  4 21  4  4 12 20  2  3 44  7  4 30  4  2  4 16  4 25  2 20 42  4  4
## [260]  7 36  4  3 12  7  4  2 12  4 23 27  4  4  5  4  5  4  4 12  3  4 16 44 41 16  4 23 45  4  3  4  4 46  3 47  4
## [297] 12  4  4  2  2 19  2  4  4  3  5 25  4 16  3  4  3  4  4  4 48 49  4  6 16 12  4  4  4  4  3  6  5  4  2  4  4
## [334]  3  4 50 51  3  4  4  4  4  4  3  4  4  2  4

Different graph layouts

## auto
plot(igraphDat, layout = layout.auto)

plot of chunk unnamed-chunk-6

## random
plot(igraphDat, layout = layout.random)

plot of chunk unnamed-chunk-6

## circle
plot(igraphDat, layout = layout.circle)

plot of chunk unnamed-chunk-6

## sphere
plot(igraphDat, layout = layout.sphere)

plot of chunk unnamed-chunk-6

## fruchterman.reingold
plot(igraphDat, layout = layout.fruchterman.reingold)

plot of chunk unnamed-chunk-6

## kamada.kawai
plot(igraphDat, layout = layout.kamada.kawai)

plot of chunk unnamed-chunk-6

## spring
plot(igraphDat, layout = layout.spring)

plot of chunk unnamed-chunk-6

## reingold.tilford
## plot(igraphDat, layout = layout.reingold.tilford)
## fruchterman.reingold.grid
plot(igraphDat, layout = layout.fruchterman.reingold.grid)

plot of chunk unnamed-chunk-6

## lgl
plot(igraphDat, layout = layout.lgl)

plot of chunk unnamed-chunk-6

## graphopt
plot(igraphDat, layout = layout.graphopt)

plot of chunk unnamed-chunk-6

## svd
plot(igraphDat, layout = layout.svd)

plot of chunk unnamed-chunk-6

## norm
plot(igraphDat, layout = layout.norm)
## Error: `layout' not a matrix

Graph with characteristics

## Color by group membership
plot.igraph(igraphDat, vertex.color = communityEdgeBetwn$membership)

plot of chunk unnamed-chunk-7

## Size by number of edges
plot.igraph(igraphDat, vertex.color = communityEdgeBetwn$membership, vertex.size = log(degree(igraphDat) + 1))

plot of chunk unnamed-chunk-7

## Add community indicating background colors
plot.igraph(igraphDat, vertex.color = communityEdgeBetwn$membership, vertex.size = log(degree(igraphDat) + 1),
            mark.groups = by(seq_along(communityEdgeBetwn$membership), communityEdgeBetwn$membership, invisible))
title("Stanford Facebook data", sub = "http://snap.stanford.edu/data/egonets-Facebook.html")
text(x = -1, y = -1, labels = sprintf("Average path length: %.2f\nTransitivity: %.2f",
                         averagePathLength, transitivityDat))

plot of chunk unnamed-chunk-7

The node sizes represent the number of edges. The background color (and node colors) are grouping by algorithmically detected “communities”. “Average path length” is the average number of nodes necessary to connect arbitrary two-person pair. The transitivity is the probability that people connected to the same pivot person A are interconnected.

d3SimpleNetwork(Data = read.table(file = "./igraph.d/facebook/0.edges"),
                height = 1000, width = 1000, standAlone = FALSE, charge = -50)