generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
07.Rmd
389 lines (312 loc) · 10.1 KB
/
07.Rmd
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
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
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
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
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
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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Network Visualization and Aesthetics
**Learning objectives:**
- How to change the aesthetics of network visualizations.
## The Basics
```{r}
suppressPackageStartupMessages(library(igraph))
suppressPackageStartupMessages(library(tidygraph))
suppressPackageStartupMessages(library(ggraph))
suppressPackageStartupMessages(library(tidyverse))
```
Visualization and statistics are the primary tools at our disposable for conveying convincing stories about the structure and dynamics of the networks we study.
Load data and create an igraph object from edgelist
```{r}
#read data
data_url <- "https://raw.githubusercontent.com/mahoffman/social_network_analysis/master/Data/money_edgelist.csv"
money_edgelist <- read_csv(data_url)
#igraph
moneyNetwork_ig <- money_edgelist %>%
as.matrix() %>%
graph_from_edgelist(directed=TRUE)
#tidygraph
moneyNetwork_tg <- as_tbl_graph(moneyNetwork_ig)
```
```{r}
money_edgelist
moneyNetwork_ig
moneyNetwork_tg
```
Basic igraph plot
```{r}
set.seed(1992)
plot(moneyNetwork_ig)
```
```{r}
set.seed(1992)
plot(moneyNetwork_ig)
```
Save "nice" layout
```{r}
nt_layout <- layout_nicely(moneyNetwork_ig)
```
Set the layout
```{r}
plot(moneyNetwork_ig, layout = nt_layout)
```
Basic ggraph plot
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point()
```
### Nodes
We can control the size of the nodes
```{r}
plot(moneyNetwork_ig, layout = nt_layout, vertex.size = 50)
plot(moneyNetwork_ig, layout = nt_layout, vertex.size = 10)
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(size = 5)
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(size = 10)
```
We can also control the color of the nodes and their frames
```{r}
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA #remove the frames of the nodes
)
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(color = "tomato", size = 5)
```
Adjust label size with vertex.label.cex. We can adjust the color with vertex.label.color
```{r}
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label.cex = .7, #adjust label size
vertex.label.color = "black" #adjust label color
)
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
Alternatively, if we want to get rid of the labels, we can just set vertex.label to NA.
```{r}
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label = NA#remove label
)
```
## Edges
Adjust edges size and curvature to give them a nicer aesthetic
```{r}
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label.cex = .7,
vertex.label = NA,
edge.curved = 0.1, #curvature of the arrow(higher value = more curvature; 0=no curvature)
edge.arrow.size = 0.3, #size of arrow head
edge.width = .7 #width of the edge
)
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_arc(strength = 0.1) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_bend(strength = 0.2) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_diagonal(strength = 0.7) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link(aes(start_cap = label_rect(node1.name),
end_cap = label_rect(node2.name)),
color = "grey60",
strength = 0.1,
width = 1,
arrow = arrow(angle = 15,
length=unit(0.5,"cm"),
ends="first",
type = "closed")) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
```{r}
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
for more tricks check this notebook <https://rpubs.com/jmmcclu3/865821>
## Layouts
![source: https://www.data-imaginist.com/2017/ggraph-introduction-layouts/](https://d33wubrfki0l68.cloudfront.net/756daa22f3690a4073ce41607ef98bf5536035a4/1f06e/post/2017-02-06-ggraph-introduction-layouts_files/figure-html/unnamed-chunk-12.gif)
-- A network's layout determines the nodes' positions in the plot. -- All layouts try to minimize the number of edges that cross.
The author states that he usually uses either the Kamada Kawai (`igraph::layout.kamada.kawai()`; `ggraph:::layout_tbl_graph_igraph()`) algorithm or the Fruchterman Reingold algorithm.
**Kamada Kawai**
```{r}
# first we run the layout function on our graph
kamadaLayout <- layout.kamada.kawai(moneyNetwork_ig)
# and then we change the default layout setting to equal the layout we generated above
plot(moneyNetwork_ig,
layout = kamadaLayout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label.cex = .7,
vertex.label = NA,
edge.curved = .1,
edge.arrow.size = .3,
edge.width = .7)
```
```{r}
ggraph(moneyNetwork_tg, layout = kamadaLayout) +
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
The layout can be directly stated in ggraph call
```{r}
ggraph(moneyNetwork_tg, layout = "kk") + #kamada.kawai
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
```
Check for layouts available ggraph from igraph ?ggraph:::layout_tbl_graph_igraph()
## Adding attributes to a network object
Load node attributes (metadata)
```{r}
data_url2 <- "https://raw.githubusercontent.com/mahoffman/social_network_analysis/master/Data/attribute_df.csv"
attributes <- read_csv(data_url2)
head(attributes)
```
add attributes to the igraph object
```{r}
moneyNetwork_ig <- graph_from_data_frame(money_edgelist,
directed = T,
vertices = attributes)
moneyNetwork_ig
V(moneyNetwork_ig)$Gender
```
and the ggraph object
```{r}
moneyNetwork_tg <- tbl_graph(edges = money_edgelist,
directed = T,
nodes = attributes)
moneyNetwork_tg
```
Let's set a layout for the network
```{r}
kk_layout <- layout.kamada.kawai(moneyNetwork_ig)
```
## Plotting based on attributes
```{r}
V(moneyNetwork_ig)$color <- ifelse(V(moneyNetwork_ig)$Gender == "Male", "dodgerblue3","seagreen")
```
```{r}
plot(moneyNetwork_ig,
layout = kk_layout,
vertex.size = 10,
vertex.frame.color = "black",
vertex.label.cex = .7,
vertex.label = NA,
edge.curved = .1,
edge.arrow.size = .3)
```
```{r}
ggraph(moneyNetwork_tg, layout = kk_layout) + #kamada.kawai
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(aes(color = Gender), size = 5)+
scale_color_manual(values = c(Female = "seagreen", Male = "dodgerblue3"))
```
Now let's try role. First, I reset color. There are four roles (Father, Mother, Son, Daughter), so we need a few more ifelse statements to code for all of them.
```{r}
V(moneyNetwork_ig)$color <- NA
role_ig <- V(moneyNetwork_ig)$Role
V(moneyNetwork_ig)$color <- case_when(
role_ig == "Father" ~ "burlywood1",
role_ig == "Mother" ~ "seagreen",
role_ig == "Son" ~ "grey70",
TRUE ~ "tomato")
plot(moneyNetwork_ig,
layout = kk_layout,
vertex.size = 10,
vertex.label.cex = .7,
vertex.label = NA,
edge.curved = .1,
vertex.frame.color = "black",
edge.arrow.size = .3,
edge.width = .7,
edge.color = "grey30")
```
```{r}
ggraph(moneyNetwork_tg, layout = kk_layout) + #kamada.kawai
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(aes(color = Role), size = 5)+
scale_color_manual(values = c(Father = "burlywood1",
Mother = "seagreen",
Son = "grey70",
Daughter = "tomato"))
```
Last but not least, let's adjust the sizes of the nodes so that they reflect differences in age. We can set node size to be 1/5th of the node's age with the code below. Simple, but effective. It looks like the oldest nodes give to the most people.
```{r}
V(moneyNetwork_ig)$size = V(moneyNetwork_ig)$Age/5
plot(moneyNetwork_ig,
layout = kk_layout,
vertex.label.cex = .7,
vertex.label = NA,
edge.curved = .1,
vertex.frame.color = "black",
edge.arrow.size = .3,
edge.width = .7,
edge.color = "grey30")
```
```{r}
ggraph(moneyNetwork_tg, layout = kk_layout) + #kamada.kawai
geom_edge_fan(width = 1,
aes(alpha = after_stat(index)),
show.legend = FALSE) +
geom_node_point(aes(color = Role,
size = Age/5))+
scale_color_manual(values = c(Father = "burlywood1",
Mother = "seagreen",
Son = "grey70",
Daughter = "tomato"))
```
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/H8ihh6AyZ4I")`
<details>
<summary>
Meeting chat log
</summary>
```
00:06:22 Abdalrhman Mostafa: Hi Mohamed, ezayak!
00:09:24 mohamed.shoeb: https://bookdown.org/markhoff/social_network_analysis/
```
</details>