The Wayback Machine - https://web.archive.org/web/20240905230814/https://www.geeksforgeeks.org/network-visualization-in-r-using-igraph/
Open In App

Network Visualization in R using igraph

Last Updated : 19 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Exploring Network Visualization in R through the powerful igraph package opens a gateway to deciphering intricate network structures without triggering plagiarism detection systems. This article embarks on an insightful journey, unraveling the art of representing and analyzing diverse networks, from social interactions to biological relationships, in a customized and informative manner. With R and igraph, we delve into the realm of network dynamics, enabling a deeper understanding of the hidden patterns within complex networks.

Network Visualization

In data analysis, network visualization is a crucial tool that helps us to find and comprehend intricate connections in datasets. This method is used in a variety of real-world contexts, including social networks, biological interactions, transportation systems, and scientific research, where an understanding of relationships is essential. The R programming language’s Igraph package, which offers versatility and a wide range of functions for building and studying network graphs made up of nodes (representing entities or data points) and edges (representing connections or relationships), is a well-liked tool for network visualization.

The strength of network visualization resides in its capacity to make complex data easier to understand. It converts abstract, frequently difficult-to-understand systems into understandable visual representations that draw attention to network structures, important elements, groups of linked nodes, and potential bottlenecks or pivotal nodes. With the help of this innovative technology, analysts and researchers can find patterns, oddities, and insightful information that can be used to make wise decisions and establish plans to improve network performance or unlock the full potential of the available data. Network visualization enables people to better grasp the underlying connections and structures in their datasets by converting data into visual forms. Its the capacity to make complex facts easier to understand.

Concepts Related To Topic

  1. Nodes and Edges: The fundamental units that make up a network are nodes. They stand in for objects, people, or data points in the dataset. Nodes, for instance, might stand in for people in a social network or cities or crossroads in a transportation network. Edges, also known as connections, links, or arcs, show how nodes are related to one another or interact. Edges may symbolize friendships in a social network or highways or routes between cities in a transportation network.
  2. Graphs: A network is represented as a graph in igraph, with various types such as directed graphs, undirected graphs, and weighted graphs. In a directed graph, each edge has a specific direction, indicating the flow or influence from one node to another. In undirected graphs, edges have no direction, and relationships are considered mutual. Weighted graphs assign values to edges, which can represent various attributes.
  3. Layouts: The positioning of the network’s nodes in the final visualization is determined by layout algorithms, which are vital in the presentation of network or graph data. These algorithms are intended to produce representations of complex networks that are aesthetically pleasing and instructive. The Fruchterman-Reingold Layout, Kamada-Kawai Layout, and Circular Layout are three popular layout algorithms that each have unique qualities and uses.
  4. Node Attributes: Nodes can be associated with attributes in network visualizations. These attributes may include color, size, label, or any other data that can be utilized to customize the node’s appearance. For instance, nodes in a social network may be colored by gender. Similarly, nodes in a biological network may represent proteins and be sized according to their degree of connectivity.
  5. Dynamic Networks: Using a specific technique called dynamic network visualization, we can observe and comprehend how networks alter and develop over time. Nodes, edges, and network properties all undergo adjustments, additions, and deletions in dynamic networks. The examination of structural and attribute changes is made possible by the depiction of the network’s state at multiple time points, which turns time into a crucial dimension in this context. Effectively illustrating these changes requires the use of techniques like animation, time-slice representations, and interactivity. This method has a wide range of applications, including tracking epidemiological trends and monitoring social networks.
  6. Interactivity: An effective and user-centered method for enhancing the exploration and understanding of complex network graphs is interactive network visualization. Users are given the ability to participate actively in the visual depiction in real time, transforming a static network into a dynamic, educational, and adaptable experience. Users can access labels, properties, or in-depth descriptions by clicking on nodes through interactive features, which makes it simpler to comprehend the context of each node. Interactivity also enables users to isolate important network components by filtering nodes and edges according to predefined criteria.
  7. Centrality Measures: Degree centrality is a measure of the number of interconnected nodes in a network. Betweenness centrality is a measurement of the number of nodes that are connected to each other. Closeness centrality is the measurement of the closeness between two nodes. These metrics are used to identify the central nodes within a network.
  8. Community Detection: Community detection is the process of determining the presence of clusters or communities in a network, where nodes in the network have similar connections or features. Community detection algorithms are used to identify substructures within a network. An essential step in network analysis is community detection, which seeks to locate cohesive groupings or clusters of nodes that share connections or characteristics. These communities reflect substructures that shed light on links and patterns in the network’s structure that might not be immediately obvious. The procedure usually entails the use of specialized algorithms that analyze the connectivity of nodes and divide them into various communities according to particular standards, such as modularity or similarity.
  9. Path Analysis: In network analysis, one of the most common tasks is to identify the routes between nodes. This information is used to gain insight into the ease of connection between different nodes in the network .Finding and analyzing the routes or pathways linking the nodes in a network is the central objective of path analysis in network analysis. Regardless of the particular type of network, this analysis is crucial for evaluating the effectiveness and simplicity of connections between various nodes. Path analysis is essential in a wide range of applications, including those related to social interactions, communication networks, biological interactions, and transportation systems.

