Skip to content
R for the Rest of Us Logo

R in 3 Months (Fall 2026)

Week 11 Live Session (Fall 2026)

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

In this week 11 session we covered advanced data viz, starting with how to build a custom ggplot theme function with arguments. We then walked through making maps with {ggplot2} and {sf}, pulling in census and international boundary data, and touched on interactive maps.

You'll learn:

  • How to write a custom theme function that wraps a built-in theme and adds theme() tweaks such as removing axis titles and bolding the plot title
  • How to add arguments to a theme function, such as a chart type, so one theme works for scatterplots and bar charts
  • How to write a scale function that applies brand colors with scale_color_manual
  • How to read geospatial data with read_sf() and plot it with geom_sf(), including layering polygons, lines and points
  • How to pull in boundary and census data directly with {tigris}, {tidycensus} and {rnaturalearth}, and join your own data to geospatial data

Resources mentioned:

Key moments:

  • 03:55 Housekeeping and overview of the final project
  • 09:54 Quiz on why group is needed in line charts
  • 13:42 Examples of organizational themes like {bbplot}
  • 15:57 Building a custom theme function
  • 20:54 Adding a hide axis titles argument to the theme
  • 27:16 Switching to a chart type argument for scatterplots and bar charts
  • 30:40 Custom brand color scale function
  • 35:20 Reading and plotting geospatial data with {sf}
  • 44:59 Layering boundaries, routes and signals in one map
  • 46:23 Getting state and county maps with {tigris}
  • 49:08 Participant question: California zip code maps and joining your own data
  • 52:49 Census median income map with {tidycensus}
  • 56:31 Interactive maps with {mapgl}
View code shown in video

Theme code

library(tidyverse)
library(janitor)
library(sf)

# Portland ----------------------------------------------------------------

portland_boundaries <-
  read_sf("data-raw/City_Boundaries.geojson") |>
  clean_names() |>
  filter(cityname == "Portland")

portland_boundaries

portland_boundaries |>
  ggplot() +
  geom_sf() +
  theme_void()

traffic_signals <-
  read_sf("data-raw/Traffic_Signals.geojson") |>
  clean_names()

traffic_signals

traffic_signals |>
  ggplot() +
  geom_sf()

snow_and_ice_routes <-
  read_sf("data-raw/Snow_and_Ice_Routes.geojson") |>
  clean_names()

snow_and_ice_routes

snow_and_ice_routes |>
  ggplot() +
  geom_sf(aes(color = priority))

ggplot() +
  geom_sf(
    data = portland_boundaries,
    fill = "gray80",
    alpha = 0.5
  ) +
  geom_sf(
    data = snow_and_ice_routes,
    alpha = 0.5
  ) +
  geom_sf(
    data = traffic_signals,
    aes(color = software_type),
    alpha = 0.5,
    size = 1
  ) +
  theme_void()

# Tigris ------------------------------------------------------------------

library(tigris)

us_states <- states()

us_states

us_states |>
  ggplot() +
  geom_sf()

us_states |>
  shift_geometry() |>
  ggplot() +
  geom_sf()

kentucky_counties <- counties(state = "Kentucky")

kentucky_counties

kentucky_counties |>
  ggplot() +
  geom_sf()

california_zip_codes <- zctas(state = "California", year = 2010)

california_zip_codes |>
  ggplot() +
  geom_sf()

read_sf(
  "data-raw/USPS_ZIP_Code_Boundaries_Product.gdb"
) |>
  ggplot() +
  geom_sf()

# Median Income -----------------------------------------------------------

library(tidycensus)
library(scales)

median_income <-
  get_acs(
    state = "Illinois",
    geography = "tract",
    variables = "B19013_001",
    geometry = TRUE
  )

median_income

median_income |>
  ggplot(aes(fill = estimate)) +
  geom_sf()


# Check-in break ----------------------------------------------------------

# International Data ------------------------------------------------------

library(rnaturalearth)

ukraine <-
  ne_download(
    scale = 10,
    type = "countries"
  ) |>
  clean_names() |>
  filter(admin == "Ukraine") |>
  select(admin)

ukraine |>
  ggplot() +
  geom_sf()

ukraine_de_jure <-
  ne_download(
    scale = 10,
    type = "admin_0_countries_ukr"
  ) |>
  clean_names() |>
  filter(admin == "Ukraine") |>
  select(admin)

ukraine_de_jure |>
  ggplot() +
  geom_sf()


# mapgl -----------------------------------------------------------------

library(mapgl)

maplibre(bounds = ukraine) |>
  add_fill_layer(
    id = "ukraine",
    source = ukraine,
    fill_color = "orange",
    fill_opacity = 0.5,
    fill_outline_color = "white"
  )

maplibre(bounds = ukraine_de_jure) |>
  add_fill_layer(
    id = "ukraine_de_jure",
    source = ukraine_de_jure,
    fill_color = "orange",
    fill_opacity = 0.5,
    fill_outline_color = "white"
  )

Mapping code

library(tidyverse)
library(janitor)
library(sf)

# Portland ----------------------------------------------------------------

portland_boundaries <-
  read_sf("data-raw/City_Boundaries.geojson") |>
  clean_names() |>
  filter(cityname == "Portland")

portland_boundaries

portland_boundaries |>
  ggplot() +
  geom_sf() +
  theme_void()

