The provided R code creates a treemap visualization illustrating the evolution of UNESCO World Heritage sites by country and year. The code utilizes the tidyverse, tidytuesdayR, showtext, glue, ggtext, and treemapify libraries. After loading data from Tidy Tuesday Week 6, the code configures Font Awesome fonts and defines colors and symbols. The treemap is generated using the heritage dataset, and various formatting options, including color scales and minimal themes, are applied. The resulting plot highlights the distribution of UNESCO World Heritage sites over time for different countries, emphasizing a clean and visually appealing presentation.

The Code

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytuesdayR)
library(showtext)
## Lade nötiges Paket: sysfonts
## Lade nötiges Paket: showtextdb
library(glue)
library(ggtext)
library(treemapify)

# load data
tuesdata <- tidytuesdayR::tt_load(2024, week = 6)
## --- Compiling #TidyTuesday Information for 2024-02-06 ----
## --- There is 1 file available ---
## --- Starting Download ---
## 
## 	Downloading file 1 of 1: `heritage.csv`
## --- Download complete ---
heritage <- tuesdata$heritage

# Add Font Awesome fonts
font_add('fa-reg', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Free-Regular-400.otf')
font_add('fa-brands', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Brands-Regular-400.otf')
font_add('fa-solid', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Free-Solid-900.otf')
showtext_auto()

# define colors
bg <- "white"
col2 <- "black"
den.1 <- "#ffffff" # danish white
nor.1 <- "#00205B" # norwegian blue
swe.1 <- "#FECC02" # swedish yellow
den.2 <- "#C8102E" # danish red
nor.2 <- "#BA0C2F" # norwegian red
swe.2 <- "#006AA7" # swedish blue

# Define symbols for Twitter, Mastodon, Link, and Data
twitter <- glue("<span style='color:{col2};font-family:fa-brands;'>&#xf099;</span>")
mastodon <- glue("<span style='color:{col2};font-family:fa-brands;'>&#xf4f6;</span>")
link <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf0c1;</span>")
data <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf1c0;</span>")
space <- glue("<span style='color:{bg}'>-</span>")
space2 <- glue("<span style='color:{bg}'>--</span>") # Horizontal lines for formatting.

# define text
t <- "<strong>UNESCO World Heritage Sites Evolution: A Treemap Visualization by Country and Year</strong>"
s <- "<strong>Tidy Tuesday Week 6</strong>"
cap <- glue("{twitter}{space2}@web_design_fh{space2} 
	{space2}{mastodon}{space2}@frankhaenel @fosstodon.org{space2}
	{space2}{link}{space}{space2}www.frankhaenel.de{space2}
	{data}{space2}100.datavizproject.com")

# create plot
heritage %>%
  pivot_longer(
    cols = !country,
    names_to = "year",
    values_to = "sites"
  )  %>%
  mutate(label=paste0(country," | ",year, ": ",sites," sites")) %>%
  ggplot(aes(area = sites, fill = country, subgroup=year,label=label),show.legend=FALSE) +
  geom_treemap() +
  geom_treemap_subgroup_border(colour = "white", size = 5) +
  geom_treemap_text(aes(colour = country),
                    place = "centre", size = 14, grow = FALSE) +
  scale_color_manual(values = c(den.1,nor.1,swe.1)) +
  scale_fill_manual(values=c(den.2,nor.2,swe.2)) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(size=20,hjust=0.5),
    plot.subtitle = element_markdown(size=16,hjust=0.5, colour = col2),
    plot.caption = element_markdown(size=12),
    plot.margin = margin(t = 10,  # Top margin
                         r = 10,  # Right margin
                         b = 10,  # Bottom margin
                         l = 10) # Left margin
    ) +
  guides(fill = "none") +
  labs(title = t, subtitle = s, caption = cap)
A treemap visualization representing the evolution of UNESCO World Heritage sites by country and year. Each country is color-coded, and the size of each block corresponds to the number of UNESCO World Heritage sites. The plot provides a clear and engaging overview of the distribution and growth of heritage sites over time.

R Code Documentation - UNESCO World Heritage Sites Treemap

This R code generates a treemap visualization depicting the evolution of UNESCO World Heritage sites by country and year.

Libraries

The following R libraries are used in the code:

  • tidyverse (Wickham et al. 2019): A collection of packages for data manipulation and visualization.
  • tidytuesdayR (Hughes 2022): A package for accessing TidyTuesday datasets.
  • showtext (Qiu 2023): Used for incorporating custom fonts into the plot.
  • glue (Hester and Bryan 2022): Enables the creation of HTML-like text strings.
  • ggtext (Wilke and Wiernik 2022): Allows the use of rich text formatting in ggplot2 plots.
  • treemapify (Wilkins D. 2021): Allows to draw treemaps in ggplot

Data Loading

The code loads data from Tidy Tuesday Week 6 and extracts the heritage dataset.

Symbols and Styling

  • Custom symbols from Font Awesome are used for Twitter, Mastodon, Link, and Data icons.
  • Glue is employed for creating stylized text strings and formatting elements.
  • Horizontal lines and spacing are incorporated for improved visual presentation.

Fonts and Styling

  • Font Awesome fonts (fa-reg, fa-brands, fa-solid) are added for custom styling.
  • showtext is used to automatically load and utilize these custom fonts.
  • Markdown-like styling is applied to titles, subtitles, and captions for enhanced visual appeal.

Plot Creation

The treemap is created using the heritage dataset. Various formatting and theming options are applied.

References