Learn-igraph系列是对Statistical Analysis of Network Data with R一书的学习笔记,介绍如何使用R进行网络数据分析,网络数据的处理主要是基于igraph包,可视化用的是ggnet

0. 基本概念

一些需要知道的基本概念;

  • Network;

  • Graph;

  • Order of a graph;

  • Size of a graph;

  • directed graph;

  • undirected graph;

  • subgraph;

1. 创建igraph class

1.1 无向图

igraph包处理网络图的数据结构为igraph class, 最基础的创建方式如下:

该网络的基本信息可以通过如下方式获得:

1.2 有向图

同样的方法,也可以用来创建有向图;

1.3 从邻接矩阵导入图;

我们选择一个神奇的数据Arecibo_message[https://en.wikipedia.org/wiki/Arecibo_message], 来说明,有时候,信息所对应的矩阵,可能就是一张图片,而不是一个图。

如果直接可视化这个图,我们什么也看不出来,然而,如果我们用将原数据视为栅格数据,那么,我们能看出这个数据的内涵是很丰富的

1.4 从data.frame中创建图

需要两个输入,一个是边的信息,一个是节点的信息

##                name age gender
## Alice         Alice  48      F
## Bob             Bob  33      M
## Cecil         Cecil  45      F
## David         David  34      M
## Esmeralda Esmeralda  21      F
##        from    to same.dept friendship advice
## 1       Bob Alice     FALSE          4      4
## 2     Cecil   Bob     FALSE          5      5
## 3     Cecil Alice      TRUE          5      5
## 4     David Alice     FALSE          2      4
## 5     David   Bob     FALSE          1      2
## 6 Esmeralda Alice      TRUE          1      3

可视化,

1.5 用预定义的函数生成

igraph里有很多带make的函数,是可以生成图的

##  [1] "make_"                     "make_bipartite_graph"     
##  [3] "make_chordal_ring"         "make_clusters"            
##  [5] "make_de_bruijn_graph"      "make_directed_graph"      
##  [7] "make_ego_graph"            "make_empty_graph"         
##  [9] "make_full_bipartite_graph" "make_full_citation_graph" 
## [11] "make_full_graph"           "make_graph"               
## [13] "make_kautz_graph"          "make_lattice"             
## [15] "make_line_graph"           "make_ring"                
## [17] "make_star"                 "make_tree"                
## [19] "make_undirected_graph"

我们展示其中的一些图:

2. 基本操作

诱导子图

## IGRAPH d91ee38 UN-- 5 6 -- 
## + attr: name (v/c)
## + edges from d91ee38 (vertex names):
## [1] 1--2 1--3 2--3 2--4 3--5 4--5

Exclusion:

## IGRAPH d923ec9 UN-- 5 6 -- 
## + attr: name (v/c)
## + edges from d923ec9 (vertex names):
## [1] 1--2 1--3 2--3 2--4 3--5 4--5

Inclusion:

## IGRAPH d928f5d UN-- 7 10 -- 
## + attr: name (v/c)
## + edges from d928f5d (vertex names):
##  [1] 1--2 1--3 2--3 2--4 3--5 4--5 4--6 4--7 5--6 6--7

union:

## IGRAPH d92f82f UN-- 7 10 -- 
## + attr: name (v/c)
## + edges from d92f82f (vertex names):
##  [1] 6--7 5--6 4--7 4--6 4--5 3--5 2--4 2--3 1--3 1--2

3. 查看/添加/修改 属性

首先创建一个示例的图,

我们可以通过$运算符来查看,添加,修改属性

可视化如下:

## [1] "Female" "Male"   "Female" "Male"   "Female"

4. 更多关于图的概念和术语

4.1 概念

下述概念不搬运书里的定义;忘记就查书。后面的章节会再用到这些概念,进行图的可视化与统计分析。

  • multi-graph

  • simple-graph: 可以用is.simple()判定,可以用simplify()multi-graph转换为simple-graph.

  • neighbors

  • degree: The degree of a vertex v defined as the number of edges incident on v;

  • in-degree

  • out-degree

  • walk

  • trails

  • circuit & cylce;

  • reachable

  • graph connected

  • component of a graph

  • strong connected

  • weak connected

  • distance/geodesic distance

  • diameter

4.2 一些特殊的图

与第一节有重叠

  • complet graph

  • clique

  • regular graph

  • tree

  • forest

  • root

  • ancestor

  • descendant

  • parents, children

  • k-star

  • dirrected acyclic graph(DAG)

  • bipartite graph

## [1] "Actor" "Actor" "Actor" "Movie" "Movie"
##   x   y   name  type category      xend      yend
## 1 0 0.0 actor1 FALSE    Actor 0.9514929 0.2378732
## 2 0 0.5 actor2 FALSE    Actor 0.9514929 0.2621268
## 3 0 0.5 actor2 FALSE    Actor 0.9514929 0.7378732
## 4 0 1.0 actor3 FALSE    Actor 0.9514929 0.7621268
## 5 0 0.0 actor1 FALSE    Actor 0.0000000 0.0000000
## 6 0 0.5 actor2 FALSE    Actor 0.0000000 0.5000000

igraph自带的例子:

附录:R配色

基本颜色:

调色版

渐变色

library(RColorBrewer)
library(colorRamps)
library(viridis)
### manu
rdylbu <- colorRampPalette(rev(brewer.pal(n = 11, name ="RdYlBu")))
rdbu <- colorRampPalette(rev(brewer.pal(n = 11, name ="RdBu")))
navy <- colorRampPalette(c("navy", "white", "firebrick3"))
jet.colors <-
  colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                     "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
cold <- colorRampPalette(c('#f7fcf0','#41b6c4','#253494','#081d58','#081d58'))
warm <- colorRampPalette(c('#ffffb2','#fecc5c','#e31a1c','#800026','#800026'))
warmcold <- colorRampPalette(c(rev(cold(21)), warm(20)))


### add manu with package function

N <- 100 # ramp length
funnames <- rev(c("manu::rdylbu","manu::rdbu","manu::navy","manu::jet.colors","manu::warmcold",
              "viridis::viridis",
              "grDevices::rainbow", "grDevices::heat.colors",
              "grDevices::terrain.colors", "grDevices::topo.colors",
              "grDevices::cm.colors", 
              "colorRamps::blue2red",
              "colorRamps::blue2green", "colorRamps::green2red",
              "colorRamps::blue2yellow", "colorRamps::cyan2yellow",
              "colorRamps::magenta2green", "colorRamps::matlab.like",
              "colorRamps::matlab.like2", "colorRamps::primary.colors",
              "colorRamps::ygobb"))
spl <- strsplit(funnames, "::")
pkgs <- sapply(spl, "[", 1)
funs <- sapply(spl, "[", 2)
kolmat <- sapply(funs, do.call, list(N))
mat <- matrix( seq(1, length(kolmat)), nrow(kolmat), ncol(kolmat))


image(seq(1, nrow(mat)), seq(1, ncol(mat)), mat, col=kolmat,
      axes=FALSE, ann=FALSE)
text( nrow(mat)/2, seq(1, ncol(mat)), funnames)
mtext("Color Ramps function", 3, cex=2)