Skip to content
R for the Rest of Us Logo

Mapping with R

Making Dot And Bubble 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(sf)
library(tidyverse)
library(leaflet)
library(leaflegend)

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

portland_drinking_fountains |>
  count(FOUNTAINSTYLE)

pal_discrete <- colorFactor(
  palette = c(
    "#7fc97f",
    "#beaed4",
    "#fdc086",
    "#ffff99",
    "#386cb0",
    "#f0027f"
  ),
  na.color = "gray70",
  domain = portland_drinking_fountains$FOUNTAINSTYLE
)

leaflet() |>
  addProviderTiles("CartoDB.Positron") |>
  addCircles(
    data = portland_drinking_fountains,
    fillColor = ~ pal_discrete(FOUNTAINSTYLE),
    stroke = FALSE,
    radius = 50,
    fillOpacity = 1
  ) |>
  addLegend(
    position = "topright",
    pal = pal_discrete,
    values = portland_drinking_fountains$FOUNTAINSTYLE
  )

oregon_places <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/oregon_places.geojson") |>
  st_transform(crs = 4326)

leaflet() |>
  addProviderTiles("CartoDB.Positron") |>
  addCircles(
    data = oregon_places,
    label = ~name,
    stroke = FALSE,
    fillOpacity = 0.5,
    radius = ~ population / 75
  ) |>
  addLegendSize(
    values = c(0, 500000),
    color = "blue",
    shape = "circle",
    opacity = .5,
    breaks = 5
  )

Your Turn

Use this code to import centroids (i.e. one point in the center of each country):

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

Make a bubble map, with the size of each bubble relative to the size of the refugee population from that country.

Learn More

To learn about the {leaflegend} package used for the legend in this lesson, 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.