forked from kolaczyk/sand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter3.R
166 lines (147 loc) · 4.46 KB
/
chapter3.R
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
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
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
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
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
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
164
165
# SAND with R, chapter3.tex
# CHUNK 1
library(sand)
g.l <- graph.lattice(c(5, 5, 5))
# CHUNK 2
data(aidsblog)
summary(aidsblog)
# ---
## IGRAPH D--- 146 187 --
# ---
# CHUNK 3
igraph.options(vertex.size=3, vertex.label=NA,
edge.arrow.size=0.5)
par(mfrow=c(1, 2))
plot(g.l, layout=layout.circle)
title("5x5x5 Lattice")
plot(aidsblog, layout=layout.circle)
title("Blog Network")
# CHUNK 4
plot(g.l,layout=layout.fruchterman.reingold)
title("5x5x5 Lattice")
plot(aidsblog,layout=layout.fruchterman.reingold)
title("Blog Network")
# CHUNK 5
plot(g.l, layout=layout.kamada.kawai)
title("5x5x5 Lattice")
plot(aidsblog, layout=layout.kamada.kawai)
title("Blog Network")
# CHUNK 6
g.tree <- graph.formula(1-+2,1-+3,1-+4,2-+5,2-+6,2-+7,
3-+8,3-+9,4-+10)
par(mfrow=c(1, 3))
igraph.options(vertex.size=30, edge.arrow.size=0.5,
vertex.label=NULL)
plot(g.tree, layout=layout.circle)
plot(g.tree, layout=layout.reingold.tilford(g.tree,
circular=T))
plot(g.tree, layout=layout.reingold.tilford)
# CHUNK 7
plot(g.bip, layout=-layout.bipartite(g.bip)[,2:1],
vertex.size=30, vertex.shape=ifelse(V(g.bip)$type,
"rectangle", "circle"),
vertex.color=ifelse(V(g.bip)$type, "red", "cyan"))
# CHUNK 8
library(igraphdata)
data(karate)
# Reproducible layout
set.seed(42)
l <- layout.kamada.kawai(karate)
# Plot undecorated first.
par(mfrow=c(1,1))
plot(karate, layout=l, vertex.label=NA)
# Now decorate, starting with labels.
V(karate)$label <- sub("Actor ", "", V(karate)$name)
# Two leaders get shapes different from club members.
V(karate)$shape <- "circle"
V(karate)[c("Mr Hi", "John A")]$shape <- "rectangle"
# Differentiate two factions by color.
V(karate)[Faction == 1]$color <- "red"
V(karate)[Faction == 2]$color <- "dodgerblue"
# Vertex area proportional to vertex strength
# (i.e., total weight of incident edges).
V(karate)$size <- 4*sqrt(graph.strength(karate))
V(karate)$size2 <- V(karate)$size * .5
# Weight edges by number of common activities
E(karate)$width <- E(karate)$weight
# Color edges by within/between faction.
F1 <- V(karate)[Faction==1]
F2 <- V(karate)[Faction==2]
E(karate)[ F1 %--% F1 ]$color <- "pink"
E(karate)[ F2 %--% F2 ]$color <- "lightblue"
E(karate)[ F1 %--% F2 ]$color <- "yellow"
# Offset vertex labels for smaller points (default=0).
V(karate)$label.dist <-
ifelse(V(karate)$size >= 10, 0, 0.75)
# Plot decorated graph, using same layout.
plot(karate, layout=l)
# CHUNK 9
library(sand)
data(lazega)
# Office location indicated by color.
colbar <- c("red", "dodgerblue", "goldenrod")
v.colors <- colbar[V(lazega)$Office]
# Type of practice indicated by vertex shape.
v.shapes <- c("circle", "square")[V(lazega)$Practice]
# Vertex size proportional to years with firm.
v.size <- 3.5*sqrt(V(lazega)$Years)
set.seed(42)
l <- layout.fruchterman.reingold(lazega)
plot(lazega, layout=l, vertex.color=v.colors,
vertex.shape=v.shapes, vertex.size=v.size)
# CHUNK 10
library(sand)
summary(fblog)
# ---
## IGRAPH UN-- 192 1431 --
## attr: name (v/c), PolParty (v/c)
# ---
# CHUNK 11
party.names <- sort(unique(V(fblog)$PolParty))
party.names
# ---
## [1] " Cap21" " Commentateurs Analystes"
## [3] " Les Verts" " liberaux"
## [5] " Parti Radical de Gauche" " PCF - LCR"
## [7] " PS" " UDF"
## [9] " UMP"
# ---
# CHUNK 12
set.seed(42)
l = layout.kamada.kawai(fblog)
party.nums.f <- as.factor(V(fblog)$PolParty)
party.nums <- as.numeric(party.nums.f)
plot(fblog, layout=l, vertex.label=NA,
vertex.color=party.nums, vertex.size=3)
# CHUNK 13
set.seed(42)
l <- layout.drl(fblog)
plot(fblog, layout=l, vertex.size=5, vertex.label=NA,
vertex.color=party.nums)
# CHUNK 14
fblog.c <- contract.vertices(fblog, party.nums)
E(fblog.c)$weight <- 1
fblog.c <- simplify(fblog.c)
# CHUNK 15
party.size <- as.vector(table(V(fblog)$PolParty))
plot(fblog.c, vertex.size=5*sqrt(party.size),
vertex.label=party.names,
vertex.color=V(fblog.c),
edge.width=sqrt(E(fblog.c)$weight),
vertex.label.dist=1.5, edge.arrow.size=0)
# CHUNK 16
k.nbhds <- graph.neighborhood(karate, order=1)
# CHUNK 17
sapply(k.nbhds, vcount)
# ---
## [1] 17 10 11 7 4 5 5 5 6 3 4 2 3 6 3 3 3
## [18] 3 3 4 3 3 3 6 4 4 3 5 4 5 5 7 13 18
# ---
# CHUNK 18
k.1 <- k.nbhds[[1]]
k.34 <- k.nbhds[[34]]
par(mfrow=c(1,2))
plot(k.1, vertex.label=NA,
vertex.color=c("red", rep("lightblue", 16)))
plot(k.34, vertex.label=NA,
vertex.color=c(rep("lightblue", 17), "red"))