Customizing {geofacet} for plotting geographically faceted graphs
Exploring how faceted plots cn be re-arragned to roughly follow the geographical location of sub-units.
Maps
{geofacet}
Geocomputation
Facets
India
Haryana
Author
Aditya Dahiya
Published
January 18, 2025
Part 1: Working with the {geofacet} package
The geofacet package provides an innovative approach to visualizing geographically structured data by arranging small multiples (facets) in a grid that mirrors the spatial arrangement of regions on a map. This facilitates comparison across regions while retaining geographical context. The package includes built-in grids for various regions, such as state_grid1 for the United States, but users can also create custom grids using grid_preview() to visualize the structure. Integration with ggplot2 allows for seamless creation of geofaceted visualizations, making it possible to apply any ggplot-compatible data. The package is highly customizable, with functions like facet_geo() providing flexibility to specify grids and scales. Additionally, geofacet supports data from other countries, such as Japan and Australia, with pre-built grids available for quick exploration. For more advanced applications, users can define their own layouts using add_custom_grid(). This makes geofacet a powerful tool for researchers and analysts working with spatially contextual data.
Install package, load required libraries
Code
# install.packages("geofacet")library(tidyverse) # Data Wranglinglibrary(sf) # Simple Features in Rlibrary(geofacet) # Geographic facetinglibrary(patchwork) # Compiling Plotslibrary(gt) # Great Tables in R # Setting themes etc. ----------------------------------sysfonts::font_add_google("Saira Condensed", "body_font")showtext::showtext_auto()theme_set(theme_minimal(base_family ="body_font",base_size =14,base_line_size =0.2 ) +theme(text =element_text(colour ="grey20" ),plot.title =element_text(size =24,margin =margin(0.1, 0.1, 0.1, 0.1, "lines") ),plot.subtitle =element_text(size =16,margin =margin(0.1, 0.1, 0.1, 0.1, "lines"),lineheight =0.3 ) ))
Exploring the package with an example
This code demonstrates how to use the geofacet package to create geofaceted bar plots shown in Figure 1, of state ranks for U.S. states, utilizing two different in-built grid layouts: us_state_grid1 and us_state_grid2. It showcases comparison across layouts by combining plots with patchwork, adding annotations, and saving the final visualization as an image.
Part 2: An Example submitting a custom Grid to {geofacet}
About the 22 districts in the State of Haryana
This code creates a detailed summary table of the 22 districts of Haryana, including their names, codes, and approximate centroid locations (latitude and longitude). Using the tibble package, it structures the data, and the gt package formats it into an interactive table with customized column labels and number formatting, shown in Table 1.
This code creates a custom geofacet grid for visualizing Haryana’s districts in a spatially relevant layout. It defines the grid’s structure using a data frame, previews the layout with grid_preview(), and customizes the appearance with titles and themes. Finally, the grid is saved as an image, shown in Figure 2.
This code creates an interactive table summarizing the percentage area of each zone, categorized by proximity to health-care facilities, across districts in Haryana. For how this data was calculated, and the compete project, check out this webpage. It uses tidyverse for data manipulation, reshapes the dataset with pivot_wider(), and formats the table using gt() with an interactive display and customized headers, shown in Table 2 .
This code demonstrates how to visualize the percentage of Haryana’s district areas within certain distance zones from health-care facilities using both standard facet_wrap() and customized facet_geo() layouts. The plots use stacked polar bar charts for comparison, with a custom geofacet layout reflecting Haryana’s spatial arrangement. The combined plots are styled, annotated, and saved as a PNG file for further use, shown in Figure 3 .