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)
library(tidytuesdayR) library(showtext)
library(glue) library(ggtext) library(treemapify) # load data tuesdata <- tidytuesdayR::tt_load(2024, week = 6)
## ## Downloading file 1 of 1: `heritage.csv`
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;'></span>") mastodon <- glue("<span style='color:{col2};font-family:fa-brands;'></span>") link <- glue("<span style='color:{col2};font-family:fa-solid;'></span>") data <- glue("<span style='color:{col2};font-family:fa-solid;'></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)
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
- Hester, Jim, and Jennifer Bryan. 2022. Glue: Interpreted String Literals. https://CRAN.R-project.org/package=glue.
- Hughes, Ellis. 2022. tidytuesdayR: Access the Weekly ’TidyTuesday’ Project Dataset. https://CRAN.R-project.org/package=tidytuesdayR.
- Qiu, Yixuan. 2023. Showtext: Using Fonts More Easily in r Graphs. https://CRAN.R-project.org/package=showtext.
- Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
- Wilke, Claus O., and Brenton M. Wiernik. 2022. Ggtext: Improved Text Rendering Support for ’Ggplot2’. https://CRAN.R-project.org/package=ggtext.
- Wilkins D. 2021 treemapify: Draw Treemaps in 'ggplot2'. https://CRAN.R-project.org/package=treemapify.