Analysis of ISO country codes reveals geographic and alphabetic patterns in international standardization. Most nations maintain three distinct identifiers.
#TidyTuesday
Author
Steven Ponce
Published
November 9, 2024
Steps to Create this Graphic
1. Load Packages & Setup
Show code
## 1. LOAD PACKAGES & SETUP ----if (!require("pacman")) install.packages("pacman")pacman::p_load( tidyverse, # Easily Install and Load the 'Tidyverse' ggtext, # Improved Text Rendering Support for 'ggplot2' showtext, # Using Fonts More Easily in R Graphs janitor, # Simple Tools for Examining and Cleaning Dirty Data scales, # Scale Functions for Visualization glue, # Interpreted String Literals here, # A Simpler Way to Find Your Files ggridges, # Ridgeline Plots in 'ggplot2' waffle, # Create Waffle Chart Visualizations patchwork # The Composer of Plots)### |- figure size ----camcorder::gg_record(dir = here::here("temp_plots"),device ="png",width =10,height =12,units ="in",dpi =320)### |- resolution ----showtext_opts(dpi =320, regular.wt =300, bold.wt =800)
### |- ridge plot ----ridge_plot <- ridge_data |>ggplot(aes(x = numeric, y = first_letter, fill =after_stat(x))) +# Geoms# add reference linesgeom_vline(xintercept =seq(0, 1000, 200),color ="gray90",linetype ="dashed" ) +# ridgesgeom_density_ridges_gradient(alpha =0.8,scale =0.95,rel_min_height =0.005,bandwidth =25,color = text_col,show.legend =TRUE ) +# country positionsgeom_point(data = ridge_data,aes(x = numeric, y = first_letter),size =0.8,alpha =0.3,color = text_col ) +# annotationsannotate("text",x =10,y ="E",label ="Early alphabet countries\noften have lower codes",size =3.0,family ="text",color ="gray25",lineheight =0.9,hjust =0.4 ) +annotate("text",x =850,y ="W",label ="Higher codes cluster in\nlater alphabet regions",size =3.0,family ="text",color ="gray25",lineheight =0.9,vjust =0.5 ) +# Scalesscale_x_continuous(breaks =seq(0, 1000, 200),expand =c(0.02, 0) ) +scale_y_discrete() +scale_fill_gradientn(colors = col_palette,name ="Numeric Code Range",guide =guide_colorbar(title.position ="top",barwidth =unit(15, "lines"),barheight =unit(0.5, "lines") ) ) +coord_cartesian(clip ="off") +# Labslabs(x ="Numeric Country Code (0-999)",y ="First Letter of Alpha-2 Code" ) +# Themetheme(legend.position ="top",legend.justification ="right",legend.direction ="horizontal",legend.title =element_text(size =rel(0.8)),legend.text =element_text(size =rel(0.7)),legend.key.width =unit(2.2, "cm"),legend.key.height =unit(0.3, "cm"),legend.spacing.x =unit(0.2, "cm"),plot.margin =margin(-5, 30, 5, 30),panel.spacing =unit(2, "lines") )### |- waffle plot ----waffle_plot <- waffle_data |>ggplot(aes(fill = key, values = value)) +# Geomgeom_waffle(n_rows =5,size =0.5,colour ="white",flip =TRUE,radius =unit(2, "pt") ) +# Scalesscale_fill_manual(values = col_palette[c(1, 5)]) +coord_equal(ratio =1) +# Themetheme_void() +theme(legend.position ="bottom",legend.title =element_blank(),legend.text =element_text(size =rel(1)),plot.margin =margin(10, 5, 0, 5),plot.background =element_rect(fill = bkg_col, color = bkg_col),panel.background =element_rect(fill = bkg_col, color = bkg_col), )### |- title plot ----# Add total count annotationn_countries <-nrow(countries)title_plot <-ggplot() +# Geomsannotate("text",x =0, y =0.85,label ="The Architecture of\nGlobal Country Codes",hjust =0,size =10,lineheight =1,fontface ="bold",family ="title",color = title_col ) +annotate("text",x =0, y =0.5,label =str_wrap("Analysis of ISO country codes reveals geographic and alphabetic patterns in international standardization. Most nations maintain three distinct identifiers.",width =65 ),hjust =0,size =3.5,color = title_col,family ="text" ) +annotate("text",x =0, y =0.3,label =glue("Analysis of {n_countries} country codes"),hjust =0,size =3.5,family ="text",color = title_col ) +annotate("text",x =0, y =0.1,label ="Example:\nUS (Alpha-2)\nUSA (Alpha-3)\n840 (Numeric)",hjust =0,size =2.8,color = title_col,family ="text",lineheight =1.2 ) +# Scalesscale_x_continuous(limits =c(0, 1)) +scale_y_continuous(limits =c(0, 1)) +# Themtheme_void() +theme(plot.background =element_rect(fill = bkg_col, color = bkg_col),panel.background =element_rect(fill = bkg_col, color = bkg_col),plot.margin =margin(5, 10, 0, 10) )### |- explanatory text plot for the waffle chart ----waffle_explanation <-ggplot() +# Geomsannotate("text",x =0, y =0.85,label ="Country Code Systems\n",hjust =0, size =3.5, fontface ="bold", family ="text" ) +annotate("text",x =0, y =0.55,label =str_wrap("Each square represents 10 countries. A complete system means a country has all three standardized codes shown in the ridge plot below: Alpha-2 (e.g., US), Alpha-3 (USA), and Numeric (840).", 55),hjust =0, size =2.8, color = title_col, family ="text" ) +# Scalesscale_x_continuous(limits =c(0, 1)) +scale_y_continuous(limits =c(0, 1)) +# Themetheme_void() +theme(plot.background =element_rect(fill = bkg_col, color = bkg_col),panel.background =element_rect(fill = bkg_col, color = bkg_col),plot.margin =margin(5, 10, 0, 10) )### |- combined plot ----# define layout designdesign <-c(area(1, 1, 2, 4), # title_plotarea(1, 5, 1, 6), # waffle_plotarea(2, 5, 2, 6), # waffle_explanationarea(3, 1, 5, 6) # ridge_plot)combined_plot <- title_plot + waffle_plot + waffle_explanation + ridge_plot +plot_layout(design = design,heights =c(1.2, 1, 2, 1, 1), # Simplified heightswidths =c(1, 1, -0.4, 1, 0.9, 0.9) # Slightly wider right side ) +plot_annotation(caption = caption_text,theme =theme(plot.background =element_rect(fill = bkg_col, color = bkg_col),panel.background =element_rect(fill = bkg_col, color = bkg_col),plot.margin =margin(10, 10, 10, 10),plot.caption =element_markdown(size =rel(0.60),family ="caption",color =alpha(caption_col, 0.9),lineheight =0.65,hjust =0.5,margin =margin(t =10, b =5) ) ) )
7. Save
Show code
### |- plot image ----library(ggplotify)library(grid)# Convert patchwork plot to grob# There was some issues between patchwork and ggsaveplot_grob <-as.grob(combined_plot)# Activate showtext manuallyshowtext_begin()showtext_opts(dpi =320, regular.wt =300, bold.wt =800)# Save the plot as PNGinvisible({png(filename = here::here("data_visualizations/TidyTuesday/2024/tt_2024_46.png"),width =10, height =12, units ="in", res =320 )grid.draw(plot_grob)dev.off()})# Deactivate showtextshowtext_end()### |- plot thumbnail----magick::image_read(here::here("data_visualizations/TidyTuesday/2024/tt_2024_46.png")) |> magick::image_resize(geometry ="400") |> magick::image_write(here::here("data_visualizations/TidyTuesday/2024/thumbnails/tt_2024_46.png"))