Open In App

Treemap with ggplot2 and treemapify in R

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn Treemap with ggplot2 and treemapify in R programming language.

Introduction

In general, TreeMaps are used to visualize the hierarchical data. As the name indicates, the tree maps are divided into rectangular boxes where the first rectangular box is the root of all the boxes present in the graph, and the hierarchy from most to least valued data is plotted in the TreeMap. In R Programming these maps can be implemented using functions present in ggplot2 and treemapify packages.

Methods to plot Tree Map In R

Basic treemap with geom_treemap

Step 1: First we need to install and load all the required packages

R




# Installing required packages
install.packages("ggplot2")
install.packages("treemapify")
  
# Importing required library
library(ggplot2)
library(treemapify)


Step 2: Next let’s create a sample data frame of fruit sales.

R




# Creating Dataframe
df<- data.frame(Fruits=c("Banana","Apple","Melon",
 "Plums","Pineapple","Orange","Apricot","Grapes"),
                sales=c(25000,22000,15000,5000,
                        18000,20000,3000,9000))


Step 3: Let’s now plot the Simple TreeMap with the help of ggplot() and geom_treemap() functions.

R




# Importing required library
library(ggplot2)
library(treemapify)
  
# Creating dataframe
df<- data.frame(Fruits=c("Banana","Apple","Melon",
 "Plums","Pineapple","Orange","Apricot","Grapes"),
                sales=c(25000,22000,15000,5000,
                        18000,20000,3000,9000))
# Plotting the TreeMap
ggplot2::ggplot(df,aes(area=sales,fill=sales))+
  treemapify::geom_treemap()


Output:

Treemap with ggplot2 and treemapify in R

 

Adding labels to the tiles

To make the plot more interactive let’s Customize the above TreeMap by adding text labels to each and every rectangular box and also by adding the title to the plot.

R




# Importing required library
library(ggplot2)
library(treemapify)
  
# Creating Dataframe
df<- data.frame(Fruits=c("Banana","Apple","Melon",
   "Plums","Pineapple","Orange","Apricot","Grapes"),
                sales=c(25000,22000,15000,5000,
                        18000,20000,3000,9000))
  
# Plotting TreeMap Graph
ggplot2::ggplot(df,aes(area=sales,fill=Fruits,label=Fruits))+
  treemapify::geom_treemap(layout="squarified")+
  geom_treemap_text(place = "centre",size = 12)+
  labs(title="Customized Tree Plot using ggplot and treemapify in R")


Output:

Treemap with ggplot2 and treemapify in R

 

Subgroup tree plot

The subgrouped tree plot in our example the fruits are divided based on their availability in different seasons can be plotted by initialization the subgroup attribute in aesthetics(aes) of the plot in ggplot() function as follows:

R




# Importing required library
library(ggplot2)
library(treemapify)
  
# Creating dataframe
df<- data.frame(Fruits=c("Banana","Apple","Melon",
     "Plums","Pineapple","Orange","Apricot","Grapes"),
                Season=c("All Time","Winter","Summer",
    "All Time","Winter","Summer","All Time","All Time"),
                sales=c(25000,22000,15000,5000,
                        18000,20000,3000,9000))
  
# Plotting TreeMap Graph
ggplot2::ggplot(df,aes(area=sales,fill=Season,
                       label=Fruits,subgroup=Season))+
  treemapify::geom_treemap(layout="squarified")+
  geom_treemap_text(place = "centre",size = 12)+
  labs(title="Sub Grouped Tree Plot using ggplot and treemapify in R")


Output:

Treemap with ggplot2 and treemapify in R

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads