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

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:

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

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

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.



Network Visualization in R using igraph

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.

Similar Reads

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

Visualising Network using igraph

Before moving forward, make sure to install igraph package....

Contact Us