Social Network Analysis in R

Alison Link
19 November 2015

Problem

New workplace, large college, no idea of who knows whom.

Solution…some kind of network-y “Rolodex”?

Social network analysis "crash course"

  • Nodes & edges
  • Directed vs. undirected
  • Measures of centrality
    • closeness
    • degree centrality
    • betweenness

Thanks, Coursera!

The packages

library(igraph)
library(networkD3) # the library formerly known as "d3Network"

Our raw data

Two columns of people: 1) a team member 2) a “Rolodex” contact

Additional columns with attributes:

  • some relating to edges (strength of relationship)
  • some relating to nodes (department affiliation, notes)

Load the data

Read in the .CSV…

raw_csv_data <- read.csv("TEL Network Map - Spring 2015.csv", header=TRUE)

names(raw_csv_data) <- c('TELmember', 'CLAcontact', 'dept', 'context', 'notes', 'strength')

#head(raw_csv_data)

Approach #1: iGraph

library(igraph)

Prep the data

We're in luck! The iGraph package's “graph_from_data_frame” takes as an argument…

“A data frame containing a symbolic edge list in the first two columns. Additional columns are considered as edge attributes. Since version 0.7 this argument is coerced to a data frame with as.data.frame.”

So, let's convert to an iGraph-friendly data format…

network_graph_data <- graph_from_data_frame(raw_csv_data, directed=FALSE)

Note: This is the simplest way to coerce our data into an iGraph-friendly format. It will treat all columns as edge attributes–something we may or may not want. For a more complicated version that supports vertex attributes, as well, look at the graph.data.frame command.

Inspect the data

Look at the vertices…

V(network_graph_data)

Look at the edges…

E(network_graph_data)

Plot the data

plot(network_graph_data)