-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkCanvas.Rmd
More file actions
144 lines (111 loc) · 5.92 KB
/
NetworkCanvas.Rmd
File metadata and controls
144 lines (111 loc) · 5.92 KB
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
---
title: "Loading data into R from Network Canvas"
output:
html_document:
df_print: paged
html_notebook: default
---
This Notebook provides basic input and example analysis of data exported from the [*Network Canvas*](http://networkcanvas.com) software. The example will use simulated data from the following network canvas [protocol file](https://github.com/pfsj/loading-data-network-canvas/blob/main/SB22_workshop_protocol.netcanvas) which can be opened in the Network Canvas Interviewer or Architect apps.
### Import and clean the data
The first step is to load in data. We use the *egor* package to load in the data, using examples from an egor [vignette](https://cran.r-project.org/web/packages/egor/vignettes/using_egor.html)
```{r,message = FALSE}
# Load egor
library(egor)
library(sna)
library(ggplot2)
# This assumes the this file is in the same folder as your data
# If the you have data somewhere else, set the path here:
folderPath <- paste0('/Users/pfj185/Documents/github/loading-data-network-canvas/networkCanvasExport')
# Read each type of file into list, combine into single data frame
alterData <- folderPath %>%
list.files(full.names=TRUE,pattern="attributeList_Person.csv") %>%
lapply(read.csv) %>%
bind_rows()
edgelistData <- folderPath %>%
list.files(full.names=TRUE,pattern="edgeList_friends.csv") %>%
lapply(read.csv) %>%
bind_rows()
egoData <- folderPath %>%
list.files(full.names=TRUE,pattern="ego.csv") %>%
lapply(read.csv) %>%
bind_rows()
```
Now we can examine if the data looks like it should from the alter file. As you'll see, the alter data file contains a unique alter identifier (i.e., "networkCanvasUUID") as well as a unique ego identifier (i.e., "networkCanvasEgoUUID") which will both be used to define the egor object.
```{r}
head(alterData)
```
We'll want to do a little data cleaning by recoding family member status and communication frequency, before loading it into an egor object.
```{r}
alterData$family_member2 <- alterData$family_member
alterData$family_member2[is.na(alterData$family_member2) | alterData$family_member2==""] <- "false"
alterData$communication_freq2 <- alterData$communication_freq
alterData$communication_freq2[alterData$communication_freq2==-1] <- "Less than \n monthly"
alterData$communication_freq2[alterData$communication_freq2==1] <- "Monthly"
alterData$communication_freq2[alterData$communication_freq2==2] <- "Weekly"
alterData$communication_freq2[alterData$communication_freq2==3] <- "Daily"
```
After loading and cleaning the data, we still need to make it into an egor object
```{r}
# Load the file into R
egorNetworkCanvas <- egor(alters = alterData,
egos = egoData,
aaties = edgelistData,
ID.vars = list(
ego = "networkCanvasEgoUUID",
alter = "networkCanvasUUID",
source = "networkCanvasSourceUUID",
target = "networkCanvasTargetUUID"))
```
### Data visualization
Lets start with a simple visualization of one ego network. To do this we'll first convert it to a 'network' object and use the gplot function from the sna package. This visualization shows the new node labels and colors each node by the communication frequency with the participant.
```{r}
oneEgoNet <- as_network(egorNetworkCanvas)[[1]]
oneEgoNet%v%"vertex.names" <- oneEgoNet%v%"name"
colorScheme <- c( "#CC6677", "#117733", "#AA4499",
"#6699CC")
# A little recoding to get a color for each frequency
nodeColors <- ifelse(oneEgoNet%v%"communication_freq"=="-1",colorScheme[1],
ifelse(oneEgoNet%v%"communication_freq"=="1",colorScheme[2],
ifelse(oneEgoNet%v%"communication_freq"=="2",colorScheme[3],
colorScheme[4])))
gplot(oneEgoNet,
usearrows = FALSE,
label = oneEgoNet%v%"name",
displaylabels = TRUE,
vertex.col=nodeColors,
edge.col="gray")
```
As you can see, this only shows a single egocentric network. However, the egor package has several functions that facilitate comparison of networks across ego nets. For example, here is a visualization showing each ego net with nodes location being dependent on their communciation with the participant and their status as a family member (i.e., true/false).
```{r}
# Make a visualization displaying both frequency of communication and family member status
plot(egorNetworkCanvas, venn_var = "family_member2",
pie_var = "communication_freq2",vertex_label_var="nodeID",
type = "egogram")
```
## Data analysis
The egor package has numerous functions that help with basic data analysis of ego networks. For example, the *summary* function provides an overview of all ego networks in the egor object while *ego_density* function provides density for each network.
```{r}
summary(egorNetworkCanvas)
ego_density(egorNetworkCanvas)
```
We can also use a traditional package, such as *sna*, to look at these networks by applying functions (i.e., lapply) to each of these networks and aggregating the results. For example, here we first make a simple histogram of alter degrees across all ego networks.
```{r}
networkNetworkCanvas <- as_network(egorNetworkCanvas)
histData <- networkNetworkCanvas %>%
lapply(degree,cmode="indegree") %>%
unlist(recursive = FALSE) %>%
as.data.frame()
histData$degree <- as.numeric(histData$".")
ggplot(histData, aes(x=degree)) +
geom_histogram(color="black", fill="white",bins=5) +
theme_classic()
```
Finally, we often want to examine how an ego attribute may be associated with ego network characteristics. In this example we look at the association between the level of reported enjoyment of conferences and the density of their ego network.
```{r}
ego_density(egorNetworkCanvas) %>%
full_join(egorNetworkCanvas$ego,by=".egoID") %>%
ggplot(aes(x = enjoy_conferences, y = density)) +
geom_point(size=5) +
geom_text(label=egorNetworkCanvas$ego$networkCanvasCaseID, aes(vjust=c(-1.5))) +
ylim(0,0.6) + theme_classic()
```