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 in 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.

1. Installation and Loading

First, you need to install and load the igraph package:

install.packages("igraph")
library(igraph)

2. Creating a Simple Graph

Start by creating a simple graph:

g <- graph(edges=c("A", "B", "B", "C", "C", "D", "D", "E", "E", "F"), directed=FALSE)
plot(g)

3. Adding Attributes

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")

4. Basic Analysis

  • 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")
    

5. Centrality Measures

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
    

6. Community Detection

Find communities or clusters within the graph:

  • Using the walktrap method:

    wc <- cluster_walktrap(g)
    plot(wc, g)
    

7. Visualization

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))

8. Advanced Operations

  • 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)
    

9. Importing and Exporting Graphs

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")
    
  1. R Packages for Social Network Analysis:

    • Several R packages facilitate social network analysis, including igraph, statnet, and others.
    install.packages(c("igraph", "statnet"))
    
  2. 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)
    
  3. Visualizing Networks in R:

    • Visualize networks using functions like plot() or more advanced tools like ggraph.
    plot(g)
    
  4. Creating and Manipulating Graphs in R:

    • Use functions in the igraph package to add nodes, edges, and manipulate the structure of the graph.
    add_edges(g, c(1,3))
    
  5. Centrality Measures in Social Network Analysis Using R:

    • Evaluate centrality measures like degree, closeness, and betweenness using functions in igraph.
    degree(g)
    closeness(g)
    betweenness(g)
    
  6. Community Detection in R Networks:

    • Identify communities within networks using algorithms like Louvain or edge-betweenness.
    community_louvain(g)
    
  7. Dynamic Network Analysis in R:

    • Study changes in networks over time using dynamic network analysis techniques.
    library(igraph)
    dynamic_graph <- graph(edges=data.frame(time=c(1, 2, 3), from=c(1, 2, 3), to=c(2, 3, 1)))
    
  8. R statnet Package for Network Analysis:

    • statnet provides tools for network analysis, including functions for handling network data and fitting models.
    library(statnet)
    
  9. Exponential Random Graph Models (ERGMs) in R:

    • ERGMs model the probability of a network based on its structural features. Use the ergm package.
    library(ergm)
    
  10. R Network Visualization Libraries:

    • Libraries like visNetwork and ggraph provide advanced visualization capabilities for network analysis.
    library(visNetwork)
    
  11. Text Mining and Social Network Analysis in R:

    • Combine text mining and social network analysis for a comprehensive understanding of relationships in textual data.
    # Example: Analyzing co-occurrence network in text