1. Required packages
library(igraph)
library(Matrix)
library(sjPlot)
library(report)
2. Matrix of friendship ties among the 5 individuals (adjacency matrix)
adj_matrix <- matrix(c(0,1,0,1,0,
                       1,0,1,1,0,
                       0,1,0,1,1,
                       1,1,1,0,1,
                       0,0,1,1,0),
                     nrow = 5, ncol = 5, byrow = TRUE)

adj_matrix_df <- as.data.frame(adj_matrix)

# Assign row and column names
rownames(adj_matrix_df) <- paste("Person", 1:5)
colnames(adj_matrix_df) <- paste("Person", 1:5)

tab_df(adj_matrix_df) 
Person.1 Person.2 Person.3 Person.4 Person.5
0 1 0 1 0
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
0 0 1 1 0
3. Creating a graph object using the adjacency matrix
ngraph <- graph.adjacency(adj_matrix, mode = "undirected", weighted = NULL)
4. Some visual attributes for nodes and edges
node_colors <- c("lightblue", "lightgreen", "lightcoral", "lightsalmon", "lightpink")
edge_colors <- "gray"
node_labels <- c("Person 1", "Person 2", "Person 3", "Person 4", "Person 5")
5. Visualize the network using the plot() function
plot(
    ngraph,
     layout = layout_with_fr(ngraph),  # Force-directed layout
     vertex.color = node_colors,
     vertex.label = node_labels,
     vertex.size = 90,             # Adjust node size
     vertex.label.cex = 0.7,      # Adjust font size for labels
     edge.color = edge_colors,
     edge.width = 1,              # Adjust edge width
     edge.arrow.size = 0.4,       # Adjust arrow size for directed edges
     main = "Friendship Network",  # Title of the plot
     vertex.frame.color = "white", # Add white border around nodes
     vertex.label.color = "black"  # Color of node labels
)

6. Network Statistics
degree <- degree(ngraph)
clustering <- transitivity(ngraph)
diameter <- diameter(ngraph)

cat("Degree: ", degree, "\n")
## Degree:  2 3 3 4 2
cat("Clustering Coefficient: ", clustering, "\n")
## Clustering Coefficient:  0.6428571
cat("Diameter: ", diameter, "\n")
## Diameter:  2
7. A data frame with network statistics
network_stats <- data.frame(Statistic = c("Avg. Degree", "Clustering Coefficient", "Diameter"),
  Value = c(mean(degree), mean(clustering), mean(diameter)))

tab_df(network_stats)
Statistic Value
Avg. Degree 2.80
Clustering Coefficient 0.64
Diameter 2.00

References

report_packages(include_R = TRUE)
##   - Matrix (version 1.6.1; Bates D et al., 2023)
##   - igraph (version 1.5.1; Csardi G, Nepusz T, 2006)
##   - sjPlot (version 2.8.15; Lüdecke D, 2023)
##   - report (version 0.5.7; Makowski D et al., 2023)
##   - R (version 4.2.1; R Core Team, 2022)