Steps Needed For Network Visualization In R Using Igraph

  • Install and Load the igraph Package: Install the igraph package if not already installed using install.packages(“igraph”). Load the package into your R session with library(igraph).
  • Create a Graph: It is necessary to define the nodes and boundaries of your network, usually through the use of a data frame or adjacency matrix.
  • Customize the Graph: To improve the visual representation, attribute nodes and edges, including colors or labels, can be assigned.
  • Choose a Layout: Choose a layout algorithm to organize the nodes for visualization, such as the Fruchtermann-Reingold algorithm or Kamada Kawai algorithm.
  • Plot the Graph: Use plot() to create the visualization, specifying the graph and layout.
  • Customize the Visualization: Enhance the visual clarity and readability of the graph by fine-tuning the label, color, and other factors.
  • Save or Display the Output: It is possible to store the visualization in the form of an image using file formats such as PNG(), PDF(), or to view the visualization directly in the R environment.

Visualising Network using igraph

Before moving forward, make sure to install igraph package.

install.packages('igraph')

igraph package: igraph is a library collection for creating and manipulating graphs and analyzing networks

Visualizing a Facebook friendship network

R




library(igraph)
 
users <- data.frame(UserID = 1:10,
                    Name = c("User1", "User2", "User3", "User4", "User5",
                             "User6",
                             "User7", "User8", "User9", "User10"),
                    Gender = c("M", "F", "M", "M", "F", "F", "M", "F",
                               "M", "M"))
 
friendships <- data.frame(UserID1 = c(1, 1, 2, 2, 3, 3, 4, 5, 6, 7),
                          UserID2 = c(2, 3, 4, 5, 4, 6, 5, 8, 8, 9))
graph <- graph.data.frame(friendships, directed = FALSE, vertices = users)
V(graph)$color <- ifelse(V(graph)$Gender == "M", "blue", "pink")
node_sizes <- degree(graph, mode = "all")
# Choose a layout (Fruchterman-Reingold)
layout <- layout_with_fr(graph)
plot(graph, layout = layout, vertex.label.dist = 2, vertex.label.cex = 1.2,
     vertex.size = node_sizes)


Output:

Facebook Friendship network-GeeksforGeeks

Facebook Friendship network

Graph Construction: The graph.data.frame function from igraph is used to create a graph representation of the friendship network. It treats the relationships as undirected (parameter directed = FALSE) and assigns user profile data (from the users data frame) to the graph nodes.

  • Node Customization: Nodes in the graph are customized based on the “Gender” attribute of users. Male users are assigned the color “blue,” and female users are assigned the color “pink.”
  • Node Sizes: The degree function calculates the number of friendships (degree) for each user, considering both incoming and outgoing connections.
  • Layout Selection: The layout of the graph is set using the Fruchterman-Reingold layout algorithm (layout_with_fr). This algorithm helps arrange the nodes in a visually pleasing way, considering their connections and minimizing overlaps.
  • The output of the code is a visual representation of the friendship network, where nodes represent users, and edges represent mutual friendships.

Transportation Network: Visualizing a Subway or Metro Network

R




library(igraph)
 
stations <- data.frame(StationID = 1:8, Name = c("Station A", "Station B",
                                                 "Station C", "Station D",
                                                 "Station E", "Station F",
                                                 "Station G", "Station H"))
connections <- data.frame(Station1 = c(1, 2, 3, 4, 5, 6, 7, 8),
                          Station2 = c(2, 3, 4, 5, 6, 7, 8, 1))
