Chapter 1 Introduction

This book focuses on using leaflet and shiny together to make interactive maps.

Here’s a simple leaflet map.

library(leaflet)

leaflet() %>%
  addTiles() %>% # Add default OpenStreetMap map tiles
  addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")

Figure 1.1: Simple leaflet map

Before we begin adding to this map, we need to create the layers that we want to add.

In the iTRAQI app, we used markers, rasters and polygons to show the key locations and interpolations.

See the iTRAQI shiny app here and read more about it in the information tab of the app.

Chapter 2 will focus on these first steps, before making any maps or interactivity. If you’re already well-versed in making these layers and the sf R package, you can skip to the latter chapters.

1.1 leaflet layers

  • To display statistical area level 1 and 2 (SA1 and SA2) regions on the map, we will be using sf objects with MULTIPOLYGON geometries. These are multipolygons because some of these areas include distinct areas, such as a set of islands, that aren’t contained within a single polygon.

  • To display the location of acute and rehab centers and town locations with travel times that we used for interpolations, we used (spatial) data.frames that had longitudes and latitudes for their location.

  • To display the continuous interpolations, we used RasterLayer objects.

Using a polygon and raster layer that’s used in the iTRAQI map and some markers in a data.frame, we can make see the basic approach that we use to display these on a leaflet map.

First, lets make a data.frame with the coordinates for the Princess Alexandra and Townsville University Hospitals, and download a raster and polygon layer from the iTRAQI app GitHub repository.

library(tidyverse)
library(sf)
download_layer <- function(layer_name, save_dir = "input") {
  githubURL <- glue::glue("https://raw.githubusercontent.com/RWParsons/iTRAQI_app/main/input/layers/{layer_name}")
  download.file(githubURL, file.path(save_dir, layer_name), method = "curl")
  readRDS(file.path(save_dir, layer_name))
}

raster_layer <- download_layer("rehab_raster.rds") %>%
  raster::raster(., layer = 1)

polygons_layer <- download_layer("stacked_SA1_and_SA2_polygons_year2016_simplified.rds")
polygons_layer <- polygons_layer[polygons_layer$SA_level == 2, ] # show SA2 regions for example

marker_locations <- data.frame(
  centre_name = c("Princess Alexandra Hospital (PAH)", "Townsville University Hospital"),
  x = c(153.033519, 146.762041),
  y = c(-27.497374, -19.320502)
)

Here, in figure 1.2, we make a leaflet map with the three object types. We will use these three functions, addPolygons(), addRasterImage(), and addMarkers() to add almost all of the content to our leaflet maps.

leaflet() %>%
  addProviderTiles("CartoDB.VoyagerNoLabels") %>% # add a simple base map
  addPolygons(
    data = polygons_layer,
    fillColor = "Orange",
    color = "black",
    weight = 1,
    group = "Polygons"
  ) %>%
  addRasterImage(
    x = raster_layer,
    colors = "YlOrRd",
    group = "Raster"
  ) %>%
  addMarkers(
    lng = marker_locations$x,
    lat = marker_locations$y,
    label = marker_locations$centre_name,
    group = "Points"
  ) %>%
  addLayersControl(
    position = "topright",
    baseGroups = c("Polygons", "Raster", "Points"),
    options = layersControlOptions(collapsed = FALSE)
  )

Figure 1.2: leaflet map with polygons, rasters and markers

Almost all of these objects were made before being used in the shiny app. Chapter 2 will introduce the methods used to make them. Chapter 3 will introduce the basics of a shiny app. The following chapters will introduce the more specific methods that were used to construct the iTRAQI app itself.