-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotly_network.Rmd
293 lines (228 loc) · 6.13 KB
/
plotly_network.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
---
title: "Network graph using R Plotly"
date: "`r Sys.Date()`"
output:
workflowr::wflow_html:
toc: true
---
```{r setup, include=FALSE}
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE)
```
Plotly can be used to plot a [Network Graph in R](https://plotly.com/r/network-graphs/). The documentation does not work, so here's my implementation.
## Dependencies
```{r install_packages, message=FALSE, warning=FALSE}
packages <- c('plotly', 'igraph', 'igraphdata', 'sna')
sapply(packages, function(x){
y <- require(x, character.only = TRUE)
if(y == FALSE){
install.packages(x, quiet = TRUE)
library(x, character.only = TRUE)
}
as.character(packageVersion(x))
})
```
## Example
Load [Zachary's karate club](https://en.wikipedia.org/wiki/Zachary%27s_karate_club). Network description from Wikipedia:
>A social network of a karate club was studied by Wayne W. Zachary for a period of three years from 1970 to 1972. The network captures 34 members of a karate club, documenting links between pairs of members who interacted outside the club. During the study a conflict arose between the administrator "John A" and instructor "Mr. Hi" (pseudonyms), which led to the split of the club into two. Half of the members formed a new club around Mr. Hi; members from the other part found a new instructor or gave up karate. Based on collected data Zachary correctly assigned all but one member of the club to the groups they actually joined after the split.
```{r karate}
data(karate, package="igraphdata")
karate
```
Upgrade graph.
```{r upgrade_karate}
G <- upgrade_graph(karate)
str(G)
```
This is how the graph is supposed to look, plotted using `igraph`.
```{r plot_igraph}
set.seed(1984)
L <- layout_nicely(G)
plot.igraph(G, layout = L)
```
The colour can be obtained from `vertex_attr`, which can query vertex attributes of a graph.
```{r vertex_attr}
vertex_attr(G, 'color')
```
The function `layout_nicely`:
>This function tries to choose an appropriate graph layout algorithm for the graph, automatically, based on a simple algorithm.
```{r check_out_layout}
head(L)
```
We can get the name of the vertices using `V`.
```{r vertices}
vs <- V(G)
vs
```
The edge list shows the connections.
```{r edge_list}
el <- as.data.frame(get.edgelist(G))
head(el)
```
Create the network with just the nodes using our layout `L`.
```{r nodes}
network <- plot_ly(
x = ~L[, 1],
y = ~L[, 2],
mode = "markers",
text = vs$label,
hoverinfo = "text",
type = "scatter",
size = I(42),
color = as.character(vertex_attr(G, 'color')),
colors = c('orange', 'skyblue'),
showlegend=FALSE
)
network
```
The graph above lacks the edges, which we will manually create. For example, these two nodes need to connect.
```{r eg1}
el[1, ]
```
The layout contains the coordinates of the nodes but is not named.
```{r tail_layout}
tail(L)
```
We can get the names using `names`.
```{r vertice_names}
my_layout <- L
row.names(my_layout) <- names(V(G))
tail(my_layout)
```
To get the (x, y) coordinates, we just subset `my_layout`.
```{r get_xy}
get_xy <- function(x){
my_layout[x, ]
}
get_xy('Mr Hi')
```
Get the (x, y) coordinates of every node in the edge list.
```{r get_xy_el}
xy1 <- t(
apply(el, 1, function(x){
get_xy(x[1])
})
)
xy2 <- t(
apply(el, 1, function(x){
get_xy(x[2])
})
)
head(xy1)
```
Build the list of lines that will be used to connect the nodes.
```{r build_lines}
my_line <- list(
type = "line",
line = list(color = "#030303", width = 0.3),
xref = "x",
yref = "y"
)
my_lines <- lapply(seq_along(xy1[, 1]), function(x){
c(
my_line,
x0 = xy1[x, 1],
y0 = xy1[x, 2],
x1 = xy2[x, 1],
y1 = xy2[x, 2]
)
})
```
Plot the graph using `layout` to modify the default layout.
```{r layout_graph}
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
layout(
network,
title = "Zachary's karate club Network",
shapes = my_lines,
xaxis = axis,
yaxis = axis
)
```
## Plot igraph with Plotly
Incorporate all the steps above into one function but without colours.
```{r plotly_igraph}
plotly_igraph <- function(G, L = layout_nicely(G), my_title = NULL){
vs <- V(G)
el <- as.data.frame(get.edgelist(G))
network <- plot_ly(
x = ~L[, 1],
y = ~L[, 2],
mode = "markers",
text = names(vs),
hoverinfo = "text",
type = "scatter",
size = I(42),
showlegend=FALSE
)
row.names(L) <- names(vs)
get_xy <- function(x){
L[x, ]
}
xy1 <- t(
apply(el, 1, function(x){
get_xy(x[1])
})
)
xy2 <- t(
apply(el, 1, function(x){
get_xy(x[2])
})
)
my_line <- list(
type = "line",
line = list(color = "#030303", width = 0.3),
xref = "x",
yref = "y"
)
my_lines <- lapply(seq_along(xy1[, 1]), function(x){
c(
my_line,
x0 = xy1[x, 1],
y0 = xy1[x, 2],
x1 = xy2[x, 1],
y1 = xy2[x, 2]
)
})
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
layout(
network,
title = my_title,
shapes = my_lines,
xaxis = axis,
yaxis = axis
)
}
set.seed(1984)
plotly_igraph(G, my_title = "Zachary's karate club Network")
```
## Graphviz dot file
The [read.dot](https://search.r-project.org/CRAN/refmans/sna/html/read.dot.html) function can read network information from a Graphviz's DOT format file, returning an adjacency matrix.
```{r read_dot}
adj_mat <- read.dot("data/rnaseq_variant_calling.dot")
dim(adj_mat)
```
The adjacency matrix is directional. `CALL_markduplicates` -> `CALL_split_n_cigar_reads` and not the other way around.
```{r check_adj_mat}
adj_mat[1:2, 1:2]
```
[Convert matrix to adjacency list](adj_list.html).
```{r adj_list}
as_tibble(adj_mat, rownames = "parent") |>
pivot_longer(-parent, names_to='child') |>
filter(value == 1) |>
select(-value) |>
mutate(parent = str_trim(parent, 'both'), child = str_trim(child, 'both')) -> adj_list
head(adj_list)
```
Create network.
```{r plot_net}
net <- graph.data.frame(adj_list, directed = TRUE)
set.seed(1984)
plot(net, layout = layout_nicely)
```
Need to make the lines into arrows.
```{r plotly_igraph_net}
set.seed(1984)
plotly_igraph(net, L = layout_nicely(net))
```