Making sliding maps

Andrew Irwin, a.irwin@dal.ca

2024-03-21

Plan

  • Kinds of maps (outline vs. tiles; interactive vs. static)

  • Creating basic maps

  • Adding points

  • Shading areas

  • Tile service providers

Map libraries

  • Dynamic: leaflet for drawing raster or tiled maps (leafletjs.com)

  • Static: ggmap (part of tidyverse, tiles from several services)

  • Both rely on internet services such as:

A basic map

m <- leaflet() |>
  addTiles() |>  # Add default OpenStreetMap map tiles
  addMarkers(lng=-63.5932, lat=44.63697, popup="Math & Stats, Dalhousie U")

A basic map

Adding points

cities <- read_csv("static/L28/selected_cities.csv")
head(cities) |> kable() |> kable_styling()
city city_ascii lat lng country iso2 iso3 admin_name capital population id
Tokyo Tokyo 35.6897 139.6922 Japan JP JPN Tōkyō primary 37977000 1392685764
Jakarta Jakarta -6.2146 106.8451 Indonesia ID IDN Jakarta primary 34540000 1360771077
Delhi Delhi 28.6600 77.2300 India IN IND Delhi admin 29617000 1356872604
Mumbai Mumbai 18.9667 72.8333 India IN IND Mahārāshtra admin 23355000 1356226629
Manila Manila 14.5958 120.9772 Philippines PH PHL Manila primary 23088000 1608618140
Shanghai Shanghai 31.1667 121.4667 China CN CHN Shanghai admin 22120000 1156073548
m2 <- leaflet(data = cities) |>
  addTiles() |>
  addCircleMarkers(~ lng, ~lat, label = ~city)

Adding points

Colour regions

library(geojsonio)
pal <- colorNumeric("viridis", NULL)  # make a viridis palette
canada <- geojson_read("https://gist.githubusercontent.com/mikelotis/2156d7c170d10d2c77cb79424fe2137d/raw/7a13748ed7ea5ba64876c77c53b6cb64dd5c3ab0/canada-province.geojson", what="sp")
my_values = runif(13, 0, 10) # random numbers between 0 and 10
m3 <- canada |>
  leaflet() |>
  addTiles() |>
  addPolygons(fillColor = ~ pal(my_values),
              weight = 1, color = "black") |>
  addLegend(pal = pal, values = my_values, opacity = 1.0)

Colour regions

Other tiled map services

Google maps have more options, but you must sign up for an API key first. These examples are for anyone who wants to experiment with that option.

mymap_terrain <- ggmap::get_googlemap(bbox = c(left = -130, bottom = 41, 
                                        right = -50, top = 66), 
                               zoom=5, maptype = "terrain")
m4 <- ggmap(mymap_terrain)  

See help for the package ggmap for more services (google maps, open street maps).

Start with a small integer for zoom and increase it if your map is fuzzy. (Don’t burden yourself or a free service by downloading unnecessary data.)

Add text and points

my_points <- tibble(lat = c(43+57/60, 49+53/60),
                    lon = c(-59-55/60, -97-9/60),
                    label = c("Sable Is.", "Winnipeg")
)
m5 <- ggmap(mymap_terrain)  +
  geom_point(data = my_points, color="brown") + 
  geom_label(data = my_points, 
            aes(label=label), 
            fill = "#FFFFFF90",
            color = "black", nudge_y = 1.5)

Summary

  • Outline vs tiled (image) maps

  • Make a basic map

  • Add points and labels

  • Fill regions with colour (requires polygons)

  • There are several tile services, but free access comes and goes frequently