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

Making Interactive Maps with {leaflet}

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
library(leaflet)
library(tidyverse)
library(tidycensus)
library(janitor)
library(tigris)
library(scales)
library(sf)

leaflet() |>
  addTiles() |>
  addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")

leaflet() |>
  addProviderTiles("CartoDB.Positron") |>
  addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")

leaflet() |>
  addProviderTiles("Stadia.StamenToner") |>
  addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")

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_wgs84 <-
  speak_language_other_than_english |>
  st_transform(crs = 4326)

leaflet() |>
  addTiles() |>
  addPolygons(data = speak_language_other_than_english_wgs84)

leaflet() |>
  addTiles() |>
  addPolygons(
    data = speak_language_other_than_english_wgs84,
    weight = 0
  )

leaflet() |>
  addTiles() |>
  addPolygons(
    data = speak_language_other_than_english_wgs84,
    weight = 1,
    color = "red"
  )

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

leaflet() |>
  addProviderTiles("CartoDB.Positron") |>
  addCircles(data = portland_drinking_fountains)

library(tigris)

multnomah_county_roads <-
  roads(state = "OR", county = "Multnomah") |>
  st_transform(4326)

leaflet() |>
  addProviderTiles("CartoDB.Positron") |>
  addPolylines(
    data = multnomah_county_roads,
    weight = 0.5
  )

leaflet() |>
  addProviderTiles("CartoDB.Positron", group = "Map") |>
  addProviderTiles("Esri.WorldImagery", group = "Satellite") |>
  addPolylines(
    data = multnomah_county_roads,
    weight = 0.5,
    color = "purple",
    group = "Roads"
  ) |>
  addCircles(
    data = portland_drinking_fountains,
    color = "green",
    group = "Drinking Fountains"
  ) |>
  addLayersControl(
    baseGroups = c(
      "Map",
      "Satellite"
    ),
    overlayGroups = c(
      "Roads",
      "Drinking Fountains"
    )
  ) |>
  hideGroup("Roads")

Your Turn

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

  2. Map this using {leaflet}. All countries will be the same color at this point (we’ll change that in the next lesson).

Learn More

To learn about {leaflet}, check out its documentation website.

There is also a vignette on basemaps. To learn about the various basemaps you can use, check out this interactive preview of them.

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

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

Amal Dahounto

Amal Dahounto • October 30, 2025

Hi David, it looks like the video shown for lesson1 "Making Interactive Maps with {leaflet} / Interactive Maps" is not the expected one. The code shown in this video doesn't match the one in the "View code shown in video" section. Could you please take a look and verify? Thanks in advance. Amal

David Keyes

David Keyes Founder • October 30, 2025

Sorry about that! Should be fixed now!

Abigail Todhunter-Reid

Abigail Todhunter-Reid • November 5, 2025

Hi David, the video and transcript for the "Making Interactive Maps with {leaflet}" is not showing up. I am just getting a blank screen and transcript.

David Keyes

David Keyes Founder • November 6, 2025

Sorry, should be fixed now!

Annie Borland

Annie Borland • January 28, 2026

Hi, I'm getting an error that this video is not currently available - all other videos seem to be working

David Keyes

David Keyes Founder • January 28, 2026

Sorry! Just fixed it.