To perform basic network analysis on your graph and visualize the distribution of node degrees (i.e., the number of connections per node) and identify the most connected nodes, follow these steps using the igraph package in R:
Step 1: Summary Statistics
Start by summarizing the graph's structure:
library(igraph)
# Number of nodes and edges
cat("Number of nodes:", vcount(graph), "\n")
cat("Number of edges:", ecount(graph), "\n")
# Network density (ratio of edges to possible edges)
cat("Network density:", edge_density(graph), "\n")
Step 2: Degree Distribution
The degree of a node is the number of edges connected to it. For a directed graph, you can analyze total degree (mode = "all"), in-degree (mode = "in"), or out-degree (mode = "out").
Plot the Degree Distribution
# Calculate total degree for each node
deg <- degree(graph, mode = "all") # Use "in" or "out" for directed analysis
# Plot histogram
hist(deg,
breaks = 30,
col = "steelblue",
main = "Degree Distribution",
xlab = "Degree",
ylab = "Frequency",
border = FALSE)
grid() # Add grid lines for readability
Interpretation
- A right-skewed distribution (few high-degree nodes, many low-degree nodes) is common in real-world networks.
- For scale-free networks, use
log = "xy" in plot() to check for power-law behavior.
Step 3: Identify Most Connected Nodes
Find the nodes with the highest degrees:
# Sort nodes by degree (descending)
top_n <- 10 # Top 10 most connected nodes
top_degrees <- sort(deg, decreasing = TRUE)[1:top_n]
# Print results
print(top_degrees)
# Optional: Bar plot of top nodes
barplot(top_degrees,
col = "skyblue",
main = paste("Top", top_n, "Most Connected Nodes"),
ylab = "Degree",
las = 2) # Rotate x-axis labels
Step 4: Advanced: In-Degree vs. Out-Degree (Optional)
For directed graphs, analyze in-degree and out-degree separately:
# In-degree and out-degree
in_deg <- degree(graph, mode = "in")
out_deg <- degree(graph, mode = "out")
# Scatter plot to compare in-degree vs. out-degree
plot(in_deg, out_deg,
xlab = "In-Degree",
ylab = "Out-Degree",
main = "In-Degree vs. Out-Degree",
pch = 16,
col = rgb(0, 0, 1, 0.5))
Key Insights
- High-degree nodes (hubs) are critical for network connectivity and influence.
- Degree distribution shape reveals network properties:
- Poisson-like: Random network.
- Heavy-tailed: Scale-free network (e.g., social networks).
- Density indicates sparsity/density of connections.
Example Output
- Histogram: Shows how degrees are distributed across nodes.
- Barplot: Highlights the most connected nodes (e.g.,
"NodeA" with degree 50, "NodeB" with degree 45, etc.).
Let me know if you need further analysis (e.g., centrality measures, community detection)!