R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Social Network Analysis (SNA) is a method used to analyze and visualize the connections or relationships between nodes in a network. In R, the primary package for Social Network Analysis is igraph
. This tutorial will provide a brief introduction to Social Network Analysis using the igraph
package.
First, you need to install and load the igraph
package:
install.packages("igraph") library(igraph)
Start by creating a simple graph:
g <- graph(edges=c("A", "B", "B", "C", "C", "D", "D", "E", "E", "F"), directed=FALSE) plot(g)
Nodes (also known as vertices) and edges can have attributes:
V(g)$name <- c("Alice", "Bob", "Charlie", "David", "Eve", "Frank") V(g)$age <- c(25, 30, 30, 35, 40, 50) E(g)$relationship <- c("friend", "colleague", "acquaintance", "friend", "family")
Degree: Number of connections a node has.
degree(g)
Diameter: The longest shortest path in the network.
diameter(g)
Shortest Paths: Path with the fewest number of edges between two nodes.
shortest_paths(g, from="Alice", to="Frank")
Centrality measures help identify the most important nodes in a network:
Betweenness Centrality:
betweenness(g)
Closeness Centrality:
closeness(g)
Eigenvector Centrality:
eigen_centrality(g)$vector
Find communities or clusters within the graph:
Using the walktrap method:
wc <- cluster_walktrap(g) plot(wc, g)
You can customize the graph's appearance:
plot(g, vertex.color="lightblue", vertex.size=30, vertex.frame.color="gray", edge.color="gray", edge.width=E(g)$relationship %>% as.factor %>% as.numeric, vertex.label.color="black", layout=layout_nicely(g))
Subgraphs: Extract a portion of the graph.
sg <- induced.subgraph(g, c("Alice", "Bob", "Charlie")) plot(sg)
Combining Graphs: You can combine different graphs.
g1 <- graph(edges=c("X", "Y", "Y", "Z"), directed=FALSE) combined_graph <- graph.union(g, g1) plot(combined_graph)
You can import graphs from various formats like GraphML, GML, Pajek, etc., and also export your graphs:
Read Graph:
g <- read.graph("path_to_graph_file.graphml", format="graphml")
Write Graph:
write.graph(g, file="path_to_save.graphml", format="graphml")
R Packages for Social Network Analysis:
igraph
, statnet
, and others.install.packages(c("igraph", "statnet"))
Analyzing Social Networks with igraph Package in R:
igraph
is a versatile package for creating, analyzing, and visualizing graphs.library(igraph) # Create a graph g <- graph(edges=c(1,2, 2,3, 3,1), n=3)
Visualizing Networks in R:
plot()
or more advanced tools like ggraph
.plot(g)
Creating and Manipulating Graphs in R:
igraph
package to add nodes, edges, and manipulate the structure of the graph.add_edges(g, c(1,3))
Centrality Measures in Social Network Analysis Using R:
igraph
.degree(g) closeness(g) betweenness(g)
Community Detection in R Networks:
community_louvain(g)
Dynamic Network Analysis in R:
library(igraph) dynamic_graph <- graph(edges=data.frame(time=c(1, 2, 3), from=c(1, 2, 3), to=c(2, 3, 1)))
R statnet Package for Network Analysis:
statnet
provides tools for network analysis, including functions for handling network data and fitting models.library(statnet)
Exponential Random Graph Models (ERGMs) in R:
ergm
package.library(ergm)
R Network Visualization Libraries:
visNetwork
and ggraph
provide advanced visualization capabilities for network analysis.library(visNetwork)
Text Mining and Social Network Analysis in R:
# Example: Analyzing co-occurrence network in text