16  Mapping With leaflet

16.1 Call Libraries

Show the code
library(leaflet) # web based maps

library(sf) # simple (spatial) features

library(readr) # import csv

library(dplyr) # data wrangling

library(here) # where am I?

library(pander) # nice tables

# setwd(here()) # set the working directory

16.2 Get Simulated Client Data

Show the code
clients <- read_csv("./location-data/clients.csv")

pander(head(clients)) # top of client data
Table continues below
ID age gender race_ethnicity family_income program
2892 23 Male African American 42359 Program B
1971 39 Female Asian American 66500 Program C
4728 26 Female Asian American 52726 Program C
1020 24 Male Latinx 52911 Program D
4429 36 Female Asian American 50287 Program C
3136 33 Male African American 45570 Program C
mental_health_T1 mental_health_T2 latitude longitude
95.25 106.8 42.16 -83.6
82.64 96.3 42.29 -83.88
80.49 98.72 42.14 -83.78
93.82 91.67 42.24 -83.68
83.37 99.69 42.18 -83.64
75.28 92.9 42.21 -83.7

16.3 Only Clients In Ann Arbor Area

Show the code
clients <- clients %>% 
  filter(latitude <= 42.35 &
           latitude >= 42.2 &
           longitude >= -83.8 &
           longitude <= -83.65)

16.4 Read in Shapefiles

Show the code
parks <- read_sf("./shapefiles/AA_Parks/AA_Parks.shp")

university <- read_sf("./shapefiles/AA_University/AA_University.shp")

city_boundary <- read_sf("./shapefiles/AA_City_Boundary/AA_City_Boundary.shp")

17 Transform CRS

“Map projections try to portray the surface of the earth or a portion of the earth on a flat piece of paper or computer screen. A coordinate reference system (CRS) then defines, with the help of coordinates, how the two-dimensional, projected map in your GIS is related to real places on the earth. The decision as to which map projection and coordinate reference system to use, depends on the regional extent of the area you want to work in, on the analysis you want to do and often on the availability of data.” From qgis.org

see https://stackoverflow.com/questions/66471147/how-to-plot-sp-object-as-sf-in-r-leaflet

Show the code
university <- st_transform(university, 4326) # transform CRS

parks <- st_transform(parks, 4326) # transform CRS

city_boundary <- st_transform(city_boundary, 4326) # transform CRS

17.1 Leaflet Map

17.1.1 Color Palette

Show the code
pal <- colorFactor(c("red", "blue", "orange", "green"), 
                   domain = levels(as.factor(clients$program)))

17.1.2 Map

Show the code
leaflet(clients) %>%
  setView(lng = mean(clients$longitude), 
          lat = mean(clients$latitude), 
          zoom = 12) %>% 
  # addTiles() %>% # Open StreetMap
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(~longitude, 
             ~latitude, 
             popup = ~paste("Client ID:", as.character(ID)), 
             label = ~paste("Client ID:", as.character(ID)),
             color = ~pal(program),
             clusterOptions = markerClusterOptions()) %>%
  addLegend("bottomright", 
            pal = pal, 
            values = ~program,
            title = "Program") %>%
  # addPolygons(data = parks, color = "green") %>%
  # addPolygons(data = university, color = "blue") %>%
  addPolygons(data = city_boundary, 
              color = "red",
              fillOpacity = 0.0)