Creating an igraph object in R
Last Updated :
27 May, 2024
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.
- 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.
- 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.
- 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.
- 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 EdgesRemoving 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 EdgesCreate 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 RIt 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 RFirst 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.
Similar Reads
Generating a Call Graph in R
In this article, weâll discuss how to generate a call graph in R, covering the theory behind call graphs and practical examples to visualize them using R Programming Language.Understanding Call GraphsA call graph is a visual representation of function calls within a program. In R, this helps you und
4 min read
R - Creating, Listing, and Deleting Objects in Memory
One of the most interesting facts about R is, what is known as objects in R are known as variables in many other programming languages. Depending on the context objects and variables can have drastically different meanings. In every computer language variables provide a means of accessing the data s
7 min read
How to Create an Ogive Graph in Excel?
Ogive graph, the name might scare you, but believe me this is very simple to create. The ogive graph is a cumulative frequency graph. The graph is plotted between the fixed intervals vs the frequency added up before. It is a curve plotted for cumulative frequency distribution on a graph. There are t
4 min read
How to create a Venn Diagram in R ?
Venn diagram is the graphical representation of sets used for showing the relationship between them. Through the use of Venn diagram one can highlight the differences as well as similarities between elements of sets. Venn diagram is also known as Logic diagram or set diagram. We use circles to repre
7 min read
How to Create a Forest Plot in R?
In this article, we will discuss how to create a Forest Plot in the R programming language. A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in med
4 min read
R - Charts and Graphs
R language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, charts and graphs are used in R. R - graphs There are hundreds of charts and graphs present in R. For example, bar plot, box plot, mosaic plot
6 min read
How To Create A Pictograph In Excel?
The Pictograph is the record consisting of pictorial symbols. Generally, in mathematics, it is represented by the help of graphs with pictures or icons representing certain quantities or numbers of people, books, etc. It is also known as pictogram, pictogramme, pictorial chart, picture graph, or sim
3 min read
How to Plot a Correlation Matrix into a Graph Using R
A correlation matrix is a table showing correlation coefficients between sets of variables. It's a powerful tool for understanding relationships among variables in a dataset. Visualizing a correlation matrix as a graph can provide clearer insights into the data. This article will guide you through t
4 min read
How to Create Interaction Plot in R?
In this article, we will discuss how to create an interaction plot in the R Programming Language. The interaction plot shows the relationship between a continuous variable and a categorical variable in relation to another categorical variable. It lets us know whether two categorical variables have a
3 min read
Graphical Data Analysis in R
Graphical Data Analysis (GDA) is a powerful tool that helps us to visualize and explore complex data sets. R is a popular programming language for GDA as it has a wide range of built-in functions for producing high-quality visualizations. In this article, we will explore some of the most commonly us
7 min read