-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork_plotting_with_R.Rmd
226 lines (185 loc) · 5.11 KB
/
Network_plotting_with_R.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
---
title: "Network plotting with Rstudio"
subtitle: "⚔<br/>presentation powered by xaringan"
author: "Gaspar Jekely"
institute: "RStudio course - LSI <br> 25-26th April 2022"
output:
xaringan::moon_reader:
css: addons/xaringan-themer-GJ.css
nature:
ratio: "16:9"
beforeInit: ["addons/macros.js"]
highlightStyle: github
highlightLines: true
countIncrementalSlides: true
slideNumberFormat: "%current%"
navigation:
scroll: true
---
```{r xaringanExtra, echo=FALSE}
xaringanExtra::use_xaringan_extra(c("tile_view", "animate_css", "tachyons"))
#to load xaringanExtra in a single call
```
```{r xaringan-editable, echo=FALSE}
xaringanExtra::use_editable(expires = 1)
#makes slides editable to make a component of your slides editable, use the .can-edit[] class.
```
```{r xaringan-scribble, echo=FALSE}
xaringanExtra::use_scribble()
#to draw on slides
```
```{r xaringan-panelset, echo=FALSE}
xaringanExtra::use_panelset()
#to add panels to slides with
#.panel[.panel-name[NAME]
#...content...
#]
```
---
# Network plotting - Create a matrix
``` {r, eval=TRUE}
library(tidyverse)
m <- matrix(c(1,2,3,0,0,3,11,4,0,1,0,3,4,5,6,7), nrow=4)
m
```
--
``` {r, eval=TRUE}
rownames(m) <- c('IN1','PRC','IN2','SN1')
m
```
--
``` {r, eval=TRUE}
colnames(m) <- c('IN1','PRC','IN2','SN1')
m
```
---
# Convert to tibble
``` {r, eval=TRUE}
tb <- as.data.frame(m) %>%
rownames_to_column(var = "presyn_cell") %>%
pivot_longer(-presyn_cell, names_to = "postsyn_cell", values_to = "synapses")
tb
```
---
# Plot with ggplot
``` {r, eval=TRUE, fig.height=6}
tb %>%
ggplot(aes(x=presyn_cell, y=postsyn_cell, size = synapses, color = synapses)) +
geom_point() +
theme_minimal()
```
---
#Mutate
```{r, eval=TRUE}
tb <- tb %>%
group_by(presyn_cell) %>%
mutate(synapse_fraction = synapses / sum(synapses, na.rm = TRUE))
tb
```
---
# Plot with ggplot
``` {r, eval=TRUE, fig.height=6}
tb %>%
ggplot(aes(x=presyn_cell, y=postsyn_cell, size = sqrt(synapses), color = synapse_fraction)) +
geom_point() +
theme_minimal()
```
---
```{r eval=TRUE}
library(igraph)
library(visNetwork)
library(tidygraph)
```
---
## convert to VisNetwork via tbl graph (works with all igraph methods)
```{r eval=TRUE}
g.visn <- tb %>%
as_tbl_graph %>%
toVisNetworkData()
g.visn
## we can assign color to nodes
g.visn$nodes$color <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442")
```
---
```{r eval=TRUE, fig.height=3}
visNet <- visNetwork(g.visn$nodes,g.visn$edges)%>%
visIgraphLayout(layout = "layout_nicely", physics = TRUE, randomSeed = 42) %>%
visPhysics(solver = "forceAtlas2Based",
forceAtlas2Based = list(gravitationalConstant = -400))%>%
visEdges(smooth = list(type = 'curvedCW', roundness=0.1),
scaling=list(min=2, max=8),
color = list(inherit=TRUE, opacity=0.7),
arrows = list(to = list(enabled = TRUE,
scaleFactor = 1.2, type = 'arrow'))) %>%
visNodes(borderWidth=0.3,
color = list(background=g.visn$nodes$color, border='black'),
opacity=0.9,
shape='dot',
font=list(color='black', size=24),
scaling = list(label=list(enabled=TRUE, min=16, max=28))) %>%
visOptions(highlightNearest = TRUE) %>%
visInteraction(dragNodes = TRUE, dragView = TRUE,
zoomView = TRUE, hover=TRUE,
multiselect=TRUE) %>%
addFontAwesome()
visNet
```
---
# Read adjacency matrix
```{r eval=TRUE, fig.height=5}
adj <- read_csv("assets/data/adjacency_matrix.csv")
```
---
# Tidy it up
```{r eval=TRUE}
adj_tb <- adj %>%
rename(presyn_cell=Neurons) %>%
pivot_longer(-presyn_cell, names_to = "postsyn_cell", values_to = "synapses")
adj_tb
```
---
# Plot
```{r eval=TRUE, fig.height=5}
adj_tb %>%
ggplot(aes(x=presyn_cell, y=postsyn_cell, size = sqrt(synapses), color = synapses)) +
geom_point() +
theme_minimal()
```
---
# Convert to network with tidygraph package (gives an iGraph object), then tp VisNetwork
```{r eval=TRUE}
library(tidygraph)
g.visn <- adj_tb %>%
as_tbl_graph %>%
toVisNetworkData()
```
## define edge weights as 'values'
```{r eval=TRUE}
g.visn$edges$value <- g.visn$edges$synapses
```
---
# visNetwork plot
```{r eval=TRUE}
visNet <- visNetwork(g.visn$nodes,g.visn$edges)%>%
visIgraphLayout(layout = "layout_nicely", physics = TRUE, randomSeed = 42) %>%
visPhysics(solver = "forceAtlas2Based",
forceAtlas2Based = list(gravitationalConstant = -400))%>%
visEdges(smooth = list(type = 'curvedCW', roundness=0.1),
scaling=list(min=2, max=8),
color = list(inherit=TRUE, opacity=0.7),
arrows = list(to = list(enabled = TRUE,
scaleFactor = 1.2, type = 'arrow'))) %>%
visNodes(borderWidth=0.3,
color = list(background=g.visn$nodes$color, border='black'),
opacity=0.9,
shape='dot',
font=list(color='black', size=24),
scaling = list(label=list(enabled=TRUE, min=16, max=28))) %>%
addFontAwesome()
```
---
# show graph
```{r eval=TRUE}
visNet
```
---