Skip to content
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.