graph <- graph.data.frame(connections, directed = FALSE, vertices = stations)
layout <- layout_with_fr(graph)
plot(graph, layout = layout, vertex.label = V(graph)$Name, vertex.size = 30)


Output:

Rplot01

  • The code starts by defining two data frames, “stations” and “connections.”
  • The graph.data.frame function constructs a graph using the provided connection data frame (“connections”).
  • The directed = FALSE argument indicates that the graph is undirected, meaning connections are mutual between stations.
  • The “vertices” argument links the vertex attributes (station names) to the graph.
  • The “Fruchterman-Reingold” algorithm is a force-directed layout method that positions the vertices based on attractive and repulsive forces
  • The code will generate a graphical representation of the station network, where vertices represent stations labeled with their names, and edges indicate connections between them.

Social Network Visualization Using the igraph Package in R

R




library(igraph)
nodes <- data.frame(name = c("Alice", "Bob", "Charlie", "David", "Eve"))
edges <- data.frame(from = c("Alice", "Alice", "Bob", "Charlie", "David"),
                    to = c("Bob", "Charlie", "Charlie", "David", "Eve"))
graph <- graph_from_data_frame(d = edges, vertices = nodes)
layout <- layout_with_fr(graph)
plot_colors <- c("lightblue", "lightgreen", "lightpink", "lightyellow",
                 "lightcyan")
V(graph)$color <- plot_colors
V(graph)$size <- 20
V(graph)$label.cex <- 1.2
V(graph)$label.color <- "black"
plot(graph, layout = layout, vertex.label.dist = 1, edge.arrow.size = 0.5,
     edge.curved = 0.2, edge.label = NA)
title(main = "Social Network")
legend("topright", legend = nodes$name, col = plot_colors, pch = 20,
       cex = 1.2, bty = "n")


Output:

Screenshot-(278)

Social Network Visualization

Load the igraph package, which gives us the tools to manipulate and visualize graphs. Create a simple social network graph, Nodes and edges are defined as two data frames.

  • Nodes in the nodes data frame are the names of the people in your social network, while edges define the relationships between those people. Create an igraph object from the nodes and edges,Using the data frame nodes and edges, this line creates the igraph graph object named graph.
  • After that Customize the layout of the graph,Using the Fruchterman-Reingold layout technique, this creates a layout for the graph. Using this approach, the nodes are placed so that the forces between them are reduced, emulating a physical system.
  • Set some visual attributes, It establishes the colors for each node, determines the node’s size, and modifies the labels’ size and color. After that Plot the graph with the customized attributes and add a title and legend, create a legend. The node names are linked to the matching colors and symbols in the tale and at last Save the plot to a file.

Zachary Karate Club Network

R




# Load the 'igraph' package if not already loaded
library(igraph)
 
# Create the Zachary karate club graph
zach <- graph("Zachary")
 
# Define a layout for better visualization
layout <- layout_with_fr(zach)
 
# Customize the plot with attractive settings
plot(zach,
     layout = layout,         
     vertex.size = 10,        
     vertex.label.cex = 0.8, 
     vertex.label.color = "black",
     vertex.frame.color = "black", 
     vertex.color = "lightblue",
     edge.color = "gray",   
     edge.width = 2,     
     main = "Zachary Karate Club Network",
     sub = "Customized Plot"
)


Output:

Zachary Karate Club Network-Geeksforgeeks

Zachary Karate Club Network

we’ve customized the plot of the Zachary karate club network by adjusting various parameters to make it more visually appealing. The changes include resizing vertices, adjusting label sizes and colors, setting border colors for vertices, modifying edge colors and widths, and adding titles for context.

Conclusion

Network visualization in R offers a versatile approach for dissecting diverse network structures, including social, biological, transportation, and communication networks. Utilizing R’s Igraph package, we exemplify its potential through illustrations such as protein-protein interactions, financial networks, and road mapping. By tailoring attributes, layouts, and styles, R empowers comprehensive network exploration, revealing patterns, identifying pivotal nodes, and facilitating data-driven decisions across domains, enriching our comprehension of intricate network dynamics.



