-
Notifications
You must be signed in to change notification settings - Fork 0
/
middle_school.qmd
411 lines (325 loc) · 9.58 KB
/
middle_school.qmd
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
---
title: "Middle School Students"
format:
html:
toc: true
toc-location: left
---
This webpage will explore the dataset exploring connections in 7th and 8th grade students at a middle school. The data was taken from ["Estimates of Social Contact in a Middle School Based on Self-Report and Wireless Sensor Data"](https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0153690). This is the "Example 1" on the poster.
## Introduction || Cleaning Data || Explore Data
These are the packages that were used for this analysis:
```{R}
#| echo: true
#| include: true
#| label: packages
#| warning: false
#| message: false
# attaching packages ####
library(igraph)
library(netplot)
library(data.table)
```
First, let's clean it all up, getting rid of the "isolates" (those who only are connected to one person). Here is the script for cleaning that data:
```{R}
#| echo: true
#| include: true
#| label: clean_data
#| warning: false
#| message: false
# loading and cleaning data ####
students <- fread("./misc/data-raw/middle_school/pone.0153690.s001.csv")
interactions <- fread("./misc/data-raw/middle_school/pone.0153690.s003.csv")
students <- students[!is.na(id)]
students <- students[gender %in% c("0", "1")]
interactions <- interactions[!is.na(id) & !is.na(contactId)]
## Which connections are not OK?
ids <- sort(unique(students$id))
## Alright, this narrowed our data from 10781 to 5150
interactions <- interactions[(id %in% ids) & (contactId %in% ids)]
## Creating weights matrix
net <- graph_from_data_frame(
d = interactions[, .(id, contactId)],
directed = FALSE, vertices = as.data.frame(students)
)
## Getting only connected individuals
net_with_no_isolates <- induced_subgraph(net, which(degree(net) > 0))
## Plot with no isolates
nplot(
net_with_no_isolates
)
```
<br>
Awesome. Now that things are cleaned up a bit, let's see what separate sections we are dealing with!
```{R}
#| echo: true
#| include: true
#| label: print
#| warning: false
#| message: false
# let's see what it looks like ####
print(net_with_no_isolates)
```
<br>
Here we go! Other than the new unique aspects, we have grade, gender, and lunch period that we can explore.
<br>
## Load in "color_nodes.R" function
```{R}
#| echo: true
#| include: true
#| label: color_nodes
#| warning: false
#| message: false
## load in 'color_nodes' function ####
source(file = "./misc/color_nodes_function.R")
```
<br>
## Splitting Data
### Split According to Grade
```{R}
#| echo: true
#| include: true
#| label: grade
#| warning: false
#| message: false
## adjust 'grade' to factor
V(net_with_no_isolates)$grade <- as.factor(V(net_with_no_isolates)$grade)
# plotting connections among grades ####
set.seed(77)
a_colors <- color_nodes(net_with_no_isolates,"grade", c("gray40","red3"))
attr(a_colors, "map")
grades <- nplot(
net_with_no_isolates,
vertex.color = color_nodes(net_with_no_isolates, "grade", c("gray40","red3")),
vertex.nsides = ifelse(V(net_with_no_isolates)$grade == 7, 10, 10),
vertex.size.range = c(0.015, 0.015),
edge.color = ~ego(alpha = 1, col = "lightgray") + alter(alpha = 0.25, col = "lightgray"),
vertex.label = NULL,
edge.curvature = pi/6,
edge.line.breaks = 10
)
# add radial gradient fill
grades <- set_vertex_gpar(grades,
element = "core",
fill = lapply(get_vertex_gpar(grades, "frame", "col")$col, \(i) {
radialGradient(c("white", i), cx1=.8, cy1=.8, r1=0)
}))
# add legend to graph
grades_general <- nplot_legend(
grades,
labels = c("7th", "8th"),
pch = c(21,21),
gp = gpar(
fill = c("gray40","red3")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grades_general
grid.text("Split According to Grade", x = .2, y = .87, just = "bottom")
```
<br>
### Split According to Gender
```{R}
#| echo: true
#| include: true
#| label: gender
#| warning: false
#| message: false
# let's get a graph for the gender data
V(net_with_no_isolates)$gender <- as.factor(V(net_with_no_isolates)$gender)
a_colors <- color_nodes(net_with_no_isolates,"gender", c("lightgoldenrod2","forestgreen"))
attr(a_colors, "map")
## plot
set.seed(77)
gender <- nplot(
net_with_no_isolates,
vertex.color = color_nodes(net_with_no_isolates, "gender",c("lightgoldenrod2","forestgreen")),
vertex.nsides = ifelse(V(net_with_no_isolates)$gender == 0, 10, 4),
vertex.size.range = c(0.01, 0.01),
edge.color = ~ego(alpha = 0.33, col = "gray") + alter(alpha = 0.33, col = "gray"),
vertex.label = NULL,
edge.line.breaks = 10
)
# add legend to graph
nplot_legend(
gender,
labels = c("Male", "Female"),
pch = c(21,23),
gp = gpar(
fill = c("lightgoldenrod2","forestgreen")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grid.text("Split According to Gender", x = .2, y = .87, just = "bottom")
```
<br>
### Split According to Lunch Period
```{R}
#| echo: true
#| include: true
#| label: lunch
#| warning: false
#| message: false
#| cache: true
# now let's do the same with lunch period
V(net_with_no_isolates)$lunch <- as.factor(V(net_with_no_isolates)$lunch)
a_colors <- color_nodes(net_with_no_isolates,"lunch", c("purple","palegreen","steelblue"))
attr(a_colors, "map")
## plot
set.seed(77)
lunch <- nplot(
net_with_no_isolates,
vertex.color = color_nodes(net_with_no_isolates, "lunch",c("purple","palegreen","steelblue")),
vertex.nsides =
ifelse(V(net_with_no_isolates)$gender == 0, 4, # First Lunch
ifelse(V(net_with_no_isolates)$gender == 1, 3, # Second Lunch
10)), # Other
vertex.size.range = c(0.01, 0.01),
edge.color = ~ego(alpha = 0.33, col = "gray") + alter(alpha = 0.33, col = "gray"),
vertex.label = NULL,
edge.line.breaks = 10
)
# add legend to graph
nplot_legend(
lunch,
labels = c("First", "Second", "Other"),
pch = c(23,24,21),
gp = gpar(
fill = c("purple","palegreen","steelblue")),
packgrob.args = list(side = "bottom"),
ncol = 3
)
grid.text("Split According to Lunch Period", x = .2, y = .87, just = "bottom")
```
<br>
## Different `netplot` Options
### Changing Lines to Dashes
This graph correlates to the graph titled "Changing lines to dashes, diamonds to circles, and edge lines to straight lines" on the poster.
```{R}
#| echo: true
#| include: true
#| label: dashes
#| warning: false
#| message: false
#| cache: true
set.seed(77)
grades <- nplot(
net_with_no_isolates,
bg.col = "#F5F5F5",
vertex.color = color_nodes(net_with_no_isolates, "grade", c("red","blue")),
vertex.size.range = c(0.02, 0.02),
edge.color = ~ego(alpha = .15, col = "black") + alter(alpha = .15, col = "black"),
vertex.label = NULL,
edge.width.range = c(2,2),
edge.line.lty = 6,
edge.line.breaks = 1
)
# add legend to graph
grades_dashed <- nplot_legend(
grades,
labels = c("7th", "8th"),
pch = c(21,21),
gp = gpar(
fill = c("red","blue")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grades_dashed
```
### Colored Edges and Skipped Vertices
This graph is correlated to the graph titled "Skipping vertices" on the poster.
```{R}
#| echo: true
#| include: true
#| label: skipped
#| warning: false
#| message: false
#| cache: true
set.seed(77)
grades <- nplot(
net_with_no_isolates,
bg.col = "#F5F5F5",
vertex.color = color_nodes(net_with_no_isolates, "grade", c("red","blue")),
vertex.nsides = ifelse(V(net_with_no_isolates)$grade == 7, 10, 4),
vertex.size.range = c(0.0001, 0.0001),
edge.color = ~ego(alpha = 0.33) + alter(alpha = 0.33),
vertex.label = NULL,
edge.width.range = c(2,2),
edge.line.breaks = 10
)
# add legend to graph
grades_edge_colored <- nplot_legend(
grades,
labels = c("7th", "8th"),
pch = c(21,21),
gp = gpar(
fill = c("red","blue")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grades_edge_colored
```
### Changing Background Color
This graph correlates with the graph titled "Background gradient as function of vertex color" on the poster.
```{R}
#| echo: true
#| include: true
#| label: background
#| warning: false
#| message: false
#| cache: true
set.seed(77)
grades <- nplot(
net_with_no_isolates,
bg.col = linearGradient(c("lightpink", "lightskyblue")),
vertex.color = color_nodes(net_with_no_isolates, "grade", c("red","blue")),
vertex.nsides = ifelse(V(net_with_no_isolates)$grade == 7, 10, 4),
vertex.size.range = c(0.01, 0.01),
edge.color = ~ego(alpha = 0.15, col = "black") + alter(alpha = 0.15, col = "black"),
vertex.label = NULL,
edge.line.breaks = 10
)
# add legend to graph
grades_background <- nplot_legend(
grades,
labels = c("7th", "8th"),
pch = c(21,23),
gp = gpar(
fill = c("red","blue")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grades_background
```
### Different Colors
This graph correlates to the graph titled "Changing vertex colors & edge lines to straight lines" on the poster.
```{R}
#| echo: true
#| include: true
#| label: colors
#| warning: false
#| message: false
#| cache: true
set.seed(77)
grades <- nplot(
net_with_no_isolates,
bg.col = "#F5F5F5",
vertex.color = color_nodes(net_with_no_isolates, "grade", c("#FFDB58","#708090")),
vertex.nsides = ifelse(V(net_with_no_isolates)$grade == 7, 10, 4),
vertex.size.range = c(0.02, 0.02),
edge.color = ~ego(alpha = .15, col = "black") + alter(alpha = .15, col = "black"),
vertex.label = NULL,
edge.width.range = c(2,2),
edge.line.breaks = 1
)
# add legend to graph
grades_different_color <- nplot_legend(
grades,
labels = c("7th", "8th"),
pch = c(21,23),
gp = gpar(
fill = c("#FFDB58","#708090")),
packgrob.args = list(side = "bottom"),
ncol = 2
)
grades_different_color
```