Let’s start by installing the packages that we’re going to use to analyze our data, knowing that we want to conduct social network analysis and that we want to create sophisticated sociograms. We’ll need the following packages:
{tidyverse} which offers multiple R packages
{tidygraph} for working with relational data
{ggraph} to support network visualizations
{igraph} to optimize network visualizations
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(tidygraph)
##
## Attaching package: 'tidygraph'
##
## The following object is masked from 'package:stats':
##
## filter
library(ggraph)
library(igraph)
##
## Attaching package: 'igraph'
##
## The following object is masked from 'package:tidygraph':
##
## groups
##
## The following objects are masked from 'package:lubridate':
##
## %--%, union
##
## The following objects are masked from 'package:dplyr':
##
## as_data_frame, groups, union
##
## The following objects are masked from 'package:purrr':
##
## compose, simplify
##
## The following object is masked from 'package:tidyr':
##
## crossing
##
## The following object is masked from 'package:tibble':
##
## as_data_frame
##
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
##
## The following object is masked from 'package:base':
##
## union
I decided to take on a challenge this week, because I want to be able to work with my own datasets as we approach the end of the semester. I wanted to see if I could generate a sociogram based on the data I can export from our makerspace staff scheduling software (a program called Deputy).
Although we have 60+ staff that work over the entire year, I wanted to keep the dataset manageable for this first attempt since I wasn’t sure what I was doing. I chose to limit my data analysis to only student staff who had worked more than 20 shifts during the Spring 2023 semester. Under these constraints, I ended up with 21 “actors” (BeAM student staff).
Each of these staff members worked some unique combination of 20-30 shifts out of a total possible 119 shifts during the Spring 2023 semester. Many of them overlapped with each other, and I was able to create an adjacency matrix that displayed their interactions with each other (i.e. the number of times they worked with a particular person, etc.)
The DeputyStaffMatrix.csv document shows the number
of times each staff member worked with another specific staff
member.
The DeputyStaffNodesList contains the ID number and
reference name (color-based pseudonym) of each staff member.
The main questions I had going into this exercise were mostly logistics-based:
I will need to tidy the data as we did in the Unit 1 case study, where I will read in the DeputyStaffMatrix document and include the col_names argument to make sure R doesn’t include column names in my data. I’ll also read in the DeputyStaffNodesList that contains the reference names (color-based pseudonyms) and ID numbers for the staff.
BeAM_staff_edges <- read_csv("data/DeputyStaffMatrix.csv",
col_names = FALSE)
## Rows: 21 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (21): X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
BeAM_staff_nodes <- read_csv("data/DeputyStaffNodesList.csv")
## Rows: 21 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Ref_Name
## dbl (1): ID
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
BeAM_staff_edges
## # A tibble: 21 × 21
## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0 8 3 10 5 7 7 10 10 8 9 3 15
## 2 8 0 8 7 2 13 9 2 10 17 6 5 2
## 3 3 8 0 5 6 5 7 3 7 8 6 5 2
## 4 10 7 5 0 4 8 8 12 6 17 14 9 10
## 5 5 2 6 4 0 3 2 4 6 5 7 5 6
## 6 7 13 5 8 3 0 5 6 9 18 8 9 6
## 7 7 9 7 8 2 5 0 5 10 11 8 5 4
## 8 10 2 3 12 4 6 5 0 7 9 10 5 10
## 9 10 10 7 6 6 9 10 7 0 6 8 11 7
## 10 8 17 8 17 5 18 11 9 6 0 12 9 6
## # … with 11 more rows, and 8 more variables: X14 <dbl>, X15 <dbl>, X16 <dbl>,
## # X17 <dbl>, X18 <dbl>, X19 <dbl>, X20 <dbl>, X21 <dbl>
So far, so good. Time to add labels to the matrix so that we can better understand what we’re working with. Since there are 21 actors in our network, that’s how many labels I need. After that, I need to restructure this “tibble” into a more formal matrix object so that I can analyze it.
Finally, I’ll convert the matrix to an undirected network graph and save it as a new object called BeAM_staff_network. (NOTE: This network is undirected because reciprocity does not apply here - staff are either working together on shift or not.)
rownames(BeAM_staff_edges) <- 1:21
## Warning: Setting row names on a tibble is deprecated.
colnames(BeAM_staff_edges) <- 1:21
BeAM_staff_matrix <- as.matrix(BeAM_staff_edges)
BeAM_staff_network <- as_tbl_graph(BeAM_staff_matrix,
directed = FALSE)
## Warning in (is.null(rownames(x)) && is.null(colnames(x))) || colnames(x) == :
## 'length(x) = 21 > 1' in coercion to 'logical(1)'
BeAM_staff_network
## # A tbl_graph: 21 nodes and 210 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 21 × 1 (active)
## name
## <chr>
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## # … with 15 more rows
## #
## # Edge Data: 210 × 3
## from to weight
## <int> <int> <dbl>
## 1 1 2 8
## 2 1 3 3
## 3 1 4 10
## # … with 207 more rows
Time to extract the edges from the network object and combine it with information from the BeAM_staff_nodes document. For this first exercise, I have only included ID number and reference names (color-based pseudonyms.) However, in the future, I’d like to include additional characteristics (e.g. years worked at BeAM, tool area of expertise, etc.) that could be interesting to map to this network.
BeAM_staff_edges <- BeAM_staff_network |>
activate(edges) |>
as_tibble()
BeAM_staff_network <- tbl_graph(edges = BeAM_staff_edges,
nodes = BeAM_staff_nodes,
directed = FALSE)
BeAM_staff_network
## # A tbl_graph: 21 nodes and 210 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 21 × 2 (active)
## ID Ref_Name
## <dbl> <chr>
## 1 1 Almond
## 2 2 Amber
## 3 3 Aqua
## 4 4 Cerulean
## 5 5 Emerald
## 6 6 Jonquil
## # … with 15 more rows
## #
## # Edge Data: 210 × 3
## from to weight
## <int> <int> <dbl>
## 1 1 2 8
## 2 1 3 3
## 3 1 4 10
## # … with 207 more rows
I’m going to use similar methods from our Unit 1 Case Study, and see how they initially display the data before deciding what changes I want to make to my visualizations. Let’s start with a simple plot just to check that things are roughly on the right track.
plot(BeAM_staff_network)
Okay, we’ve got some nodes and edges, that’s about what I expected to see. However, I want to specifically demonstrate the weight of the edges. That might mean assigning some type of visual marker (e.g. line thickness) to show that two students have worked more shifts together. Let’s try a simple version of that to see if what it might look like, before we dive into trying to optimize this sociogram.
ggraph(BeAM_staff_network, layout = "stress") +
geom_edge_link(alpha = .2,
aes(width = weight)) +
geom_node_point(size = 3) +
geom_node_text(aes(label = Ref_Name),
repel=TRUE)
## Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
Hmm, it’s pretty saturated and hard to read. When I glanced at my edgelist, I noticed that all of the actors had worked with everyone else at least once. That means that every node in this network is connected. However, I want to drill down into the weight of those connections - for example, Keppel might have worked with Magenta 5 times but Cerulean 20 times.
It might be useful to create a cut-off value for displaying edges. We could also use measures of centrality in a more meaningful way if we knew that not all of the nodes were connected. I’m going to play around with an edges filter until I find a value that allows me to keep all the nodes, but drop any edges below that weight. I ended up with a filter value of 7 shifts:
BeAM_staff_network2 <- BeAM_staff_network |>
activate("edges") |>
filter(weight >= 7)
ggraph(BeAM_staff_network2, layout = "stress") +
geom_edge_link(alpha = .25,
aes(width = weight)) +
geom_node_point(size = 2) +
geom_node_text(aes(label = Ref_Name),
repel=TRUE) +
theme_graph()
That’s a little better! I’m going to proceed with using BeAM_staff_network2 from now on, since it’s filtered to only the edges that have a weight of 8 or more (meaning that those two staff members have worked at least 7 shifts together.)
Now that I have a filtered dataset, I think it would be interesting to see some measures of centrality. I’m going to save the node-level centrality scores to our network, which will be another variable that I can display in the final sociogram.
centr_degree(BeAM_staff_network2, mode = "all")
## $res
## [1] 16 13 4 14 5 14 12 9 16 16 17 8 10 15 16 16 9 13 13 12 8
##
## $centralization
## [1] 0.2404762
##
## $theoretical_max
## [1] 420
BeAM_staff_network2 <- BeAM_staff_network2 |>
activate(nodes) |>
mutate(centrality = centrality_degree(mode = "all"))
BeAM_staff_network2
## # A tbl_graph: 21 nodes and 128 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 21 × 3 (active)
## ID Ref_Name centrality
## <dbl> <chr> <dbl>
## 1 1 Almond 16
## 2 2 Amber 13
## 3 3 Aqua 4
## 4 4 Cerulean 14
## 5 5 Emerald 5
## 6 6 Jonquil 14
## # … with 15 more rows
## #
## # Edge Data: 128 × 3
## from to weight
## <int> <int> <dbl>
## 1 1 2 8
## 2 1 4 10
## 3 1 6 7
## # … with 125 more rows
Our node-level centrality scores reveal a lot - it looks we have some actors that have worked at least 7 shifts with most of the other staff members, while others have only frequently worked with a few (e.g. Actor 3 has a value of 4.) The overall centrality score for this filtered network seems somewhat low, at about .24.
Just for fun, let’s also run the centrality measure for our original unfiltered network:
centr_degree(BeAM_staff_network, mode = "all")
## $res
## [1] 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
##
## $centralization
## [1] 0
##
## $theoretical_max
## [1] 420
Yep, I had a feeling it would be pretty boring when all of the nodes are connected to each other.
Let’s also use some of the functions described in the Unit 3 Case Study to see if there are any cliques or communities within our filtered network.
clique_num(BeAM_staff_network2)
## [1] 8
It looks like there are 8 “cliques” within this network where the actors are all connected to each other.
Alright, I’m going to attempt to make a more polished version of the earlier sociogram drafts, and include some visual representation of the centrality score for each node.
ggraph(BeAM_staff_network2, layout = "fr") +
geom_edge_link(alpha = .25,
aes(width = weight),
color = "#7BAFD4") +
geom_node_point(aes(size = centrality),
color = "#1D3E55") +
geom_node_text(aes(label = Ref_Name),
repel=TRUE) +
theme_graph(background = "white",
foreground = NULL) +
labs(title="BeAM Staff Shift Patterns")