Customzied geographically-oriented facet with {geofacet} in R
Plotting statistics of the districts in Haryana using facet_geo() instead of facet_wrap()
Geocomputation
{geofacet}
Haryana
India
Author
Aditya Dahiya
Published
January 19, 2025
About the Data
Figure 1: Pie-charts depicting the percentage area of each district of Haryana that falls within a particular distance zone from the nearest health-care facility. The charts are arranged in a customized facet pattern using geofacet::facet_geo() to mimic the approximate geographic location of the districts.
How I made this graphic?
Loading required libraries, data import & creating custom functions.
Code
# Data Wrangling & Plotting Toolslibrary(tidyverse) # All things tidylibrary(sf) # Simple Features in Rlibrary(geofacet) # Geographic faceting# Plot touch-up 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) # Compiling Plotslibrary(tidytext) # Getting words analysis in R
Visualization Parameters
Code
# Font for titlesfont_add_google("Marvel",family ="title_font") # Font for the captionfont_add_google("Economica",family ="caption_font") # Font for plot textfont_add_google("Economica",family ="body_font") showtext_auto()mypal <- paletteer::paletteer_d("RColorBrewer::BuPu")# A base Colourbg_col <- mypal[1]seecolor::print_color(bg_col)# Colour for highlighted texttext_hil <- mypal[9]seecolor::print_color(text_hil)# Colour for the texttext_col <- mypal[9]seecolor::print_color(text_col)# 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_caption <-paste0("**Data:** Open Street Maps; Census of India", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)# Add text to plot-------------------------------------------------plot_title <-"Haryana: distances from nearest health-care facility"plot_subtitle <-"Percentage area of each district of Haryana, that falls in a certain distance zone, from its nearest health-facility. The plots are arranged in a custom facet-grid, constructed using geofacet::facet_geo()"
Get custom data: computed using detailed R code here.
g_final <- g +inset_element(p = g_inset,left =0, right =0.3,bottom =0, top =0.3,align_to ="full",clip =FALSE )ggsave(filename = here::here("data_vizs","viz_custom_facet_hy.png" ),plot = g_final,width =400,height =500,units ="mm",bg = bg_col)
Savings the thumbnail for the webpage
Code
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "viz_custom_facet_hy.png")) |>image_resize(geometry ="x400") |>image_write( here::here("data_vizs", "thumbnails", "viz_custom_facet_hy.png" ) )
Session Info
Code
# Data Wrangling & Plotting Toolslibrary(tidyverse) # All things tidylibrary(sf) # Simple Features in Rlibrary(geofacet) # Geographic faceting# Plot touch-up 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) # Compiling Plotslibrary(tidytext) # Getting words analysis in Rsessioninfo::session_info()$packages |>as_tibble() |>select(package, version = loadedversion, date, source) |>arrange(package) |> janitor::clean_names(case ="title" ) |> gt::gt() |> gt::opt_interactive(use_search =TRUE ) |> gtExtras::gt_theme_espn()
Table 1: R Packages and their versions used in the creation of this page and graphics