igraph

R Tip of the Day

Nima Dahir

What is igraph?

igraph is an R package used to create, visualize, and analyze networks.

It allows you to study relationships between observations instead of looking at each data point individually!

Use Cases:

  • Discovering patterns in networks
  • Identifying important connections in data

Understanding Network Structure

  • Nodes (vertices): Represent individual objects
  • Edges: Represent relationships or connections

Networks can be:

  • Undirected: Connections go both ways
  • Directed: Connections have a specific direction

Example Network

library(igraph)

example <- make_graph(
  c("A", "B", "A", "C", "B", "C"),
  directed = FALSE
)

plot(
  example,
  vertex.color = "pink",
  vertex.frame.color = "lightblue",
  vertex.label.color = "navyblue",
  edge.color = "lightblue",
  vertex.size = 35
)

Example Network

Building Networks from Data

  • edges stores the relationship data.
  • graph_from_data_frame() converts the data into a graph.
  • vcount() counts the nodes.
  • ecount() counts the connections.

Data Example

edges <- data.frame(
  from = c("Nima", "Nima", "Bridget", "Bridget",
           "Liz", "Liz", "Lily", "Lily",
           "Emma", "Jackson"),
  to = c("Bridget", "Liz", "Liz", "Lily",
         "Lily", "Emma", "Emma", "Jackson",
         "Jameson", "Jameson")
)

network <- graph_from_data_frame(
  edges,
  directed = FALSE
)

vcount(network)
[1] 7
ecount(network)
[1] 10

Using igraph for Network Metrics

Degree centrality measures the number of direct connections each node has.

# calculate connections for every node
connections <- degree(network)

# rank nodes by their connections
sort(connections, decreasing = TRUE)
    Liz    Lily Bridget    Emma    Nima Jackson Jameson 
      4       4       3       3       2       2       2 

Using igraph for Network Metrics

barplot(
  connections,
  main = "Connections per Node",
  xlab = "Individuals",
  ylab = "Number of Connections",
  col = "lightblue",
  border = "pink"
)

Analyzing Relationships Between Nodes

Betweenness centrality measures how frequently a node appears on shortest paths between other nodes.

# calculate betweenness centrality
bridge_scores <- betweenness(network)

# compare nodes by their scores
sort(bridge_scores, decreasing = TRUE)
     Lily       Liz      Emma   Bridget   Jackson   Jameson      Nima 
4.6666667 3.8333333 3.1666667 1.0000000 0.8333333 0.5000000 0.0000000 

Finding the Shortest Path

# find the shortest path from Nima to Lily
path <- shortest_paths(
  network,
  from = "Nima",
  to = "Lily"
)

# display the path
path$vpath
[[1]]
+ 3/7 vertices, named, from eea475c:
[1] Nima    Bridget Lily   

Visualizing Network Structure

layout_with_fr() is a layout algorithm in igraph that uses a force-directed approach to position connected nodes near one another.

network_layout <- layout_with_fr(network)

plot(
  network,
  layout = network_layout,
  vertex.color = "pink",
  vertex.frame.color = "lightblue",
  vertex.size = 55,
  vertex.label.color = "navyblue",
  edge.color = "lightblue",
  edge.width = 4,
  main = "Network Structure"
)

Network Structure

Real-World Applications

  • Healthcare

  • Transportation

  • Social Media

igraph provides a way to move from raw relationship data to meaningful network analysis!

Thanks!