-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathN741igraphqgraph.Rmd
337 lines (230 loc) · 9.53 KB
/
N741igraphqgraph.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
---
title: "N741 Demo of igraph and qgraph packages"
author: "Melinda K. Higgins, PhD."
date: "April 16, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
#library(igraph)
#library(qgraph)
```
## `igraph` package exercise
_These notes were adapted from the course, BIOS560R: "Statistical Methods for Network Science Lab"_
Today we will explore the use of the R package `igraph`, also useful for exploring networks. So, please install the `igraph` package and load it using the `library()` statement.
```{r}
library(igraph)
```
Do the following to create an edgelist.
```{r}
datafab <- data.frame(c("Alice","Alice","Bob","Bob",
"Sue","Jim","Jim","Jim",
"Ellen","Frank"),
c("Bob","Sue","Jim","Sue","Ellen",
"Frank","Ellen","Karen",
"Frank","Karen"))
names(datafab) <- c("person1","person2")
```
You can then turn the data.frame into an `igraph` graph by running the `graph.data.frame()` command:
```{r}
g <- graph.data.frame(datafab, directed=FALSE)
g
```
Or you can find out about the graph with the `summary()` command.
```{r}
summary(g)
```
You can list the vertices and edges of the graph with the `V()` and `E()` commands:
```{r}
V(g)
E(g)
```
There should be 7 vertices and 10 edges.
** Remember to check your work **
Another way to create a graph is to import the necessary information from files in which are saved characteristics such as edge attributes, vertex attributes, and the edges between vertices.
For the next exercise, we will work with the "traits.csv" data file, which has 10 rows (10 people), and 3 columns where:
* column 1 - names of the people
* column 2 - ages of the people
* column 3 - gender of the people
First start with an empty graph:
```{r}
g<- graph.empty()
```
Next we add vertices, as well as their attributes. This information is stored in the comma separated value format (CSV) file "traits.csv". There is another .csv file named “relations.csv” which will define the edges. The "relations.csv" file has 34 rows and 5 columns:
* column 1 - person 1
* column 2 - person 2
* column 3 - room
* column 4 - friend
* column 5 - advice
NOTE: Neither file has a header row.
Let’s read both files in:
```{r}
traits <- read.csv("traits.csv", head=FALSE)
rel<-read.csv("relations.csv", head=FALSE)
```
The head parameter is set to FALSE, instructing R not to interpret the first line of the file as a header. Set this to TRUE if you have a header with column names in your file.
Now, let’s add vertices (from the "traits.csv" data file) to the empty graph as follows:
```{r}
g <- add.vertices(g, nv=nrow(traits),
name = as.character(traits[,1]),
age = traits[,2],
gender = as.character(traits[,3]))
```
Check that the graph is created properly:
```{r}
V(g)$name
V(g)$age
V(g)$gender
```
You can open up the .csv files with Excel, and you will notice that the traits are identified with first and last names, while the relations are identified with the first names only. Let’s strip the last names:
```{r}
names <- sapply(strsplit(V(g)$name, " "), "[", 1)
# NOTE: the edges as done above will throw and error
# now the ids need to start at 1
# see stackoverflow note at
# http://stackoverflow.com/questions/20730295/error-reading-dataset-in-r
#
# previous code
# ids <- 1:length(names)-1
#
# fix - don't subtract 1 anymore
ids <- 1:length(names)
names(ids) <- names
```
* List the ids.
```{r}
ids
```
Next create an edge list with vertex ids instead of symbolic names:
```{r}
from <- as.character(rel[,1])
to <- as.character(rel[,2])
edges <- matrix(c(ids[from], ids[to]), nc=2)
```
* List the edges. How many are there?
```{r}
edges
```
Finally we can add the edges to the graph, along with the edge attributes (room, friend and advice):
```{r}
# quick demo from help
#el <- matrix( c("foo", "bar", "bar", "foobar", "foo", "bar2"),
# nc = 2, byrow = TRUE)
#graph_from_edgelist(el)
graph_from_edgelist(edges)
g <- add.edges(g, t(edges),
room = as.character(rel[,3]),
friend = rel[,4],
advice = rel[,5])
```
* List the graph. Summarize it.
```{r}
g
summary(g)
V(g)
E(g)
```
## Importing Graphs
There is no special file format for importing and storing graphs in `igraph` nor in `R`. There are, however, several different methods for importing them. One should be aware that the `igraph` builders recommend using a simple edge list format for larger graphs. In general the most computational efficiency is gained by using edgelists (over adjacency matrices).
### Formats
**Edgelist Format**
The simplest format is the edge list format. In this format, each row of a text file comprises the IDs that denote the two ends of an edge, separated by a blank. It can be read using the `read.graph` function, setting the format to “edgelist” as follows.
* Read in the edges stored in the file `dolphin.txt`, which is a "space-delimited" file with 2 columns of numbers and 159 rows.
```{r}
# this initally had errors
# I had to recreate the text file
read.graph("dolphin.txt", format="edgelist")
# this works
# g2 <- make_ring(10)
# write_graph(g2, "g2.txt", "edgelist")
# rg2 <- read.graph("g2.txt", format="edgelist")
# write_graph(g2, "g4pj.txt", "pajek")
```
Note that the default is that the graph is imported with directed edges. List the edges in the graph. Alternatively you can import the file with undirected edges by adding the option directed = “FALSE” to the read.graph command. Repeat the import of the dolphins data, and list the edges again.
```{r}
read.graph("dolphin.txt", format="edgelist", directed = FALSE)
```
* Can you tell the difference between how the edges are listed for directed versus undirected graphs?
**Pajek Format**
Pajek is a popular network analysis program for Windows. To read in such a dataset, change the format type to "pajek"" in the `read.graph` command.
* Do this for the karate club dataset, stored as "karate.txt". Note that it is not necessary to tell read.graph if the relationships are directional of not, that is part of the pajek file.
```{r}
read.graph("karate.txt", format="pajek")
```
**GML Format**
Another format is GML, standing for (in some circles) graph markup language, and (in other circles) geographic markup language. Again, change the format type to "gml"" in the `read.graph` command.
* Do this for the "football"" dataset
```{r}
read.graph("football.gml", format="gml")
```
## Plotting graphs in `igraph`
There are two ways to plot graphs.
`plot.igraph(name of igraph object here)`
will give you a static plot, while
`tkplot(name of igraph object here)`
will give you more interactive capability, although I have not tested that. Try these for the graph g that you created from reading in the .csv files above.
```{r}
plot.igraph(g)
tkplot(g)
```
**NOTE:** You'll need to run the `tkplot()` from the command line to see the interactivity - this does NOT show in the final HTML file from the RMD.
* What are the differences between the two plots?
_(NB, I had good luck when I issued the tkplot command as follows, changing the edge colors)_
**RUN these lines of code interactively**
```{r}
E(g)$color <- "black"
E(g)[ room=="Y" ]$color <- "red"
tkplot(g, layout=layout.kamada.kawai,
edge.color=E(g)$color)
```
## Graph and Vertex Properties
You can determine the degree sequence of a graph as follows:
`degree(graph name here)`
Likewise the diameter of a graph:
`diameter(graph name here)`
```{r}
degree(g)
diameter(g)
```
One can calculate closeness centrality with the closeness command as follows:
`closeness(graph name here)`
or estimate it with
`closeness.estimate(graph name here)`
This statement needs a `cutoff` estimate which is useful for large graphs. You can also set this to 0 or a negative number to get the exact closeness scores.
```{r}
closeness(g)
# need an estimate for cutoff
# let's set this to 0.06
closeness.estimate(g, cutoff = 0.06)
# set to 0 to get exact closeness scores
closeness.estimate(g, cutoff = 0)
```
For directed graphs, you can use the mode option here to determine the in-, out-, or all closeness, as follows:
```{r}
closeness(g, mode="in")
closeness(g, mode="out")
closeness(g, mode="all")
# can't get this to run
# closeness(graph=g,
# mode=c("in", "out", "all"))
```
Similarly one can calculate betweenness centrality with the betweenness command as follows:
`betweenness(graph name here)`
or estimate it with
`betweenness.estimate(graph name here)`
```{r}
betweenness(g)
# try with cutoff of 10
betweenness.estimate(g, cutoff=10)
# try with cutoff of 0
betweenness.estimate(g, cutoff=0)
```
Another important option for this command is “directed=TRUE” to use when consideration of direction in digraphs is important.
* Calculate the degree sequence, average degree, diameter, betweenness centrality and closeness centrality of the social relationship graph that we created as well as for the dolphins and football networks.
```{r}
# do this exercise IN CLASS
```
* LAB 1
In the folder there is the dataset `adjnoun.gml`, which describes the word concurrencies in David Copperfield. For this graph, provide a 1-2 page description of it. For each one, give its basic characteristics in tabular format: number of vertices, number of edges, list any attributes of vertices and attributes if known (NB, direction is an attribute), give the graph and vertex properties listed above (including average degree). Include a plot.