Skip to content
Coming soon: Ally. Your guide to the world of AI and R. Learn More →
R for the Rest of Us Logo

Mapping with R

Simple Features Data

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(pillar.sigfig = 7)

library(sf)
library(tidyverse)

wyoming <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/wyoming.geojson")

wyoming

ggplot() +
  geom_sf(data = wyoming)

wyoming_one_ev_station <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/wyoming-one-ev-station.geojson")

wyoming_one_ev_station

ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_one_ev_station)

wyoming_highway_30 <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/wyoming-highway-30.geojson")

wyoming_highway_30

ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_highway_30)

rhode_island <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/rhode-island.geojson")

rhode_island

ggplot() +
  geom_sf(data = rhode_island)

wyoming_all_ev_stations <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/wyoming-all-ev-stations.geojson")

wyoming_all_ev_stations

ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_all_ev_stations)

wyoming_roads <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/wyoming-roads.geojson")

wyoming_roads

ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_roads)

wyoming

rhode_island

rhode_island_bounding_box <-
  rhode_island |>
  st_bbox() |>
  st_as_sfc() |>
  st_sf() |>
  rename(geometry = st_as_sfc.st_bbox.rhode_island..)

ggplot() +
  geom_sf(data = rhode_island) +
  geom_sf(
    data = rhode_island_bounding_box,
    color = "red",
    linewidth = 1,
    fill = NA
  )

wyoming

wyoming_different_projection <-
  wyoming |>
  st_transform(5070)

wyoming_different_projection

wyoming |>
  ggplot() +
  geom_sf()

wyoming_different_projection |>
  ggplot() +
  geom_sf()

wyoming

wyoming_coordinates <-
  wyoming |>
  st_coordinates() |>
  as_tibble() |>
  rename(longitude = X, latitude = Y) |>
  select(longitude, latitude)

wyoming_coordinates

wyoming_coordinates |>
  ggplot(
    aes(
      x = longitude,
      y = latitude
    )
  ) +
  geom_point()

Your Turn

Run the following code in order to import an sf object called africa.

library(sf)

africa <-
  read_sf(
    "https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/africa.geojson"
  )

Examine your object, ensuring you can identify its geometry type, dimensions, bounding box, coordinate reference system and geometry column.

If you want to, try making a static map with the geom_sf() function from ggplot and/or an interactive map with the mapview() function from the {mapview} package.

Learn More

My book, R for the Rest of Us: A Statistics-Free Introduction, has a chapter on mapping that you may find interesting. If you want to go deep on simple features data, this vignette from the {sf} package is for you.

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

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

Jesco Brown

Jesco Brown • September 28, 2025

When exploring the africa dataset, i did not have to rename the column to geometry as the lesson did in the wyoming example. why?
also what does the st_ in all the functions stand for?

David Keyes

David Keyes Founder • September 29, 2025

You don't have to rename the geometry column, as it is already called that. For the Wyoming example, where exactly are you talking about when I rename the column?

On the question about st_, I'm not sure if it stands for anything, but it is a prefix that tells you all of the functions are from the {sf} package.

Amal Dahounto

Amal Dahounto • October 23, 2025

Could you please provide me with the full PDF of the book at chapter on mapping? Thanks in advance