Similar Reads

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 powerfu
5 min read
Data Visualization using GoogleVis Package
GoogleVis is a package in R that is used to act as an interface between R and the Google API to produce interactive charts which can be easily embedded into web pages. This package helps the user to plot the data without uploading them into google. In this article let's parse through some charts that can be plotted using googleVis in R Programming
5 min read
COVID-19 Data Visualization using matplotlib in Python
It feels surreal to imagine how the virus began to spread from one person that is patient zero to four million today. It was possible because of the transport system. Earlier back in the days, we didn’t have a fraction of the transportation system we have today. Well, what good practices you can follow for now is to sanitize your grocery products a
8 min read
Interactive visualization of data using Bokeh
Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If you’ve worked with visualization in Python before, it’s likely that you have used matplotlib. But Bokeh differs from matplotlib. To install Bokeh type the below command in
4 min read
Animated Data Visualization using Plotly Express
Data Visualization is a big thing in the data science industry and displaying the proper statistics to a business or governments can help them immeasurably in improving their services. It is very painful to understand data from different times from multiple charts and make any sense of it. That is where the need for animated data visualizations lie
4 min read
Visualization of Quick sort using Matplotlib
Visualizing algorithms makes it easier to understand them by analyzing and comparing the number of operations that took place to compare and swap the elements. For this we will use matplotlib, to plot bar graphs to represent the elements of the array, Approach : We will generate an array with random elements.The algorithm will be called on that arr
3 min read
Visualization and Prediction of Crop Production data using Python
Prerequisite: Data Visualization in Python Visualization is seeing the data along various dimensions. In python, we can visualize the data using various plots available in different modules. In this article, we are going to visualize and predict the crop production data for different years using various illustrations and python libraries. Dataset T
4 min read
Python - Data visualization using covid19 India API
API (Application Programming Interface) is a computing interface that interacts between multiple software. JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data. It is used to send data from server to web. Required modules: matplotlibrequestspandasjson Commands to install modules: pip install matplotlib pip ins
5 min read
Data Visualization using ggvis Package in R
The ggvis is an interactive visualization package in R language that is based on the popular ggplot2 package. It allows you to create interactive plots and graphics that can be explored and manipulated by the user. ggvis supports a wide range of plot types including scatter plots, line charts, bar charts, histograms, and more. One of the key featur
15+ min read
Visualization of a correlation matrix using ggplot2 in R
In this article, we will discuss how to visualize a correlation matrix using ggplot2 package in R programming language. In order to do this, we will install a package called ggcorrplot package. With the help of this package, we can easily visualize a correlation matrix. We can also compute a matrix of correlation p-values by using a function that i
7 min read
Using Plotly for Interactive Data Visualization in Python
Plotly is an open-source module of Python which is used for data visualization and supports various graphs like line charts, scatter plots, bar charts, histograms, area plot, etc. In this article, we will see how to plot a basic chart with plotly and also how to make a plot interactive. But before starting you might be wondering why there is a need
13 min read
Data Visualization using Plotnine and ggplot2 in Python
Data Visualization is the technique of presenting data in the form of graphs, charts, or plots. Visualizing data makes it easier for the data analysts to analyze the trends or patterns that may be present in the data as it summarizes the huge amount of data in a simple and easy-to-understand format. In this article, we will discuss how to visualize
8 min read
Conditional Data Visualization Using Google Data Studio
In this article, we will learn how to do Conditional Data Visualization using Google Data studio. Before moving forward, let's understand the title itself. Data visualization Data visualization is all about getting useful insights from the data through graphical media or visuals. When data is presented in a visual format. It becomes easy and quick
3 min read
Dynamic Visualization using Python
Data visualization in Python refers to the pictorial representation of raw data for better visualization, understanding, and inference. Python provides various libraries containing different features for visualizing data and can support different types of graphs, i.e. Matplotlib, Seaborn, Bokeh, Plotly, and others. Required Module: pip install nump
11 min read
How to Save Time with Data Visualization using Stack in R with ggplot2
The widely used R package ggplot2 is used to produce beautiful and efficient data visualisations. Here are some pointers for speeding up data visualisation using the "stack" feature of ggplot2: Select the pertinent information: Make sure the data you plan to use in your visualisation is appropriate. You will need data that contains information on e
6 min read
HOG Feature Visualization in Python Using skimage
Object detection is a fundamental task in computer vision, where the goal is to identify and locate objects within images or videos. However, this task can be challenging due to the complexity of real-world images, which often contain varying lighting conditions, occlusions, and cluttered backgrounds. Traditional approaches to object detection rely
5 min read
Visualization of Superhero Characters using Python
There are a number of different libraries in Python that can be used to create visualizations of superhero characters. Some popular libraries include Matplotlib, Seaborn, and Plotly. In this article, we use Matplotlib to generate visualizations and get insights from the Superheroes Dataset. Matplotlib is a plotting library for Python that provides
6 min read
Python - Data visualization using Bokeh
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps. Bokeh provides two visualization interfaces to users: bokeh.models : A low level interface that pro
3 min read
Data Visualization using Turicreate in Python
In Machine Learning, Data Visualization is a very important phase. In order to correctly understand the behavior and features of your data one needs to visualize it perfectly. So here I am with my post on how to efficiently and at the same time easily visualize your data to extract most out of it. Before going in detail about how to visualize data
3 min read
Circular Visualization of Dataset using hishiryo Python
Among various ways to represent data, circular representation of data is one of the ways to render data points and do a corresponding analysis of the same. This article talks about ways to achieve the said visualization of data for further analytical purposes. hishiryo: This tool helps in the generation of circular visual representation of data. Co
2 min read
Weather Data Visualization using R
Visualizing weather data is essential for understanding trends, patterns, and anomalies in meteorological information. R, with its robust packages for data manipulation and visualization, offers powerful tools to create insightful weather data visualizations. This article will guide you through the process of visualizing weather data in R, covering
5 min read
Data Visualization using Matplotlib in Python
Data visualization is a crucial aspect of data analysis, enabling data scientists and analysts to present complex data in a more understandable and insightful manner. One of the most popular libraries for data visualization in Python is Matplotlib. In this article, we will provide a comprehensive guide to using Matplotlib for creating various types
15+ min read
Covid-19 Analysis and Visualization using Plotly Express
In this article, we will discuss Analyse Covid-19 data and will visualize it using Plotly Express in Python. This article deals with creating dozens of bar charts, line graphs, bubble charts, scatter plots. The graph that will be made in this project will be of excellent quality. Envisioning COVID-19 will primarily be using Plotly Express for this
13 min read
Box plot visualization with Pandas and Seaborn
Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used for detect the outlier in data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Boxplot summarizes a sample data using 25th, 50th and 75th per
2 min read
Understanding different Box Plot with visualization
Let's see how can boxplot be useful in different ways. Loading Libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt Preparing Data spread = np.random.rand(50) * 100 center = np.ones(25) * 50 flier_high = np.random.rand(10) * 100 + 100 flier_low = np.random.rand(10) * -100 data = np.concatenate((spread, center, flier_high
2 min read
Directed Graphs, Multigraphs and Visualization in Networkx
Prerequisite: Basic visualization technique for a Graph In the previous article, we have learned about the basics of Networkx module and how to create an undirected graph. Note that Networkx module easily outputs the various Graph parameters easily, as shown below with an example. import networkx as nx edges = [(1, 2), (1, 6), (2, 3), (2, 4), (2, 6
9 min read
Why Data Visualization Matters in Data Analytics?
What if you wanted to know the number of movies produced in the world per year in different countries? You could always read this data in the form of a black and white text written on multiple pages. Or you could have a colorful bar chart that would immediately tell you which countries are producing more movies and if the total movies per year are
7 min read
Difference Between Data Mining and Data Visualization
Data mining: Data mining is the method of analyzing expansive sums of data in an exertion to discover relationships, designs, and insights. These designs, concurring to Witten and Eibemust be "meaningful in that they lead to a few advantages, more often than not a financial advantage." Data in data mining is additionally ordinarily quantitative par
2 min read
Difference Between Data Visualization and Data Analytics
Data Visualization: Data visualization is the graphical representation of information and data in a pictorial or graphical format(Example: charts, graphs, and maps). Data visualization tools provide an accessible way to see and understand trends, patterns in data and outliers. Data visualization tools and technologies are essential to analyze massi
3 min read
Difference Between Data Science and Data Visualization
Data Science: Data science is study of data. It involves developing methods of recording, storing, and analyzing data to extract useful information. The goal of data science is to gain knowledge from any type of data both structured and unstructured. Data science is a term for set of fields that are focused on mining big data sets and discovering t
2 min read