plotly Treemap in R (3 Examples)
Hi! This tutorial will show you how to make treemap plot in plotly in the R programming language.
A treemap is a visual representation of hierarchical data using nested rectangles. It organizes data into rectangles, where the size and color of each rectangle convey information about the data it represents.
Here is a quick overview:
Let’s get into the R code!
Install & Load plotly & dplyr
We will need to first of all install and load the R plotly library and dplyr library before we can begin building visualizations.
We need the dplyr library because it enables us to use the pipe operator (%>% or |>) to pipe lines of code together and run them as a chunk.
So, in your preferred R coding IDE, run the lines of code below to install and load plotly and dplyr:
# install plotly & dplyr install.packages(c("plotly", "dplyr")) # load plotly & dplyr library(plotly) library(dplyr)
With plotly and dplyr installed and loaded into our R programming environment, we can now go on to load the datasets that we will use in this tutorial.
Load Dataset
For the purpose of this tutorial, we will use the popular coffee flavors dataset.
We will download two versions of that dataset. Run the lines of code below to download the datasets:
dfA <- read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv") dfB <- read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv")
If you want to, you can take a look at the first 10 rows of each data frame by running:
head(dfA, 10) # ids labels parents # 1 Aromas Aromas # 2 Tastes Tastes # 3 Aromas-Enzymatic Enzymatic Aromas # 4 Aromas-Sugar Browning Sugar Browning Aromas # 5 Aromas-Dry Distillation Dry Distillation Aromas # 6 Tastes-Bitter Bitter Tastes # 7 Tastes-Salt Salt Tastes # 8 Tastes-Sweet Sweet Tastes # 9 Tastes-Sour Sour Tastes # 10 Enzymatic-Flowery Flowery Aromas-Enzymatic head(dfB, 10) # ids labels parents # 1 Enzymatic-Flowery Flowery # 2 Enzymatic-Fruity Fruity # 3 Enzymatic-Herby Herby # 4 Sugar Browning-Nutty Nutty # 5 Sugar Browning-Carmelly Carmelly # 6 Sugar Browning-Chocolatey Chocolatey # 7 Dry Distillation-Resinous Resinous # 8 Dry Distillation-Spicy Spicy # 9 Dry Distillation-Carbony Carbony # 10 Bitter-Pungent Pungent
So, now that we have loaded the datasets, we can now build a treemap using plotly.
Example 1: Build Treemap
In this first example, we will build a basic treemap from the above datasets. Therefore, run the code below to build the treemap:
fig <- plot_ly( type = "treemap", ids = dfA$ids, labels = dfA$labels, parents = dfA$parents, domain = list(column = 0) ) fig <- fig |> add_trace( type = "treemap", ids = dfB$ids, labels = dfB$labels, parents = dfB$parents, maxdepth = 1, domain = list(column = 1) ) fig <- fig |> layout(grid = list(columns = 2,rows = 1)) fig
In the above example, since we are building a treemap, we specified that in the plot_ly() function by defining the argument type = treemap
.
We also defined other arguments, such as ids =
, parents =
, and labels =
by parsing the respective columns in dfA
to the arguments.
We then piped the add_trace() function to the figure object and defined similar arguments using columns from dfB
.
Lastly, we again piped the layout() function to the figure object wherein we defined the layout grid as 2 columns and 1 row, since we are drawing two plots side-by-side.
Example 2: Change Treemap Marker Color
In this next example, we will change the marker color of the treemap:
fig <- plot_ly( type = "treemap", ids = dfA$ids, labels = dfA$labels, parents = dfA$parents, domain = list(column = 0), marker = list(colors=c("#000", "royalblue", "lightgray", "purple", "#FFF", "lightgray", "lightblue")) ) fig <- fig |> add_trace( type = "treemap", ids = dfB$ids, labels = dfB$labels, parents = dfB$parents, maxdepth = 1, domain = list(column = 1) ) fig <- fig |> layout(grid = list(columns = 2,rows = 1)) fig
In the example above, we introduced and defined a new argument marker =
in the plot_ly()
function.
We parsed a list of colors to the argument using the list() function which changed the colors of the sectors in the treemap.
We can also use a color scale instead of parsing a list of colors to the argument.
Examples of built-in color scales we can use include “Bluered”, “Viridis”, “Reds”, “Earth”, “Greens”, and “YlOrRd”. You can see more examples here.
Apart from that, the code is the same as in the previous example.
Example 3: Change Treemap Text Font Size
In this last example, we will demonstrate how to adjust the text font size of the treemap:
fig <- plot_ly( type = "treemap", ids = dfA$ids, labels = dfA$labels, parents = dfA$parents, domain = list(column = 0), marker = list(colorscale = "Hot") ) fig <- fig |> layout(uniformtext=list(minsize=12, mode="hide")) fig
Here, for the sake of clarity, we have plotted a treemap of only dfA
.
By default, the font size of text labels in the treemap will vary to fit into the available space within a sector.
But, if you want all the text labels in the treemap to have the same font size, you can use the uniformtext =
layout argument.
The minsize
attribute sets the font size, and the mode
attribute sets what happens to labels which cannot fit within a sector with the specified fontsize, either to hide them or to show them with overflow.
Video, Further Resources & Summary
Do you need more explanations on how to make a plotly treemap in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to make a plotly treemap in R.
The YouTube video will be added soon.
With that, we have demonstrated how to make a plotly treemap in the R programming language. As you can see, it is another interesting way to visually represent hierarchical data using nested rectangles.
- How to Draw a plotly Scatterplot in R (Example)
- plotly Sunburst Chart in R (3 Examples)
- How to Draw a plotly Area Chart in R (3 Examples)
- plotly Pie & Donut Chart in R (4 Examples)
- Create Dropdown Menu in plotly Graph in R (Example)
- Introduction to R Programming
This post has shown how to make a plotly treemap in R. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter