Mapping Harvard Yard & Campus Buildings with ggplot2
Using {ggplot2}, {ggmap} and {osmdata} to plot a map of Harvard Yard and nearby academic / residential buildings.
Geocomputation
{osmdata}
Open Street Maps
Harvard
{ggmap}
Author
Aditya Dahiya
Published
March 23, 2025
This code demonstrates a comprehensive workflow for visualizing geographic data, specifically mapping Harvard University’s campus buildings. It integrates multiple packages for data wrangling (tidyverse), spatial data manipulation (sf), and raster processing (terra). The visualization setup includes custom font styling (showtext), color customization (colorspace), and social media captioning using HTML-like text (ggtext). A bounding box is defined for Harvard Yard, and building footprints are retrieved from OpenStreetMap via osmdata. Custom functions process and classify buildings into academic and dormitory types. Base maps are acquired from Stadia Maps (ggmap), masked and cropped using tidyterra. The final ggplot2 visualization overlays raster imagery, labels buildings with text-repulsion (ggrepel), and applies a minimalist map theme (ggthemes).
Figure 1: A detailed map of Harvard University’s campus, created using R and OpenStreetMap data. This visualization highlights key buildings, streets, and landmarks with custom styling for clarity and readability.
Loading required libraries, data import & creating custom functions.
Code
# Data Wrangling & Plotting Toolslibrary(tidyverse) # All things tidylibrary(sf) # Simple Features in R# 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 colours# Getting geographic data library(ggmap) # Getting raster mapslibrary(terra) # Cropping / Masking rasterslibrary(tidyterra) # Rasters with ggplot2library(osmdata) # Open Street Maps data
Visualization Parameters
Code
# Font for titlesfont_add_google("Solway",family ="title_font") # Font for the captionfont_add_google("Stint Ultra Condensed",family ="caption_font") # Font for plot textfont_add_google("Glegoo",family ="body_font") showtext_auto()mypal <-c("#A41034", "#68666F", "grey10", "white")# A base Colourbg_col <- mypal[4]seecolor::print_color(bg_col)# Colour for highlighted texttext_hil <- mypal[1]seecolor::print_color(text_hil)# Colour for the texttext_col <- mypal[3]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 & Stadia Maps", " | **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 <-"Harvard University: Halls & Homes"plot_subtitle <-"Harvard Yard and its surrounding areas, which form the heart of the campus. The colours distinguish academic buildings and residential dorms."
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "viz_harvard_yard1.png")) |>image_resize(geometry ="x400") |>image_write( here::here("data_vizs", "thumbnails", "viz_harvard_yard.png" ) )
Session Info
Code
# Data Wrangling & Plotting Toolslibrary(tidyverse) # All things tidylibrary(sf) # Simple Features in R# 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 colours# Getting geographic data library(ggmap) # Getting raster mapslibrary(terra) # Cropping / Masking rasterslibrary(tidyterra) # Rasters with ggplot2library(osmdata) # Open Street Maps datasessioninfo::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