Visualizing treemaps in R: Comparing Gross National Income (GNI) across countries using the GNI 2014 dataset

Data visualization can help you understand complex data at a glance. In particular, treemaps are very useful for visualizing a large number of data points. In this post, we'll learn how to use the R programming language to create a treemap that compares the gross national income (GNI) of countries based on the GNI 2014 dataset.

The concept of treemap

A treemap is a visualization method that represents hierarchical data as rectangular-shaped tiles. The size of each tile is proportional to the value of that item, and the color can represent additional information. Treemaps are especially useful when you have a lot of data points and want to easily compare the relative size of each item.

R code examples

Install and load the necessary packages to create a treemap using the GNI 2014 dataset.

# Required package list
packages <- c("ggplot2", "treemapify", "dplyr", "viridis")

# Checking for and installing uninstalled packages
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

Load the # package
lapply(packages, library, character.only = TRUE)

Import the GNI 2014 dataset and perform data preprocessing.

Load the # GNI2014 dataset
data(GNI2014)

# Data Preprocessing
gni2014 %
mutate(label = ifelse(GNI > quantile(GNI, 0.75), country, ""))

Write code to generate a treemap.

Copy the R codeCreate a # Treemap
ggplot(GNI2014, aes(area = GNI, fill = GNI, label = label, subgroup = continent)) +
geom_treemap() +
geom_treemap_subgroup_border(color = "white", size = 2) +
geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5, colour = "black", fontface = "bold") + geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5, colour = "black", fontface = "bold")
geom_treemap_text(color = "white", place = "centre", size = 10, grow = TRUE) +
# Apply the modified color scale
scale_fill_gradient(low = "yellow", high = "purple", breaks = c(0, 25000, 50000, 75000, 100000), labels = scales::comma) +
labs(title = "GNI 2014 Dataset Treemap",
subtitle = "Comparison of Gross National Income (GNI) by country",
caption = "Data: GNI2014") +
theme_minimal() +
theme(legend.position = "bottom",
legend.key.width = unit(2, "cm"), # Adjust the width of the legend key
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, size = 16),
plot.caption = element_text(hjust = 1, size = 10))

The code above uses the ggplot2 package to generate a treemap. aes In the function, the area of each tile is set based on its GNI value, and the color is set based on its GNI value. Also, Display the names of the countries with the top 25% GNI valuesfor the country. You can adjust this value to show the names of invisible countries.

Full source code

# List of required packages
packages <- c("ggplot2", "treemapify", "dplyr", "viridis")

# Check for and install uninstalled packages
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

Load the # package
lapply(packages, library, character.only = TRUE)

Load the # GNI2014 dataset
data(GNI2014)

Preprocess the # data
gni2014 %
    mutate(label = ifelse(GNI > quantile(GNI, 0.75), country, ""))

Generate a # treemap
ggplot(GNI2014, aes(area = GNI, fill = GNI, label = label, subgroup = continent)) +
    geom_treemap()
    geom_treemap_subgroup_border(color = "white", size = 2) +
    geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5, colour = "black", fontface = "bold") +
    geom_treemap_text(color = "white", place = "centre", size = 10, grow = TRUE) +
    # Apply the modified color scale
    scale_fill_gradient(low = "yellow", high = "purple", breaks = c(0, 25000, 50000, 75000, 100000), labels = scales::comma) +
    labs(title = "GNI 2014 dataset treemap",
         subtitle = "Comparison of Gross National Income (GNI) across countries",
         caption = "Data: GNI2014") +
    theme_minimal()
    theme(legend.position = "bottom",
          legend.key.width = unit(2, "cm"), # adjust legend key width
          plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
          plot.subtitle = element_text(hjust = 0.5, size = 16),
          plot.caption = element_text(hjust = 1, size = 10))

Common mistakes and how to avoid them

A common mistake when creating treemaps is that the tiles are too small and the labels are hard to see. To avoid this, you need to use proper data preprocessing and labeling strategies. You also need to be careful about overusing colors, as they can become visually cluttered.

Organize

If you run the source code in the above order, you'll get the following output. Hereis a little nicer than the one we generated in the previous example, right?

treemap

In this post, we saw how to create a treemap to visualize a GNI 2014 dataset using R. Treemaps are a powerful tool for intuitively comparing large amounts of data. Stay tuned to explore more visualization techniques!

Similar Posts