5 min read•august 14, 2024
Network analysis and graph theory are powerful tools for understanding complex systems. They allow us to model relationships between entities as and , revealing hidden patterns and structures in data.
In R, packages like igraph and ggraph make it easy to create, analyze, and visualize networks. We'll learn how to represent graph data, calculate network metrics, detect communities, and create stunning visualizations to bring our networks to life.
graph_from_adjacency_matrix()
function in igraph creates graph objects from adjacency matrices
g <- graph_from_adjacency_matrix(matrix(c(0,1,1,0), nrow=2), mode="undirected")
graph_from_edgelist()
function in igraph creates graph objects from edge lists
g <- graph_from_edgelist(matrix(c(1,2,2,3), ncol=2), directed=FALSE)
V()
function accesses and manipulates node attributes of a graph object
V(g)$name <- c("Alice", "Bob", "Carol")
assigns names to nodesE()
function accesses and manipulates edge attributes of a graph object
E(g)$weight <- c(0.5, 1.2)
assigns weights to edges%--%
operator creates edges between nodes in a graph object
g <- g + edge("Alice", "David")
adds a new edge to the graph+
operator adds nodes or edges to an existing graph
g <- g + vertices(c("Eve", "Frank"))
adds new nodes to the graphdegree()
function in igraphdegree(g)
returns the degree of each node in the graphbetweenness()
function in igraphbetweenness(g)
returns the betweenness centrality of each nodecloseness()
function in igraphcloseness(g)
returns the closeness centrality of each nodeeigen_centrality()
function in igrapheigen_centrality(g)$vector
returns the eigenvector centrality of each nodecluster_walktrap()
function in igraph implements a community detection algorithm based on random walks
cluster_walktrap(g)
returns a community structure objectcluster_louvain()
function in igraph implements a community detection algorithm based on optimization
cluster_louvain(g)
returns a community structure objectmembership()
function extracts the community assignments from a community structure object
membership(cluster_walktrap(g))
returns a vector of community assignments for each nodeplot()
function in igraph creates basic visualizations of graph objects, with customizable node and edge attributes
plot(g, vertex.color="red", edge.arrow.size=0.5)
plots the graph with red nodes and smaller edge arrowsplot()
function or by modifying the graph object beforehand
V(g)$color <- "blue"; plot(g)
sets the node color to blue before plottingplot(g, layout=layout_in_circle(g))
plots the graph with nodes arranged in a circular layoutggraph()
function creates a ggraph object from a graph object
ggraph(g, layout="fr")
creates a ggraph object with the Fruchterman-Reingold layoutgeom_node_*()
and geom_edge_*()
functions specify the visual properties of nodes and edges
ggraph(g) + geom_node_point(size=5) + geom_edge_link(color="gray")
creates a visualization with large nodes and gray edgesggraph()
specifies the algorithm for arranging nodes
ggraph(g, layout="kk")
uses the Kamada-Kawai layout algorithmaes()
function
ggraph(g) + geom_node_point(aes(size=degree(g)))
maps node size to degree centralityggraph(g) + geom_node_point() + facet_wrap(~community)
creates a separate plot for each community in the graph