Code
# Install once if needed:
# install.packages(c("igraph","tidygraph","ggraph","tidyverse"))
library(igraph)
library(tidygraph)
library(ggraph)
library(tidyverse)
library(dplyr)
library(readxl)
library(DT)# Install once if needed:
# install.packages(c("igraph","tidygraph","ggraph","tidyverse"))
library(igraph)
library(tidygraph)
library(ggraph)
library(tidyverse)
library(dplyr)
library(readxl)
library(DT)Provide your own network file or use the built-in example. The code below supports an edge list CSV.
In this tutorial, the built-in example uses character relationships from Game of Thrones, Book 1, to illustrate the concepts of network centrality. Each node represents a character, and each edge represents an interaction or co-appearance within the first book.
In network analysis, attributes enrich the structural data by attaching meaningful information to nodes and edges. Node attributes (e.g., gender, house, department) describe who the actors are, while edge attributes (e.g., frequency, weight, sentiment) describe how they interact.
In R’s igraph, attributes can be added directly or by joining external data frames. For example, node-level metadata like a character’s house or gender can be merged onto the graph to explore patterns of homophily, centrality by group, or inter-house alliances. Similarly, edge attributes such as interaction count or relationship strength (stored as E(g)$weight) allow weighted analyses of connectivity, centrality, or flow.
These attributes make the network more than a set of connections—they turn it into a social system with interpretable structure and context.
g <- graph_from_data_frame(d = edges[,1:2], directed = TRUE)
gIGRAPH d0e29bc DN-- 187 684 --
+ attr: name (v/c)
+ edges from d0e29bc (vertex names):
[1] Addam-Marbrand ->Jaime-Lannister
[2] Addam-Marbrand ->Tywin-Lannister
[3] Aegon-I-Targaryen ->Daenerys-Targaryen
[4] Aegon-I-Targaryen ->Eddard-Stark
[5] Aemon-Targaryen-(Maester-Aemon)->Alliser-Thorne
[6] Aemon-Targaryen-(Maester-Aemon)->Bowen-Marsh
[7] Aemon-Targaryen-(Maester-Aemon)->Chett
[8] Aemon-Targaryen-(Maester-Aemon)->Clydas
+ ... omitted several edges
# assign weights & book
E(g)$weight <- edges$weight
E(g)$book <- edges$book
nodes <- nodes %>%
mutate(surname = str_replace(Label, ".*-", "")) %>%
rename(name = Id)
# assign house and gender
# Ensure matching order
idx <- match(V(g)$name, nodes$name)
# Assign attributes
V(g)$gender <- nodes$Gender[idx]
V(g)$house <- nodes$House[idx]Density is a measure of the proportion of potential connections in a network that are actual connections.
# Calculating Density
network_density <- edge_density(g)
network_density[1] 0.01966534
Average Path Length (APL) is the average number of steps along the shortest paths for all possible pairs of network nodes.
# Calculating Average Path Length
apl <- average.path.length(g)
# network_density
apl[1] 18.2117
Diameter is the length of the longest shortest path in a network. It represents the greatest distance between any two nodes when traveling along the most efficient route. In other words, it captures how far apart the two most distant nodes are within the connected structure, offering insight into the overall reach and cohesion of the network.
# diameter
diameter <- diameter(g)
diameter[1] 130
Beginning with a network g, containing edges for two distinct time frames:
Retain only connections with a weight under four. Generate networks of specific weights.
# Selecting ties with weights of three or lower
g.edge3 <- subgraph.edges(g, which(E(g)$weight < 4))
# Networks for two different time periods
g.edge10 <- subgraph.edges(g, which(E(g)$weight < 11))
g.edge50 <- subgraph.edges(g, which(E(g)$weight < 51))Node centrality reflects a node’s potential influence or power within the network. Centrality is generally divided into several categories:
Activity: Measures how engaged a node is within the network. Control: Assesses a node’s ability to influence network flow. Efficiency: Reflects how easily a node can interact with others in the network.
Total Effects: Considers a node’s centrality as influenced by other nodes’ centralities. Edge-Properties: Analyzes the centrality of relationships between nodes. Each time we compute a centrality measure, we’ll integrate it into the graph as an attribute, consolidating our analysis in one place.
Degree centrality quantifies node activity, considering total, in, and out-degree connections. In this case, the graph is TECHNICALLY UNDIRECTED so calculating in-degree or out-degree would be not appropriate; however, we set this graph to be directed.
Betweenness centrality measures a node’s control over information flow. It is determined by the number of shortest paths that pass through a node. Note that betweenness can behave differently in weighted networks.
Closeness centrality assesses how closely a node is positioned to all other nodes in the network.
Eigenvector centrality considers the overall impact of a node’s position, influenced by the centrality of connected nodes.
degree<-degree(g.edge3, v = V(g.edge3), loops = F, normalized = T, mode = 'total')
indegree<-degree(g.edge3, v = V(g.edge3), loops = F, normalized = T, mode = 'in')
outdegree<-degree(g.edge3, v = V(g.edge3), loops = F, normalized = T, mode = 'out')
bet<-betweenness(g.edge3, v = V(g.edge3), directed = F, weights = NULL, normalized = T)
clo<-centr_clo(g.edge3, mode = c("total"), normalized = TRUE)$res
eig<-centr_eigen(g.edge3, directed = F, scale = TRUE, normalized = TRUE)$vector
graph_ind_stats<-as.data.frame(cbind(degree, indegree, outdegree, bet, clo, eig))
names(graph_ind_stats)<-c("degree","indegree","outdegree", "betweenness","closeness","eigenvector")
graph_ind_stats<-
graph_ind_stats %>%
mutate(across(where(is.numeric), round, 3))
datatable(graph_ind_stats, filter = 'top', options = list(pageLength = 5))Beyond centrality, local position analysis examines a node’s immediate network and its influence on autonomy and power.
Constraint measures the extent of interconnectivity among a node’s contacts, impacting the node’s autonomy. It’s key in understanding structural holes and information dynamics.
g_con<-constraint(g.edge3)
g_con<-cbind(g_con,bet)
g_con <- round(g_con, 3)
datatable(g_con, filter = 'top', options = list(pageLength = 5))Ron Burt’s concept of network constraint is a pivotal idea in social network analysis, detailing how an individual’s connections can either restrict or facilitate their access to diverse information and opportunities. This concept is a measure of the extent to which an individual’s network ties are to others who are interconnected. Burt’s foundational work, “Structural Holes: The Social Structure of Competition” (1992), introduces and elaborates on this concept, providing a comprehensive understanding of the dynamics within social networks.
Redundancy of Information: In networks with high constraint, there’s often a redundancy of information and resources, as members tend to share similar information (Burt, 1992).
Limited Novel Opportunities: Such networks restrict access to new opportunities or diverse viewpoints due to the homogeneity of the network (Burt, 1992).
Quantifying Constraint: Network constraint is quantified by assessing an individual’s direct connections and the degree to which these connections link among themselves (Burt, 1992).
Brokerage, as conceptualized by Burt, is the process of bridging separate social groups, which stands in contrast to network constraint. Individuals positioned as brokers can leverage unique informational advantages and resources. Burt’s work emphasizes the significant role of brokers in innovation and knowledge dissemination.
Diverse Information Access: Brokers have the unique ability to access varied information pools, providing them with distinct insights (Burt, 1992).
Control Over Information Flow: As intermediaries, brokers can exert control over the flow of information between disparate groups (Burt, 2004, “Brokerage and Closure: An Introduction to Social Capital”).
Innovation and Creativity: Brokers often facilitate innovation by synthesizing diverse ideas from different groups (Burt, 1992).
Burt’s seminal work on brokerage is extensively presented in “Structural Holes: The Social Structure of Competition” (1992). This book delineates how social networks consist of ‘structural holes’ (the gaps between non-redundant contacts) and how individuals acting as brokers (filling these holes) can secure competitive advantages in various arenas such as career progression, innovation, and organizational performance.
The concepts of network constraint and brokerage, as developed by Burt, are foundational in understanding social network dynamics. They underscore the importance of diverse, less redundant networks and the strategic benefits of acting as a bridge between different groups. These concepts are not only pivotal in academic discourse but also have practical implications for individual and organizational strategies in navigating complex social and professional networks.