-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTutorial.Rmd
332 lines (237 loc) · 6.16 KB
/
Tutorial.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
---
title: "Tutorial"
author: "Marco Mello & Renata Muylaert"
date: "23/09/2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Summary
1. [Get ready](#ready)
2. [Figure 21.1](#fig1)
3. [Figure 21.2](#fig2)
4. [Figure 21.3](#fig3)
5. [Figure 21.4](#fig4)
## 1. Get ready {#ready}
Load the required packages:
```{r}
library("igraph")
library("ggplot2")
library("reshape2")
```
## 2. Figure 21.1 {#fig1}
Import the data:
```{r}
studies = read.delim("Analysis/studies.txt", row.names=1)
```
Plot the barplot:
```{r, fig.width = 17, fig.height = 5}
par(mfrow = c(1,1), mar = c(6,7,1,1), xpd=TRUE)
counts <- table(studies$Phyllostomidae, studies$Links)
barplot(counts, main="",
xlab="Topic",
ylab = "Number of studies",
col=gray.colors(length(rownames(counts))),
border = F,
beside=TRUE,
las = 0,
mgp = c(4,1,0),
cex.lab=3,
cex.axis=2,
cex.main=3,
cex.sub=3,
cex.names=1.5)
legend(x = 1,
y = 8,
title = "Phyllostomidae as a model?",
cex = 1.5,
bty="n",
fill=gray.colors(length(rownames(counts))),
legend=rownames(counts))
par(mfrow = c(1,1))
```
## 3. Draw Figure 21.2{#fig2}
Use the same data as in Fig.21.1:
```{r}
head(studies, 3)
```
Plot the histogram:
```{r, fig.width = 12, fig.height = 6}
par(mfrow = c(1,1), mar = c(5,7,1,3))
hist(studies$Year,
breaks=5,
col = adjustcolor("grey", alpha.f = .5),
border = F,
xlab = "Year",
ylab = "Number of studies",
main = "",
mgp = c(4,1,0),
cex.lab=3,
cex.axis=2,
cex.main=3,
cex.sub=3)
par(mfrow = c(1,1))
```
## 4. Draw Figure 21.3{#fig3}
Import the data:
```{r}
softmat = as.matrix(read.delim("Analysis/software.txt", row.names=1))
```
Inspect the object:
```{r}
class(softmat)
softmat
```
Create an igraph object:
```{r}
softnet <- graph_from_incidence_matrix(softmat, weighted = NULL)
```
Inspect the object:
```{r}
class(softnet)
softnet
E(softnet)
V(softnet)
```
Name the two sets of vertices: rows will be "software" and columns, "references":
```{r}
V(softnet)$set = ifelse(V(softnet)$type == FALSE, "software", "reference")
V(softnet)$set
```
Set the layout for the graph:
```{r}
lref <- layout.fruchterman.reingold(softnet)
```
Set the edge curvatures:
```{r}
curvesref = curve_multiple(softnet)
```
Set the edge mode and width:
```{r}
E(softnet)$arrow.mode = 0
E(softnet)$width = 1
```
Set the node shapes:
```{r}
V(softnet)$shape = V(softnet)$set
V(softnet)$shape = gsub("reference","circle",V(softnet)$shape)
V(softnet)$shape = gsub("software","square",V(softnet)$shape)
```
Set the node colors:
```{r}
V(softnet)$color = V(softnet)$set
V(softnet)$color = gsub("reference","grey10",V(softnet)$color)
V(softnet)$color = gsub("software","grey50",V(softnet)$color)
```
Draw the graph with node colors by class:
```{r, fig.width = 8, fig.height = 6}
par(mfrow = c(1,1))
plot(softnet,
vertex.color = V(softnet)$color,
vertex.frame.color = V(softnet)$color,
#vertex.shape = vertexshape,
vertex.size = 12,
#vertex.size = V(softnet)$size,
vertex.label = V(softnet)$name,
vertex.label.color = "white",
vertex.label.cex = .5,
edge.color = adjustcolor("grey70", alpha.f = .5),
#edge.color = "#FFFD00",
#edge.curved = curvesref,
edge.curved = 0.2,
edge.width = 7,
#layout = layout_in_circle,
layout = lref)
legend(x = 0.7,y = -0.6, legend = c("References", "Software"),
pch = c(19,15), title="Legend",
text.col = "gray20", title.col = "black", box.lwd = 0,
cex = 2, col=c("grey10", "grey50"))
par(mfrow = c(1,1))
```
## 5. Draw Figure 21.4{#fig4}
Import the data:
```{r}
sarmentomat = as.matrix(read.delim("Analysis/sarmento.txt", row.names=1))
```
Inspect the object:
```{r}
class(sarmentomat)
sarmentomat
```
Create an igraph object:
```{r}
sarmentonet <- graph_from_incidence_matrix(sarmentomat,
directed = F, weighted = TRUE)
```
Inspect the object:
```{r}
class(sarmentonet)
sarmentonet
E(sarmentonet)
V(sarmentonet)
```
Specify which nodes represent which taxonomic groups:
```{r}
V(sarmentonet)$set[1:12] = "Bats"
V(sarmentonet)$set[13:22] = "Birds"
V(sarmentonet)$set[23:89] = "Plants"
```
Set the layout for the graphs:
```{r}
lsar <- layout_nicely(sarmentonet)
```
Set the edge curvatures:
```{r}
curvessar = curve_multiple(sarmentonet)
```
Set the edge mode and width:
```{r}
E(sarmentonet)$arrow.mode = 0
E(sarmentonet)$width = E(sarmentonet)$weight/5+1
```
Calculate the Louvain modularity (resolution = 1.0):
```{r}
sarmentonet.lou = cluster_louvain(sarmentonet)
```
Import the "diamond" vertex shape:
```{r}
source("Analysis/MyDiamond.R")
```
Set the vertex shapes:
```{r}
V(sarmentonet)$shape = V(sarmentonet)$set
V(sarmentonet)$shape = gsub("Bats","diamond",V(sarmentonet)$shape)
V(sarmentonet)$shape = gsub("Birds","square",V(sarmentonet)$shape)
V(sarmentonet)$shape = gsub("Plants","circle",V(sarmentonet)$shape)
```
Set the colors for the nodes and clouds based on the modularity analysis:
```{r}
colrs <- gray.colors(length(sarmentonet.lou), start = 0.3,
end = 0.6, gamma = 1.5, alpha = NULL)
V(sarmentonet)$color <- colrs[sarmentonet.lou$membership]
clouds = gray.colors(length(sarmentonet.lou), start = 0.7,
end = 0.9, gamma = 1.5, alpha = 0.1)
```
Plot the graph with node colors by Louvain modularity (grey tones) + clouds around modules:
```{r, fig.width = 8, fig.height = 6}
par(mfrow=c(1,1),mar=c(1,1,1,5))
plot(sarmentonet.lou,
sarmentonet,
col = V(sarmentonet)$color,
mark.border="lightgrey",
mark.col=clouds,
vertex.size=7.5,
vertex.label=V(sarmentonet)$set,
vertex.label.color="white",
vertex.label.cex=.5,
edge.color = adjustcolor("grey", alpha.f = .5),
edge.curved=0.3,
edge.width = 3,
layout=lsar)
legend(x = 0.9,y = 1.0, legend = c("Bats", "Birds", "Plants"),
pch = c(18,15,19), title="Taxon",
text.col = "gray20", title.col = "black",
box.lwd = 0, cex = 2, col=c("grey", "grey", "grey"))
par(mfrow=c(1,1))
```