0. 说明

我们接着讲更多关于对igraph对象的操作,参考Statistical Network Analysis with igraph第一章。

1. 创建igraph 对象

使用管道

补充:更为实际的案例中,需要使用数据集来创建图。igraph作者提供了一些根据数据集创建好的igraph对象:

2. 使用iraph对象查看边和点的信息

## IGRAPH f7130f3 DN-- 45 463 -- 
## + attr: Citation (g/c), Author (g/c), shape (v/c), name (v/c)
## + edges from f7130f3 (vertex names):
##  [1] V1 ->V2     V1 ->V3     V1 ->V3A    V1 ->V4     V1 ->V4t    V1 ->MT    
##  [7] V1 ->PO     V1 ->PIP    V2 ->V1     V2 ->V3     V2 ->V3A    V2 ->V4    
## [13] V2 ->V4t    V2 ->VOT    V2 ->VP     V2 ->MT     V2 ->MSTd/p V2 ->MSTl  
## [19] V2 ->PO     V2 ->PIP    V2 ->VIP    V2 ->FST    V2 ->FEF    V3 ->V1    
## [25] V3 ->V2     V3 ->V3A    V3 ->V4     V3 ->V4t    V3 ->MT     V3 ->MSTd/p
## [31] V3 ->PO     V3 ->LIP    V3 ->PIP    V3 ->VIP    V3 ->FST    V3 ->TF    
## [37] V3 ->FEF    V3A->V1     V3A->V2     V3A->V3     V3A->V4     V3A->VP    
## [43] V3A->MT     V3A->MSTd/p V3A->MSTl   V3A->PO     V3A->LIP    V3A->DP    
## + ... omitted several edges

原作者是这么解释的:


This is the standard way of showing (printing) an igraph graph object on
the screen. The top line of the output declares that the object is an igraph
graph, and also lists its most important properties. A four-character long
code is printed first:

‘D/U’ The first character is either ‘D’ or ‘U’ and encodes whether the graph
is directed or undireted.
‘N’ The second letter is ‘N’ for named graphs (see Section 1.2.5). A dash
here means that the graph is not named.
‘W’ The third letter is ‘W’ if the graph is weighted (in other words, if the
graph is a valued graph, Section 2.4). Unweighted graphs have a dash in
this position.
‘B’ Finally, the fourth is ‘B’ if the graph is bipartite (two-mode, Section ??).
For unipartite (one-mode) graphs a dash is printed here.

This notation might seem quite dense at first, but it is easy to get used to and
conveys much information in a small space. Then two numbers are printed,
these are the number of vertices and the number of edges in the graph, 45
and 463 in our case. At the end of the line the name of the graph is printed,
if there is any. The next line(s) list attributes, meta-data that belong to the
vertices, edges or the graph itself. Finally, the edges of the graph are listed.
Except for very small graphs, this list is truncated, so that it fits to the screen.

一些基本量的展示,之前讲过,此外,还有更多关于边的操作:

3. 子图

创建子图

## IGRAPH cb88d15 DN-- 16 156 -- 
## + attr: Citation (g/c), Author (g/c), shape (v/c), name (v/c)

连通

## [1] TRUE
## [1] TRUE

边和点的筛选:

建立边或者点的索引向量:

类似于算数操作,关于点的操作汇总:

data("kite")
V(kite)
# + 10/10 vertices, named, from 6b7ddad:
#  [1] A B C D E F G H I J
V(kite)[1:3,7:10]
# + 7/10 vertices, named, from 6b7ddad:
# [1] A B C G H I J
V(kite)[degree(kite) < 2]
# + 1/10 vertex, named, from 6b7ddad:
# [1] J
V(kite)[.nei("D")]
# + 6/10 vertices, named, from 6b7ddad:
# [1] A B C E F G
V(kite)[.innei("D")]
# + 6/10 vertices, named, from 6b7ddad:
# [1] A B C E F G
V(kite)[.outnei("D")]
# + 6/10 vertices, named, from 6b7ddad:
# [1] A B C E F G
V(kite)[.inc("A|D")]
# + 2/10 vertices, named, from 6b7ddad:
# [1] A D
c(V(kite)["A"],V(kite)["D"])
# + 2/10 vertices, named, from 6b7ddad:
# [1] A D
rev(V(kite))
# + 10/10 vertices, named, from 6b7ddad:
#  [1] J I H G F E D C B A
unique(V(kite)["A","A","C","C"])
# + 2/10 vertices, named, from 6b7ddad:
# [1] A C

### Set operation

union(V(kite)[1:5],v(kite)[6:10])
# + 2/10 vertices, named, from 6b7ddad:
# [1] A C
intersection(V(kite)[1:7],V(kite)[5:10])
# + 3/10 vertices, named, from 6b7ddad:
# [1] E F G
difference(V(kite),V(kite)[1:5])
# + 5/10 vertices, named, from 6b7ddad:
# [1] F G H I J


E(kite)
# + 18/18 edges from 6b7ddad (vertex names):
#  [1] A--B A--C A--D A--F B--D B--E B--G C--D C--F D--E D--F D--G E--G F--G F--H G--H
# [17] H--I I--J

E(kite,path = c("A","D","C"))
# + 2/18 edges from 6b7ddad (vertex names):
# [1] A--D C--D

E(kite)[ V(kite)[1:2] %--%  V(kite)[3:4] ]
# + 3/18 edges from 6b7ddad (vertex names):
# [1] A--C A--D B--D

E(kite)[1:3,7:10]
# + 7/18 edges from 6b7ddad (vertex names):
# [1] A--B A--C A--D B--G C--D C--F D--E

E(kite)[seq_len(gsize(kite))[seq_len(gsize(kite)) %%2 == 0]]
# + 9/18 edges from 6b7ddad (vertex names):
# [1] A--C A--F B--E C--D D--E D--G F--G G--H I--J

E(kite)[seq_len(gsize(kite)) %%2 == 0]
# + 9/18 edges from 6b7ddad (vertex names):
# [1] A--C A--F B--E C--D D--E D--G F--G G--H I--J

E(kite)[seq_len(gsize(kite)) %%2]

# + 9/18 edges from 6b7ddad (vertex names):
# [1] A--B A--B A--B A--B A--B A--B A--B A--B A--B

E(kite)[.inc("D")]
# + 6/18 edges from 6b7ddad (vertex names):
# [1] A--D B--D C--D D--E D--F D--G

E(macaque)[.from("V1")]
# + 8/463 edges from f7130f3 (vertex names):
# [1] V1->V2  V1->V3  V1->V3A V1->V4  V1->V4t V1->MT  V1->PO  V1->PIP
E(macaque)[.to("V1")]
# + 8/463 edges from f7130f3 (vertex names):
# [1] V2 ->V1 V3 ->V1 V3A->V1 V4 ->V1 V4t->V1 MT ->V1 PO ->V1 PIP->V1

### The remains are same as Vertices operations