<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simple-steps-to-create-treemap-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/simple-steps-to-create-treemap-in-r/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The following document details how to create a treemap in R using the treemap package.</p>
<p><span></span></p>
<p>What are they &amp; when do we use them</p>
<p>In the most basic terms a treemap is generally used when we want to visualize proportions. It can be thought of a pie map where the slices are replaced by rectangles.<br>
Using pie charts to visualize proportion is an excellent way, however if the categories keep on increasing the pie charts tends to become more and more unreadable. This issue of pie charts is overcomed in a Treemap which uses nested structure. These are ideal for displaying large amount of hierarchical data. We can use a treemap when space is a constraint and we have a large amount of hierarchical data to get an overview.</p>
<blockquote>
<p>A treemap is a diagram representing hierarchical data in the form of nested rectangles, the area of each corresponding to its numerical value</p>
</blockquote>
<h2>When not to use a treemap</h2>
<p>A treemap should not be used when there is a big difference between the measure values or the values are not comparable. Also, negative values cannot be displayed on a treemap.</p>
<p>Building a Treemap in R</p>
<p>To create a treemap we use one or more dimension and a maximum of 2 measures. We will be using the treemap package in R. For this article we will use the Super Store data which is provided along with the article.</p>
<p>Step 1: Importing Data and installing treemap package in R</p>
<p><pre>## Set the working directory location to the file location##
>setwd(&quot;H:/R Treemap&quot;)

## Import the datafile in R and view the data sample)
>data= read.csv(&quot;data.csv&quot;, header = TRUE, sep =&quot;,&quot;)
>View(data)
</pre>

<p>Once we get the data in R we need to load the package treemap so that we can go ahead creating our required plot.</p>
<p><pre>## Installing the package and calling the package in R##
>install.packages(&quot;treemap&quot;)
>library(treemap)
</pre>

<p>The data that we are using is already reshaped data and so we can go ahead with creating our basic treemap and move step by step from it.</p>
<p>Step 2: Creating a Treemap</p>
<p>The treemap function is used to create a treemap.</p>
<p><pre>## Creating the most basic treemap##
>treemap(data,index = c(&quot;Category&quot;),vSize =&quot;Sales&quot;)
</pre>

<p>The first argument in the above formula is the data file name which is “data” in our case. The arguments within the index specify the hierarchy that we are looking into and the argument vSize tell R to pick up a values on which the proportion of the boxes are to be decided.<br>
In our case since we have only index in our formula the command just splits the entire tree in three parts ( each representing the proportion of Sales for these part)<br>
A first look into the above figures shows that the Proportion of Technology, Furniture and Office Supplies is almost within the same range , the highest being technology.</p>
<p>We can check our hunch right away, Type in the following :</p>
<p><pre>>aggregate(Sales ~ Category, data, sum)
And you will get the following result :

      Category               Sales
1     Furniture              741999.8
2     Office Supplies        719047.0
3     Technology             836154.0
</pre>

<p>We see how close these Sales are to other (proportionately)</p>
<p>Now as we have created our most basic treemap lets go a bit further and see what happens when we list multiple values in the index ( create a hierarchy )</p>
<p><pre>## Creating a treemap with Category and Subcategory as a hierarchy.
>treemap(data,index = c(&quot;Category&quot;,&quot;Sub.Category&quot;),vSize = &quot;Sales&quot;)
</pre>

<p>Here is what happened, the tree is first splits at the category level and then each category further splits under a subcategory. We can see from the treemap that the Technology Category accounted for maximum sales and within the Technology category, Phones accounted for most of the sales. (the size of the boxes are still by Sales)</p>
<p>Let’s go a step further and color the boxes by another measure let’s say profit.</p>
<p><pre> 
##Coloring the boxes by a measure##
>treemap(data,index = c(&quot;Category&quot;,&quot;Sub.Category&quot;),vSize =&quot;Sales&quot;,vColor = &quot;Profit&quot;,type=&quot;value&quot;)
</pre>

