How to Create Stunning Treemaps Using treemapify in R

Dec 4, 2023 | Data Science

Treemaps are a powerful visualization tool for displaying hierarchical data. With the treemapify package in R, you can effortlessly create treemaps that effectively represent data in a colorful and intuitive way. In this article, we’ll guide you through the installation process, utilizing sample data, and various features that treemapify offers. Let’s get started!

Installation Guide

To use treemapify, you first need to install it. Here’s how:

  • For the Stable Release Version: You can install it directly from CRAN with:
  • install.packages("treemapify")
  • For the Development Version: If you want to access the latest features, install it from GitHub using:
  • devtools::install_github("wilkoxtreemapify")

Understanding the G20 Dataset

treemapify includes a built-in example dataset with statistics about the G-20 group of major economies. Here’s a glimpse of what it looks like:

G20
#           region        country gdp_mil_usd   hdi econ_classification
# 1         Africa   South Africa      384315 0.629          Developing
# 2  North America  United States    15684750 0.937            Advanced
# 3  North America         Canada     1819081 0.911            Advanced
# 4  North America         Mexico     1177116 0.775          Developing
# 5 South America         Brazil     2395968 0.730          Developing
# ... (co... more countries)

This dataset includes various attributes such as GDP, Human Development Index (HDI), and economic classification.

Creating Your First Treemap

Here’s how to draw your first treemap with the G20 dataset. Each tile will represent a country, with its area proportional to its GDP, while the fill color will indicate the HDI:

ggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) + 
  geom_treemap()

Initially, this plot may not seem very informative without country labels. Let’s enhance it by adding labels to each tile:

ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country)) + 
  geom_treemap() + 
  geom_treemap_text(fontface = "italic", colour = "white", place = "centre", grow = TRUE)

Subgroup Tiles for Enhanced Clarity

You can further clarify your treemap by subgrouping countries based on their regions. Try the following code:

ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country, subgroup = region)) + 
  geom_treemap() + 
  geom_treemap_subgroup_border() + 
  geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5, colour = "black", fontface = "italic") + 
  geom_treemap_text(colour = "white", place = "topleft", reflow = TRUE)

This will give you visually distinct tiles for each region, making comparisons much easier.

Creating Animated Treemaps

Want to bring your treemap to life? You can create animated treemaps that show changes over time! Start by incorporating the gganimate package as shown:

library(gganimate)
library(gapminder)

p <- ggplot(gapminder, aes(label = country, area = pop, subgroup = continent, fill = lifeExp)) + 
  geom_treemap(layout = "fixed") + 
  geom_treemap_text(layout = "fixed", place = "centre", grow = TRUE, colour = "white") + 
  geom_treemap_subgroup_text(layout = "fixed", place = "centre") + 
  geom_treemap_subgroup_border(layout = "fixed") + 
  transition_time(year) + 
  ease_aes('linear') + 
  labs(title = "Year: {frame_time}")

anim_save("animated_treemap.gif", p, nframes = 48)

This creates a captivating animation that illustrates changes in population or life expectancy data over time.

Troubleshooting Tips

If you encounter any issues while using the treemapify package, here are some tips to help you out:

  • Ensure that all necessary packages are installed and loaded correctly.
  • If your labels don’t appear, check if they are being hidden due to size constraints. Adjust the min.size argument in geom_treemap_text.
  • When creating animated treemaps, ensure you specify layout = "fixed" consistently across geom layers to avoid layout mismatches.
  • For additional insights or collaboration on AI projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

Now you have the knowledge to create professional and captivating treemaps using the treemapify package in R! With its powerful features and flexibility, you can produce insightful visualizations that effectively communicate complex data relationships.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox