Skip to content
R for the Rest of Us Logo

Mapping with R

Making Interactive Maps with {mapgl}

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(tidyverse)
library(sf)
library(mapgl)

mapboxgl(style = mapbox_style("light"))

mapboxgl(style = mapbox_style("dark"))

mapboxgl(style = mapbox_style("outdoors"))

maplibre()

maplibre(style = carto_style("positron"))

maplibre(style = carto_style("dark-matter"))

mapboxgl(
  mapbox_style("light"),
  center = c(-43.23412, -22.91370)
)

mapboxgl(
  mapbox_style("light"),
  center = c(-43.23412, -22.91370),
  zoom = 18
)

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

mapboxgl(
  mapbox_style("light"),
  center = c(-100.29957845401096, 38.93484429077689),
  zoom = 2
) |>
  add_fill_layer(
    id = "speak_language_other_than_english_wgs84_with_labels",
    source = speak_language_other_than_english_wgs84_with_labels
  )

mapboxgl(
  mapbox_style("light"),
  center = c(-100.29957845401096, 38.93484429077689),
  zoom = 2
) |>
  add_fill_layer(
    id = "speak_language_other_than_english_wgs84_with_labels",
    source = speak_language_other_than_english_wgs84_with_labels,
    fill_color = interpolate(
      column = "pct",
      values = c(0, 1),
      stops = c("lightblue", "darkblue"),
      na_color = "lightgrey"
    ),
    fill_opacity = 0.75
  )

mapboxgl(
  mapbox_style("light"),
  center = c(-100.29957845401096, 38.93484429077689),
  zoom = 2
) |>
  add_fill_layer(
    id = "speak_language_other_than_english_wgs84_with_labels",
    source = speak_language_other_than_english_wgs84_with_labels,
    fill_color = interpolate(
      column = "pct",
      values = c(0, 1),
      stops = c("lightblue", "darkblue"),
      na_color = "lightgrey"
    ),
    popup = "text_label",
    fill_opacity = 1
  )

mapboxgl() |>
  fly_to(
    center = c(2.1228412549747344, 41.3812461533571),
    zoom = 16,
    pitch = 25
  )

Your Turn

Turn any of the interactive refugee maps you have made into an interactive map with {mapgl}

Learn More

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

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

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