The Wayback Machine - https://web.archive.org/web/20240707221420/https://www.geeksforgeeks.org/circular-packing-to-visualise-hierarchy-data-in-r/
Open In App

Circular Packing to visualise hierarchy data in R

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are talking about handling hierarchical data using circular packing visualizations. To prepare circular packing with R Programming Language, we will use ggraph package and prepare a bubble to show the hierarchies. 

Circular Packing to visualize hierarchy data in R

Preparing the Hierarchical Data

Here we are going to prepare hierarchical data for demonstration. For this, we will use flare datasets.

R




# Libraries
library(ggraph) # to prepare visualisation
library(igraph) # for network analysis
library(tidyverse) # for data handling
library(viridis) # for generating the color map
 
# data for hierarchical structure
edges = flare$edges
head(edges)


 

 

Output:

 

 

Creating another dataframe for hierarchical structure

 

R




vertices = flare$vertices
head(vertices)


 

 

Output:

 

 

Preparing the graph with dataframe:

 

R




# preparing the graph
mygraph <- graph_from_data_frame( edges,
                                  vertices = vertices )
mygraph


 

 

Output:

 

IGRAPH 6e05b59 DN-- 252 251 --  
+ attr: name (v/c), size (v/n), shortName (v/c) 
+ edges from 6e05b59 (vertex names): 
[1] flare.analytics.cluster->flare.analytics.cluster.AgglomerativeCluster 
[2] flare.analytics.cluster->flare.analytics.cluster.CommunityStructure   
[3] flare.analytics.cluster->flare.analytics.cluster.HierarchicalCluster  
[4] flare.analytics.cluster->flare.analytics.cluster.MergeEdge            
[5] flare.analytics.graph  ->flare.analytics.graph.BetweennessCentrality  
[6] flare.analytics.graph  ->flare.analytics.graph.LinkDistance           
[7] flare.analytics.graph  ->flare.analytics.graph.MaxFlowMinCut          
[8] flare.analytics.graph  ->flare.analytics.graph.ShortestPaths          
+ ... omitted several edges

Visualize Circular Hierarchy 

 

Here we will visualize the dataframe with a hierarchical structure.

 

R




# plot the graph using ggraph
ggraph(mygraph, # graph data
       layout = 'circlepack',
        
       # size of bubbles based on
       # the size parameter in vertices data
       weight = size) +
  geom_node_circle(aes(fill = as.factor(depth),
                       color = as.factor(depth) )) +
  # define the color of each different labels
  scale_color_manual( values=c("0" = "green", "1" = "red",
                               "2" = "red",
                               "3" = "red", "4"="red") ) +
  scale_fill_manual(values = c("0" = "green", "1" = viridis(4)[1],
                               "2" = viridis(4)[2], "3" = viridis(4)[3],
                               "4" = viridis(4)[4])) +
  theme_void()


 

 

Output:

 

 



Similar Reads

How to Create and Visualise Volcano Plot in R
In R, a volcano plot is commonly used in bioinformatics and genomics to visualize differential expression analysis results. It displays fold change on the x-axis and statistical significance on the y-axis, typically represented as -log10(p-value). Volcano plots provide a concise way to identify significantly different features between two condition
6 min read
Circular Barplots and Customisation in R
In this article, we are going to see how to create Circular Barplots and Customisation in R Programming Language. A circular barplot is similar to a barplot, but instead of cartesian coordinates, it uses polar coordinates. A circular barplot is one in which the bars are presented in a circle rather than a line. This article will show you how to cre
3 min read
Data Wrangling in R Programming - Data Transformation
A dataset can be presented in many different ways to the world. Let us look at one of the most essential and fundamental distinctions, whether a dataset is wide or long. The difference between wide and long datasets condenses to whether we prefer to have more rows in our dataset or more columns. A dataset that spotlights on putting additional data
3 min read
Convert an Object to Data Frame in R Programming - as.data.frame() Function
as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters: object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic example of as.data.frame() Function in R C/C++ Code # R
2 min read
Check if the Object is a Data Frame in R Programming - is.data.frame() Function
is.data.frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE. R data.frame is a powerful data type, especially when processing table (.csv). It can store the data as row and columns according to the table. Syntax: is.data.frame(x) Parameters: x: specified data.frame Example 1: # R progra
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: # R program to convert a data frame # into a numeric matrix # Creating a dataframe df1 = data.frame( &quot;Na
2 min read
Modify Data of a Data Frame with an Expression in R Programming - with() Function
with() function in R Language is used to modify data of a data frame by evaluating an expression within the arguments of the function. Syntax: with(x, expr) Parameters: x: Data frame expr: Expression to modify data Example 1: # R program to modify data of an object # Calling predefined data set BOD # Calling with() function with(BOD, {BOD$demand
1 min read
Generate a set of Sample data from a Data set in R Programming - sample() Function
sample() function in R Language creates random sample based on the parameters provided in the function call. It takes either a vector or a positive integer as the object in the function parameter. Syntax: sample(x, size, replace) Parameters: x: indicates either vector or a positive integer or data frame size: indicates size of sample to be taken re
2 min read
data.table vs data.frame in R Programming
data.table in R is an enhanced version of the data.frame. Due to its speed of execution and the less code to type it became popular in R. The purpose of data.table is to create tabular data same as a data frame but the syntax varies. In the below example let we can see the syntax for the data table: C/C++ Code DataTable = data.table(name = c(&quot;
3 min read
Difference Between as.data.frame() and data.frame() in R
R Programming Language contains a large number of data structures, data frames being very crucial in it. It is used to organize the data in a well-arranged tabular manner. Data frames can both be created from scratch as well as other data objects can be converted to data frames easily using large inbuilt R methods. as.data.frame() The as.data.frame
2 min read
Article Tags :
three90RightbarBannerImg