chooseCRANmirror(ind = 1)  # Choose a CRAN mirror interactively

Exercise of using MAPBOX API

I participated in Kyle Walker’s workshop ‘Location Intelligence with R and Mapbox’ on May 15, 2024. This R Markdown document is my exercise using the Mapbox API. Below are the basic packages I installed. I have removed my personal Mapbox token from this document.

options(tigris_use_cache = TRUE)


install.packages(c("tidycensus", "mapview", "tidyverse",
                   "janitor", "mapboxapi", "gganimate",
                   "leaflet.extras2", "ggspatial", "glue",
                   "randomNames"))
## 
## The downloaded binary packages are in
##  /var/folders/94/xq_5tm3s7jx0qrwy9cyvhptm0000gn/T//RtmpRjP7cO/downloaded_packages
library(mapboxapi)
## Usage of the Mapbox APIs is governed by the Mapbox Terms of Service.
## Please visit https://www.mapbox.com/legal/tos/ for more information.
miyoung_token <- "It should be your API token, I removed it for data privacy."
#mb_access_token(miyoung_token, install = TRUE)
#Sys.setenv(MAPBOX_PUBLIC_TOKEN = miyoung_token)

Basic uses mapview library

This is my child’s elementary school address. First, with the mapboxapi library, the mb_geocode() function is used for geocoding. Geocoding is the process of converting a text-based description of a location, such as addresses or place names, into geographic coordinates like latitude and longitude. Then, I can simply plot the address on the map using the mapview library.

Milwaukee County Park dataset

I downloaded the dataset for Milwaukee County parks in Wisconsin from the OpenData site. The link is https://gis-mclio.opendata.arcgis.com/datasets/9c05da1d46f044eb95d45a6b71ed76dd/explore?location=43.011734%2C-87.866229%2C11.00

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mk_park <- read_csv("/Users/miyoungyoon/R_miyoung/Milwaukee_Park/Milwaukee_County_Parks.csv")
## Rows: 154 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): Name, ParkID, Name_Sub, State, County, Address, Region, Unit, Last...
## dbl  (3): ObjectID, Acres, ShapeSTArea
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(mk_park)
## # A tibble: 6 × 13
##   ObjectID Name          Acres ParkID Name_Sub State County Address Region Unit 
##      <dbl> <chr>         <dbl> <chr>  <chr>    <chr> <chr>  <chr>   <chr>  <chr>
## 1       19 Chippewa Park 10.5  CHI    Chippewa Wisc… Milwa… 11500 … Centr… Curr…
## 2       20 Alcott Park   16.8  ALC    Alcott   Wisc… Milwa… 3751 S… Centr… Gree…
## 3       21 Vogel Park    11.8  VOG    Vogel    Wisc… Milwa… 8601 W… North  Dine…
## 4       22 Nash Park      9.56 NAS    Nash     Wisc… Milwa… 7800 W… North  Dine…
## 5       23 Lucille Berr…  3.00 LIP    Lucille… Wisc… Milwa… 3629 N… North  Linc…
## 6       24 Atkinson Tri…  1.28 ATK    Atkinso… Wisc… Milwa… 936 W.… North  Linc…
## # ℹ 3 more variables: Last_Update <chr>, Park_Class <chr>, ShapeSTArea <dbl>

Batch geocoding

In the table, there are multiple addresses. The mb_batch_geocode() function is used to send an address table to a geocoding service and retrieve X and Y coordinates for all of those addresses.

mk_park_sf <- mk_park |> 
  mb_batch_geocode(
    address_line1 = "Address",
    place = "County",
    region = "State",
    country = "US"
  )

head(mk_park_sf)
## Simple feature collection with 6 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -88.03465 ymin: 42.97624 xmax: -87.92278 ymax: 43.10905
## Geodetic CRS:  WGS 84
## # A tibble: 6 × 17
##   ObjectID Name          Acres ParkID Name_Sub State County Address Region Unit 
##      <dbl> <chr>         <dbl> <chr>  <chr>    <chr> <chr>  <chr>   <chr>  <chr>
## 1       19 Chippewa Park 10.5  CHI    Chippewa Wisc… Milwa… 11500 … Centr… Curr…
## 2       20 Alcott Park   16.8  ALC    Alcott   Wisc… Milwa… 3751 S… Centr… Gree…
## 3       21 Vogel Park    11.8  VOG    Vogel    Wisc… Milwa… 8601 W… North  Dine…
## 4       22 Nash Park      9.56 NAS    Nash     Wisc… Milwa… 7800 W… North  Dine…
## 5       23 Lucille Berr…  3.00 LIP    Lucille… Wisc… Milwa… 3629 N… North  Linc…
## 6       24 Atkinson Tri…  1.28 ATK    Atkinso… Wisc… Milwa… 936 W.… North  Linc…
## # ℹ 7 more variables: Last_Update <chr>, Park_Class <chr>, ShapeSTArea <dbl>,
## #   matched_address <chr>, accuracy <chr>, confidence <chr>,
## #   geometry <POINT [°]>

Draw a map using burst control in mapview

The advanced controls related to legends in mapview can be found at this link: https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html

mapview(mk_park_sf, zcol = "Region", burst = TRUE)