Network Analysis with igraph

Network analysis is essential for understanding relationships in data. We’ll use the igraph package.

Step 1: Install and Load igraph

rCopy codeinstall.packages("igraph")
library(igraph)

Step 2: Create a Sample Graph

rCopy code# Create a sample graph
edges <- data.frame(
  from = c("A", "A", "B", "C", "C", "D", "E"),
  to = c("B", "C", "D", "D", "E", "E", "A")
)

# Create a graph object
graph <- graph_from_data_frame(edges, directed = TRUE)

# Plot the graph
plot(graph, vertex.color = "lightblue", vertex.size = 30, edge.arrow.size = 0.5,
     main = "Sample Directed Graph")

Step 3: Analyze the Graph

rCopy code# Calculate degree centrality
degree_centrality <- degree(graph)
print(degree_centrality)

# Identify the largest connected component
largest_component <- components(graph)$membership
print(largest_component)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *