| name | r-network |
| description | R network analysis packages. Use for graph analysis, social network analysis, network visualization, and community detection. |
R Network Analysis Skill
Sub-skills
Network and graph analysis in R.
Core Packages
| Package | Description |
|---|
| igraph ★ | Comprehensive network analysis |
| tidygraph ★ | Tidy API for graphs |
| network | Basic relational data tools |
| sna | Social network analysis |
Network Modeling
| Package | Description |
|---|
| ergm | Exponential random graph models |
| latentnet | Latent position/cluster models |
| manynet | Many network types |
Dynamic Networks
| Package | Description |
|---|
| networkDynamic | Dynamic/temporal networks |
| ndtv | Animated network visualization |
| netdiffuseR | Network diffusion analysis |
Visualization
| Package | Description |
|---|
| ggraph ★ | Grammar of graphics for graphs |
| visNetwork ★ | Interactive visualization (vis.js) |
| networkD3 | D3 network graphs |
| autograph | Automagic network plotting |
Metrics & Analysis
| Package | Description |
|---|
| tnet | Weighted/two-mode networks |
| rgexf | Export to GEXF (Gephi) |
Quick Examples
library(igraph)
g <- graph_from_data_frame(edges, directed = TRUE, vertices = nodes)
g <- graph_from_adjacency_matrix(adj_matrix)
vcount(g)
ecount(g)
degree(g)
betweenness(g)
closeness(g)
page_rank(g)$vector
communities <- cluster_louvain(g)
membership(communities)
modularity(communities)
shortest_paths(g, from = "A", to = "B")
distances(g)
plot(g,
vertex.size = degree(g) * 2,
vertex.color = membership(communities),
edge.arrow.size = 0.5)
library(tidygraph)
library(ggraph)
tg <- as_tbl_graph(g) %>%
activate(nodes) %>%
mutate(
centrality = centrality_degree(),
community = group_louvain()
)
ggraph(tg, layout = "fr") +
geom_edge_link(alpha = 0.5) +
geom_node_point(aes(size = centrality, color = factor(community))) +
geom_node_text(aes(label = name), repel = TRUE) +
theme_graph()
library(visNetwork)
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE) %>%
visLayout(randomSeed = 123)
transitivity(g)
diameter(g)
graph.density(g)
assortativity_degree(g)
Common Workflows
Social Network Analysis
library(igraph)
edges <- read.csv("edges.csv")
nodes <- read.csv("nodes.csv")
g <- graph_from_data_frame(edges, vertices = nodes)
nodes$degree <- degree(g)
nodes$betweenness <- betweenness(g)
nodes$eigenvector <- eigen_centrality(g)$vector
comm <- cluster_louvain(g)
nodes$community <- membership(comm)
head(nodes[order(-nodes$betweenness), ])
Network Visualization
library(ggraph)
library(tidygraph)
tg <- as_tbl_graph(g) %>%
activate(nodes) %>%
mutate(importance = centrality_pagerank())
ggraph(tg, layout = "fr") +
geom_edge_link(aes(alpha = weight)) +
geom_node_point(aes(size = importance)) +
theme_void()
ggraph(tg, layout = "linear", circular = TRUE) +
geom_edge_arc(aes(alpha = weight)) +
geom_node_point(aes(color = factor(community))) +
coord_fixed()
Resources