<p>The treemap that we get here is similar to the previous one except for the fact that now the box color represents the Profit instead. So we can see here that the most profitable subcategory was Copiers while on the other hand Tables were the most unprofitable.<br>
The argument vColor tells R to pick up a variable that we want to be used as a color. Type Defines if it is a value, index or categorical.</p>
<p><pre>##Using a categorical variable as color##
>treemap(data,index = c(&quot;Category&quot;,&quot;Region&quot;),vSize =&quot;Sales&quot;,vColor = &quot;Region&quot;,type=&quot;categorical&quot;)
</pre>

<p>Here we see that the tree is split into Categories first and under each category we have all the four region that are distinguished by individual color.</p>
<p>Step 3: Enhancing our treemap</p>
<p>Let’s move ahead and make our treemap more readable. To do this we will add a title to our treemap and change to font size of the Labels for category and Subcategories. We will try to keep the labels for Categories bigger and sub categories a bit smaller. Here’s how to do it:</p>
<p><pre>## Titles and font size of the labels##
>treemap(data,index = c(&quot;Category&quot;,&quot;Sub.Category&quot;),vSize =&quot;Sales&quot;,vColor = &quot;Profit&quot;,type=&quot;value&quot;,title = &quot;Sales Treemap For categories&quot;,fontsize.labels = c(15,10))
</pre>

<p>Notice how we have added a custom title to treemap and change the label size for Categories and Sub categories. The argument title allows us to add title to our visual while the argument fontsize.labels helps in adjusting the size of the labels.</p>
<p>How about positioning the labels ??</p>
<p>How about keeping the Categories labels centered and keeping that of Sub Categories in top left.<br>
This can be achieved by the argument align.labels as under:</p>
<p><pre>## Aligning the labels##
>treemap(data,index = c(&quot;Category&quot;,&quot;Sub.Category&quot;),vSize =&quot;Sales&quot;,vColor = &quot;Profit&quot;,type=&quot;value&quot;,title = &quot;Sales Treemap For categories&quot;,fontsize.labels = c(15,10),align.labels = list(c(&quot;centre&quot;,&quot;centre&quot;),c(&quot;left&quot;,&quot;top&quot;)))
</pre>

<p>There it is, our labels are now aligned beautifully.<br>
We can also choose our custom palette for treemaps using the palette argument as under:</p>
<p><pre>>treemap(data,index = c(&quot;Category&quot;,&quot;Sub.Category&quot;),vSize =&quot;Sales&quot;,vColor = &quot;Profit&quot;,type=&quot;value&quot;,palette=&quot;RdYlGn&quot;,range=c(-20000,60000),mapping=c(-20000,10000,60000),title = &quot;Sales Treemap For categories&quot;,fontsize.labels = c(15,10),align.labels = list(c(&quot;centre&quot;,&quot;centre&quot;),c(&quot;left&quot;,&quot;top&quot;)))
</pre>

<p>Here we have used the custom Red Yellow Green palette to see the profit more clearly.</p>
<p>Red being the most unprofitable and Green being the most profitable. In this article we looked upon how to create a treemap in R and adding aesthetic to our plot. There’s much more that can be done using the arguments under a treemap. For a complete list of argument and functionality refer the package documents.</p>
<p>“Area-based visualizations have existed for decades. This idea was invented by professor Ben Shneiderman at the University of Maryland, Human – Computer Interaction Lab in the early 1990s. Shneiderman and his collaborators then deepened the idea by introducing a variety of interactive techniques for filtering and adjusting treemaps. These early treemaps all used the simple “slice-and-dice” tiling algorithm”.</p>
<p>Treemaps are the most efficient option to display hierarchy that gives a quick overview of structure. They are also great at comparing the proportion between categories via their area sizes.</p>
<h2>Author Bio:</h2>
<p>This article was contributed by <a rel="nofollow" href="https://www.perceptive-analytics.com/tableau-consultants/" target="_blank">Perceptive Analytics</a>. Rahul Singh, Chaitanya Sagar, Jyothirmayee Thondamallu and Saneesh Veetil contributed to this article.<br>
Perceptive Analytics provides data analytics, data visualization, business intelligence and reporting services to e-commerce, retail, healthcare and pharmaceutical industries. Our client roster includes Fortune 500 and NYSE listed companies in the USA and India.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-posts.com/simple-steps-to-create-treemap-in-r/"> R-posts.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/LCzMtr32xv8" height="1"></a></div>