A cartogram of World Map that distorts the size of each country based on its population, with larger areas representing more populous nations.
#TidyTuesday
Maps
{cartogram}
Author
Aditya Dahiya
Published
October 22, 2024
The dataset for this analysis comes from the CIA World Factbook, specifically the 2014 Country Comparisons section. This comprehensive resource provides essential intelligence on various global aspects, including demographics, government, economics, and military statistics across 265 world entities. The data has been accessed via the {openintro} R package and the {usdatasets} R package, with additional insights shared through this LinkedIn post.
Key variables in the dataset include country names, population figures, birth and death rates, life expectancy, maternal and infant mortality rates, internet users, migration statistics, and more. These metrics provide a well-rounded view of each country’s demographic and social health indicators. No further data cleaning was necessary as the dataset was already well-prepared for analysis.
How I made this graphic?
Loading required libraries, data import & creating custom functions.
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidylibrary(janitor) # Cleaning names etc.library(here) # Root Directory Management# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Combining plotslibrary(cartogram) # Cartogram maps in Rlibrary(sf) # Handling sf objectscia_factbook <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-10-22/cia_factbook.csv')
Visualization Parameters
Code
# Font for titlesfont_add_google("Offside",family ="title_font") # Font for the captionfont_add_google("Saira Extra Condensed",family ="caption_font") # Font for plot textfont_add_google("Smooch Sans",family ="body_font") showtext_auto()# Background Colourbg_col <-"white"# Colour for the text (highlighted)text_hil <-"#00778BFF"# Colour for texttext_col <-"#211747FF"# Define Base Text Sizebts <-90# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""xtwitter_username <-"@adityadahiyaias"social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username} </span>")social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")plot_title <-"A Cartogram of the countries' population\n and Population Density"plot_caption <-paste0("**Data:** CIA World Factbook, {openintro}", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )subtitle_text <-"Countries resized to reflect their population, with vibrant shades depicting population density (persons per square kilometer) -- revealing the dominance of Asia and Africa."plot_subtitle <-str_wrap(subtitle_text, width =95)plot_subtitle |>str_view()rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "tidy_cia_factbook.png")) |>image_resize(geometry ="400") |>image_write( here::here("data_vizs", "thumbnails", "tidy_cia_factbook.png" ) )
Session Info
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidylibrary(here) # Root Directory Management# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Combining plotslibrary(ggmap) # To get background map tilessessioninfo::session_info()$packages |>as_tibble() |>select(package, version = loadedversion, date, source) |>arrange(package) |>filter(package %in%.packages()) |> janitor::clean_names(case ="title" ) |> gt::gt() |> gt::opt_interactive(use_search =TRUE ) |> gtExtras::gt_theme_espn()