File: howto.Rmd

package info (click to toggle)
r-cran-intergraph 2.0-4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: sh: 13; makefile: 2
file content (316 lines) | stat: -rw-r--r-- 8,325 bytes parent folder | download | duplicates (2)
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
---
title: "Short `intergraph` tutorial"
author: "MichaƂ Bojanowski"
output: 
  rmarkdown::html_vignette:
    toc: true
    number_sections: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Short intergraph tutorial}
  %\VignetteEncoding{UTF-8}
---

<!--
vim:spell:spelllang=en_us
-->

```{r, setup, include=FALSE}
library(intergraph)
library(knitr)

set.seed(123)
```

- - -



"Intergraph" is an R package with coercion routines for netowrk data objects. For more information, see

* Homepage on [https://mbojan.github.io/intergraph/](https://mbojan.github.io/intergraph/).
* Package development pages on [https://github.com/mbojan/intergraph](https://github.com/mbojan/intergraph).

This is a short tutorial showing how to use functions in package "intergraph"
using some example network data contained in the package.







# Loading example data

To show the data, first load the packages.

```{r,packages}
library(intergraph)
library(network)
library(igraph)
```

Now, these are the summaries of the "igraph" objects:
```{r, summarize-igraph}
summary(exIgraph)
summary(exIgraph2)
```

These are the summaries of the "network" objects:
```{r,summarize-network}
exNetwork
exNetwork2
```

More information is available in the Appendix.












# Functions `asNetwork` and `asIgraph`

Conversion of network objects between classes "network" and "igraph" can be
performed using functions `asNetwork` and `asIgraph`.


## network => igraph

Converting "network" objects to "igraph" is done by calling function
`asIgraph` on a "network" object:
```{r,network2igraph}
# check class of 'exNetwork'
class(exNetwork)
# convert to 'igraph'
g <- asIgraph(exNetwork)
# check class of the result
class(g)
```

Check if edgelists of the objects are identical
```{r}
el.g <- get.edgelist(g)
el.n <- as.matrix(exNetwork, "edgelist")
identical( as.numeric(el.g), as.numeric(el.n))
```


## igraph => network

Converting "igraph" objects to "network" is done by calling function
`asNetwork` on an "igraph" object:
```{r,igraph2network}
net <- asNetwork(exIgraph)
```
Note the warning because of a "non-standard" network attribute `layout`, which
is a function.  Printing "network" objects does not handle non-standard
attributes very well.  However, all the data and attributes are copied
correctly.

Check if edgelists of the objects are identical
```{r}
el.g2 <- get.edgelist(exIgraph)
el.n2 <- as.matrix(net, "edgelist")
identical( as.numeric(el.g2), as.numeric(el.n2))
```



## Handling attributes

Objects of class "igraph" and "network", apart from storing actual network data
(vertexes and edges), allow for adding attributes of vertexes, edges, and
attributes of the network as a whole (called "network attributes" or "graph
attributes" in the nomenclatures of packages "network" and "igraph"
respectively).

Vertex and edge attributes are used by "igraph" and "network" in a largely
similar fashion. However, network-level attributes are used differently.
Objects of class "network" use network-level attributes to store various
metadata, e.g., network size, whether the network is directed, is bipartite, etc.
In "igraph" this information is stored separately.

The above difference affects the way the attributes are copied when we convert
"network" and "igraph" objects into one another.

Both functions `asNetwork` and `asIgraph` have an additional argument `attrmap`
that is used to specify how vertex, edge, and network attributes are copied.
The `attrmap` argument requires a data frame. Rows of that data frame specify
rules of copying/renaming different attributes.  The data frame should have the
following columns (all of class "character"):

* `type`: one of "network", "vertex" or "edge", whether the rule applies to
  network, vertex or edge attribute.
* `fromslc`: name of the which we are *converting from*
* `fromattr`: name of the attribute in the object we are converting from
* `tocls`: name of the class of the object we are *converting to*
* `toattr`: name of the attribute in the object we are converting to

The default rules are returned by a function `attrmap()`, these are:

```{r attrmap-defaults}
attrmap()
```

For example, the last row specifies a rule that when an object of class
"igraph" is converted to class "network", then a vertex attribute `name` in the
"igraph" object will be copied to a vertex attribute called `vertex.names` in
the resulting object of class "network.

If the column `toattr` contains an `NA`, that means that the corresponding
attribute is not copied.  For example, the first row specifies a rule that when
an object of class "network" is converted to class "igraph", then a network
attribute `directed` in the "network" object is *not* copied to the resulting
object of class "igraph".

Users can customize the rules, or add new ones, by constructing similar
data frames and supplying them through argument `attrmap` to functions
`asIgraph` and `asNetwork`.

As an example let us set the option to always drop the `na` vertex attribute. First, we need to setup the rule by adding an extra row to the data frame returned by `attrmap`:

```{r attrmap-example-rules}
new_rule <- data.frame(type="vertex", fromcls="network", fromattr="na",
                       tocls="igraph", toattr=NA,
                       stringsAsFactors=FALSE)
# combine with the default rules
rules <- rbind( attrmap(), new_rule )
rules
```

Now we can use it with `asIgraph`:

```{r attrmap-example}
(ig1 <- asIgraph(exNetwork))
(ig2 <- asIgraph(exNetwork, amap=rules))

# check if "na" was dropped
"na" %in% igraph::vertex_attr_names(ig1)
"na" %in% igraph::vertex_attr_names(ig2)
```









# Network objects to/from data frames

Function `asDF` can be used to convert network object (of class "igraph" or "network")
to a list of two data frames:

```{r asDF}
l <- asDF(exIgraph)
str(l)
```

The resulting list has two components `edges` and `vertexes`. The `edges` component is essentially
an edge list containing ego and alter ids in the first two columns. The remaining columns store
edge attributes (if any). For our example data it is

```{r show-edgedb}
l$edges
```

The `vertexes` component contains data on vertexes with vertex id (the same
that is used in the first two column of `edges`) is stored in the first two
columns. The remaining columns store vertex attributes (if any). For our
example data it is:

```{r show-vertexdb}
l$vertexes
```

Functions `asNetwork` and `asIgraph` can also be used to create network objects
from data frames such as those above. The first argument should be an edge list data frame.
Optional argument `vertices` expectes data frames with vertex data (just like `l$vertexes`).
Additionally we need to specify whether the edges should be interpreted as directed or not
through the argument `directed`.

For example, to create an object of class "network" from the dataframes created above from
object `exIgraph` we can:

```{r fromdf}
z <- asNetwork(l$edges, directed=TRUE, l$vertexes)
z
```

This is actually what basically happens when we call `asNetwork(exIgraph)`



- - -


# Appendix

## Example networks

Package intergraph contains four example networks:

* Objects `exNetwork` and `exIgraph` contain the same *directed* network as objects of class "network" and "igraph" respectively.
* Objects `exNetwork2` and `exIgraph2` contain the same *undirected* network as objects of class "network" and "igraph" respectively.

All four datasets contain:

* A vertex attribute `label` with vertex labels. These are letters from `a` to `o`.
* An edge attribute `label` with edge labels. These are pasted letters of the adjecent nodes.

We will use them in the examples below.


Networks are shown below using the following code:

```{r showdata-code,eval=FALSE}
layout(matrix(1:4, 2, 2, byrow=TRUE))
op <- par(mar=c(1,1,2,1))
# compute layout
coords <- layout.fruchterman.reingold(exIgraph)
plot(exIgraph, main="exIgraph", layout=coords)
plot(exIgraph2, main="exIgraph2", layout=coords)
plot(exNetwork, main="exNetwork", displaylabels=TRUE, coord=coords)
plot(exNetwork2, main="exNetwork2", displaylabels=TRUE, coord=coords)
par(op)
```

```{r showdata-pic, ref.label="showdata-code",echo=FALSE,fig.height=10,fig.width=10}
```




## Session information


```{r, session_info}
sessionInfo()
```