Creating an igraph object in R

Network analysis provides a powerful framework for modeling and analyzing such systems, whether they involve social networks, biological pathways, or information flow. In the R Programming Language, the igraph package stands out as a versatile and efficient tool for performing network analysis tasks.

What is iGraph?

iGraph is a versatile and powerful R package designed for network analysis. Its primary purpose is to provide tools for creating, analyzing, and visualizing networks, which are often represented as graphs composed of nodes (vertices) and edges (connections). In network analysis, iGraph holds significant relevance due to its capability to handle various types of networks, including social networks, biological networks, communication networks, and more. It offers a wide range of functionalities, allowing users to explore and understand the structural properties and dynamics of complex systems.

  1. Graph Construction: iGraph provides functions to create different types of graphs, including directed and undirected graphs, weighted graphs, and bipartite graphs. Users can specify edges and vertices manually or import data from external sources.
  2. Network Analysis: Once a graph is constructed, iGraph offers numerous functions for computing network metrics and properties. These include centrality measures (e.g., degree centrality, betweenness centrality), clustering coefficients, shortest paths, and community detection algorithms.
  3. Visualization: iGraph enables users to create visually appealing visualizations of networks. It supports various layout algorithms for arranging nodes, allowing users to customize the appearance of the plot by adjusting attributes such as colors, sizes, and labels.
  4. Integration: iGraph seamlessly integrates with other R packages, making it easy to incorporate network analysis into broader data science workflows. Users can combine iGraph with packages like tidyverse for data manipulation and ggplot2 for advanced plotting.

How to Create igraph object in R

Creating a graph object involves defining vertices and edges. In this example, we’ll create a simple undirected graph with five vertices and some edges.

R
# Step 1: Load the igraph package
library(igraph)

# Create a graph object
graph <- graph(edges = c(1, 2, 1, 3, 2, 3, 3, 4, 4, 5), n = 5, directed = FALSE)

# Plot the original graph
plot(graph)

Output:

Create basics Graph

Adding Edges on igraph in R

  • To add edges to the graph, we used the add_edges() function.
  • Add an edge between vertices 1 and 5, resulting in a graph with 5 edges.
R
# Step 1: Load the igraph package
library(igraph)

# Create a graph object
graph <- graph(edges = c(1, 2, 1, 3, 2, 3, 3, 4, 4, 5), n = 5, directed = FALSE)

# Add an edge between vertices 1 and 5
graph <- add_edges(graph, c(1, 5))

# Plot the original graph
plot(graph)

Output:

Adding Edges

Removing Edges on igraph in R

  • To remove edges from the graph, we used the delete_edges() function.
  • Removed an edge between vertices 2 and 3, resulting in a graph with 3 edges.
R
# Step 1: Load the igraph package
library(igraph)

# Create a graph object
graph <- graph(edges = c(1, 2, 1, 3, 2, 3, 3, 4, 4, 5), n = 5, directed = FALSE)

# Remove an edge between vertices 2 and 3
graph <- delete_edges(graph, c(2, 3))

# Plot the original graph
plot(graph)

Output:

Removing Edges

Create Circular Chain Graph using igraph

Here’s an example of creating a circular attractive chain using the igraph package in R:

R
# Step 1: Load the igraph package
library(igraph)

# Create a circular attractive chain graph with 8 vertices
circular_chain <- make_ring(8, directed = TRUE)

# Plot the circular chain graph
plot(
  circular_chain, 
  layout = layout_in_circle(circular_chain),  # Arrange vertices in a circle
  vertex.label = LETTERS[1:8],                # Assign labels A to H to vertices
  main = "Circular Attractive Chain Graph"    # Plot title
)

Output:

igraph in R

It creates a directed graph representing a circular chain with 8 vertices (A to H) and arranges them in a circle, making it visually appealing. Each vertex points to the next one, forming a circular chain.

Customization of igraph in R

Now we creates a simple visualization of a graph with 10 vertices arranged providing a basic overview of the graph structure.

R
# Step 1: Load the igraph package
library(igraph)

# Create a basic directed graph
graph <- graph.full(10, directed = TRUE)

# Plot the graph
plot(
  graph, 
  layout = layout_with_fr(graph),  # Fruchterman-Reingold layout
)

Output:

igraph in R

First we creates a directed graph with 10 vertices using the graph.full() function. This function generates a complete graph, meaning that each vertex is connected to every other vertex by a directed edge.

  • The plot() function is used to visualize the created graph. Several arguments are provided to customize the plot.
  • llayout_with_fr(graph) specifies the Fruchterman-Reingold layout algorithm, which positions the vertices based on a force-directed layout, aiming to minimize the energy of the graph.

Conclusion

igraph in R is a powerful tool for studying and visualizing networks. It helps users analyze various types of networks, like social networks or transportation networks, by providing easy-to-use functions. With igraph, we can find important nodes, predict how diseases spread, or even detect fraud in financial networks. Its flexibility and user-friendly interface make it accessible to researchers and analysts in different fields. Overall, igraph is invaluable for understanding the intricate connections and patterns within networks, making it a must-have tool for anyone working with network data in R.



Contact Us