Skip to content
R for the Rest of Us Logo

Mapping with R

Mapping with ggplot

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.

View code shown in video
options(tigris_use_cache = TRUE)

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

load_variables(
  year = 2023,
  dataset = "acs5/subject",
  cache = TRUE
) |>
  filter(str_detect(name, "S1601"))

get_acs(
  geography = "county",
  variable = "S1601_C01_003",
  summary_var = "S1601_C01_001"
)

speak_language_other_than_english <-
  get_acs(
    geography = "county",
    variable = "S1601_C01_003",
    summary_var = "S1601_C01_001",
    geometry = TRUE
  ) |>
  clean_names() |>
  mutate(pct = estimate / summary_est) |>
  select(name, pct)

speak_language_other_than_english

speak_language_other_than_english |>
  ggplot() +
  geom_sf()

library(tigris)

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

speak_language_other_than_english |>
  shift_geometry() |>
  ggplot() +
  geom_sf(aes(fill = pct))

Your Turn

  1. Import a dataset I’ve created on the number of refugees from each country in the world

  2. Use ggplot to make a map of this data, with countries sending higher numbers of refugees in a different color than countries with lower numbers of refugees

Learn More

Although I only show geom_sf() for working with polygon data, this function handles any type of sf object. Full details about the function available here and in Chapter 6 of Kyle Walker's book Analyzing US Census Data: Methods, Maps, and Models in R.

If you want to learn more about how to use the {tidycensus} package, I cover it in Chapter 11 of my book, R for the Rest of Us: A Statistics-Free Introduction. Chapter 4 of my book covers making maps.

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

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