First, we load the necessary packages for network analysis and data manipulation.
# Loading Libraries
library(tidyverse)
library(igraph)
library(knitr)
library(kableExtra)
Here I defined the nodes amd the connections between them. R
# Network Nodes
vertices <- tibble(
id = 1:7,
name = c("Me", "Roxy", "Sona", "Adam", "Simba", "Telma", "Louise"),
group = c("Self", "Wife", "Mom", "Family", "Family", "Work", "Work"),
hometown = c("Los Angeles", "Los Angeles", "Lebanon", "Seattle", "Salt Lake", "Beijing", "New York")
)
# Create the Relationships (Edges)
edges <- tibble(
from = c(1, 1, 1, 1, 1, 1, 2, 4, 6),
to = c(2, 3, 4, 5, 6, 7, 3, 5, 7)
)
# Display the nodes
vertices %>%
kable(caption = "My Social Network Nodes") %>%
kable_styling()
| id | name | group | hometown |
|---|---|---|---|
| 1 | Me | Self | Los Angeles |
| 2 | Roxy | Wife | Los Angeles |
| 3 | Sona | Mom | Lebanon |
| 4 | Adam | Family | Seattle |
| 5 | Simba | Family | Salt Lake |
| 6 | Telma | Work | Beijing |
| 7 | Louise | Work | New York |
I converted our data frames into an igraph object,
assign colors based on the social groups, and plot the network.
# Graph
my_network <- graph_from_data_frame(d = edges, vertices = vertices, directed = FALSE)
# Colors
group_colors <- c("Self" = "#E76F51", "Family" = "#2A9D8F", "Friend" = "#E9C46A", "Work" = "#264653")
V(my_network)$color <- group_colors[V(my_network)$group]
# Myself Larger
V(my_network)$size <- ifelse(V(my_network)$name == "Me", 35, 25)
# Plot the Final Network
plot(
my_network,
layout = layout_with_fr(my_network), # Fruchterman-Reingold layout
vertex.label.cex = 0.9,
main = "My Social Network Graph"
)
## Warning: vertex attribute color contains NAs. Replacing with default value 1
Finally, I ran some basic analytics to find tightly knit groups, identify influencers using PageRank, and locate connections with shared backgrounds.
# A. Counting friendship triangles (tightly-knit groups)
triangle_counts <- tibble(
name = V(my_network)$name,
triangle_count = count_triangles(my_network)
) %>% arrange(desc(triangle_count))
print(triangle_counts)
## # A tibble: 7 × 2
## name triangle_count
## <chr> <dbl>
## 1 Me 3
## 2 Roxy 1
## 3 Sona 1
## 4 Adam 1
## 5 Simba 1
## 6 Telma 1
## 7 Louise 1
# B. Identifying Influencers (PageRank)
pagerank_scores <- page_rank(my_network)$vector
rankings <- tibble(
name = V(my_network)$name,
pagerank = round(pagerank_scores, 4)
) %>% arrange(desc(pagerank))
print(rankings)
## # A tibble: 7 × 2
## name pagerank
## <chr> <dbl>
## 1 Me 0.313
## 2 Roxy 0.114
## 3 Sona 0.114
## 4 Adam 0.114
## 5 Simba 0.114
## 6 Telma 0.114
## 7 Louise 0.114
# C. Finding users with the same hometown
same_hometown <- edges %>%
left_join(vertices, by = c("from" = "id")) %>%
rename(a_hometown = hometown) %>%
left_join(vertices, by = c("to" = "id")) %>%
rename(b_hometown = hometown) %>%
filter(a_hometown == b_hometown) %>%
select(from, to, shared_hometown = a_hometown)
print(same_hometown)
## # A tibble: 1 × 3
## from to shared_hometown
## <dbl> <dbl> <chr>
## 1 1 2 Los Angeles
Mapping out this social network in R has been a great exercise for visualizing how abstract relationships translate into data.
Building this network highlighted a distinct hub-and-cluster structure, where my central node bridges two completely separate family groups. Analyzing the specific node connections reveals that my Older Sister acts as a critical structural bridge, representing a “friend-of-a-friend” link that could easily connect my mother and younger sister directly to my brother-in-law.By running PageRank and calculating network triangles on my own circles, I can see exactly how influence and clusters form within a group. Taking these concepts from this exercise into the real-world will be a powerful tool for me. In my day-to-day work with advancement services and CRM operations, applying these same relationship-mapping could be a helpful new way to approach donor outreach for my team.