traffic_signals <-
  read_sf("data-raw/Traffic_Signals.geojson") |>
  clean_names()

traffic_signals

traffic_signals |>
  ggplot() +
  geom_sf()

snow_and_ice_routes <-
  read_sf("data-raw/Snow_and_Ice_Routes.geojson") |>
  clean_names()

snow_and_ice_routes |>
  select(priority)

snow_and_ice_routes |>
  ggplot() +
  geom_sf(aes(color = priority))

ggplot() +
  geom_sf(
    data = portland_boundaries,
    fill = "gray80",
    alpha = 0.5
  ) +
  geom_sf(
    data = snow_and_ice_routes,
    alpha = 0.5
  ) +
  geom_sf(
    data = traffic_signals,
    aes(color = software_type),
    alpha = 0.5,
    size = 1
  ) +
  theme_dk(hide_gridlines = TRUE, hide_legend = FALSE) +
  theme(axis.text = element_blank())


# Tigris ------------------------------------------------------------------

library(tigris)

us_states <- states()

us_states

us_states |>
  shift_geometry() |>
  ggplot() +
  geom_sf()

kentucky_counties <- counties(state = "Kentucky")

kentucky_counties

kentucky_counties |>
  ggplot() +
  geom_sf()

# Median Income -----------------------------------------------------------

library(tidycensus)
library(scales)

median_income <-
  get_acs(
    state = "Illinois",
    geography = "county",
    variables = "B19013_001",
    geometry = TRUE
  )

median_income

median_income |>
  ggplot(aes(fill = estimate)) +
  geom_sf()


# Check-in break ----------------------------------------------------------

# International Data ------------------------------------------------------

library(rnaturalearth)

ne_countries()

ukraine <-
  ne_states(
    country = c("Ukraine")
  )

ukraine |>
  ggplot() +
  geom_sf()


# Mapview -----------------------------------------------------------------

library(mapview)

mapview(ukraine)


# Interactive -------------------------------------------------------------

library(ggiraph)

median_income_interactive_plot <-
  median_income |>
  mutate(estimate_formatted = dollar(estimate)) |>
  mutate(tooltip_text = str_glue("{NAME} {estimate_formatted}")) |>
  ggplot(aes(
    fill = estimate,
    tooltip = tooltip_text
  )) +
  geom_sf_interactive()

girafe(ggobj = median_income_interactive_plot)

Interactive data viz code

---
title: "Interactive Data Viz Example"
format: html
execute: 
  warning: false
  message: false
  echo: false
editor_options: 
  chunk_output_type: console
---

```{r}
library(tidyverse)
library(scales)
library(ggiraph)
```


```{r}
cbem <-
  read_csv(here::here("data-raw/cbem.csv"))
```

## Static Plot

```{r}
cbem |>
  filter(location == "Oregon") |>
  filter(age_group == "Under 18") |>
  filter(group != "All Persons") |>
  mutate(
    group = fct(
      group,
      levels = c(
        "American Indian or Alaska Native",
        "Asian or Pacific Islander",
        "Black or African American",
        "White",
        "Hispanic or Latino"
      )
    )
  ) |>
  mutate(x_position = row_number()) |>
  mutate(
    x_position = case_when(
      group == "Hispanic or Latino" ~ 5.5,
      .default = x_position
    )
  ) |>
  mutate(percent_formatted = percent(percent)) |>
  ggplot(
    aes(
      x = x_position,
      y = percent,
      fill = group,
      label = percent_formatted
    )
  ) +
  geom_col() +
  geom_text(
    vjust = 1.5,
    color = "white"
  ) +
  scale_fill_manual(
    values = c(
      "American Indian or Alaska Native" = "#9CC892",
      "Asian or Pacific Islander" = "#0066cc",
      "Black or African American" = "#477A3E",
      "White" = "#6CC5E9",
      "Hispanic or Latino" = "#ff7400"
    )
  ) +
  theme_void() +
  theme(legend.position = "none")
```


## Interactive Plot


```{r}
cbem_plot <-
  cbem |>
  filter(location == "Oregon") |>
  filter(age_group == "Under 18") |>
  filter(group != "All Persons") |>
  mutate(
    group = fct(
      group,
      levels = c(
        "American Indian or Alaska Native",
        "Asian or Pacific Islander",
        "Black or African American",
        "White",
        "Hispanic or Latino"
      )
    )
  ) |>
  mutate(x_position = row_number()) |>
  mutate(
    x_position = case_when(
      group == "Hispanic or Latino" ~ 5.5,
      .default = x_position
    )
  ) |>
  mutate(percent_formatted = percent(percent)) |>
  ggplot(
    aes(
      x = x_position,
      y = percent,
      fill = group,
      label = percent_formatted,
      tooltip = percent_formatted
    )
  ) +
  geom_col_interactive() +
  # geom_text(
  #   vjust = 1.5,
  #   color = "white"
  # ) +
  scale_fill_manual(
    values = c(
      "American Indian or Alaska Native" = "#9CC892",
      "Asian or Pacific Islander" = "#0066cc",
      "Black or African American" = "#477A3E",
      "White" = "#6CC5E9",
      "Hispanic or Latino" = "#ff7400"
    )
  ) +
  theme_void() +
  theme(legend.position = "none")

girafe(ggobj = cbem_plot)
```

Learn More

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

143 Lessons

New from Ally Debug R error messages