November 15, 2023

leaflet basics

  • leaflet operates under similar layering logic to ggplot
  • While it can be used to make static maps, it has additional power in creating interactive maps as well.
  • The official leaflet github information page is great!

Loading necessary packages

library(tidyverse)
library(sf)
library(leaflet)
library(htmlwidgets)
library(webshot)

Set Locations and Convert to SHP Type

### shape file path
shp_loc <- "datasets/shpfiles"
svi_loc <- "datasets/census_tract_svi"
image_output_loc <- "path_to_writeout_images" # need to set this yourself to where you want the output 
# image to download to

# convert to shape file system (R)
wwtp_shp <- st_read(paste0(shp_loc, "/annarbor/annarborcatch.shp"), stringsAsFactors=FALSE)
wwtp_shp_ll <- st_transform(wwtp_shp, "+proj=longlat +ellps=WGS84 +datum=WGS84")
wwtp_shp_ll <- st_as_sf(wwtp_shp_ll)
wwtp_shp_ll <- st_cast(wwtp_shp_ll, to = c("POLYGON"))


census_shp <- st_read(paste0(shp_loc, "/tl_2018_26_tract/tl_2018_26_tract.shp"), stringsAsFactors=FALSE)
census_shp_ll <- st_transform(census_shp, "+proj=longlat +ellps=WGS84 +datum=WGS84")
census_shp_ll <- st_as_sf(census_shp_ll)
census_shp_ll <- st_cast(census_shp_ll, to = c("POLYGON"))

One Basic Map

center_lat <- 42.2808
center_long <- -83.7430
map_zoom <- 12

m <- leaflet() %>% setView(lng = center_long, lat = center_lat, zoom = map_zoom) %>%
  addTiles() %>% 
  addPolygons(data = wwtp_shp_ll$geometry,
              color = "#954B6C", # you need to use hex colors
              fillOpacity = 0.4,
              weight = 1,
              smoothFactor = 0.2) %>% 
  addPolygons(data = census_shp_ll$geometry,
              color = "#000000", # you need to use hex colors
              fillOpacity = 0,
              weight = 2,
              smoothFactor = 0.2, 
              label = census_shp_ll$NAME,
              labelOptions = labelOptions(noHide = T, textsize = "8px", textOnly = TRUE))

saveWidget(m, paste0(image_output_loc, "/temp.html"), selfcontained = FALSE)
webshot(paste0(image_output_loc, "/temp.html"), file = paste0(image_output_loc, "/annarbor_catchment_censustract.png"),
        cliprect = "viewport")

Load in Data to Color By

# read in svi data
svi_ct <- read.csv(paste0(svi_loc, "/Michigan_censustract_2018_SVI.csv"))
svi_ct <- filter(svi_ct, RPL_THEMES != -999)
svi_ct <- svi_ct %>% select(FIPS, RPL_THEMES) %>% mutate(RPL_THEMES = as.numeric(RPL_THEMES))

### need to attach it to our census tract data
census_shp <- st_read(paste0(shp_loc, "/tl_2018_26_tract/tl_2018_26_tract.shp"), stringsAsFactors=FALSE)
census_shp_ll <- merge(census_shp, svi_ct, by.x = c("GEOID"), by.y = c("FIPS"), all.x = TRUE)
census_shp_ll$geometry<- st_transform(census_shp_ll$geometry,"+proj=longlat +datum=WGS84") # sf package-geometries into something usable by leaflet

Color Scheme Generation

# make a color scheme
pal2 <- colorBin("Blues", census_shp_ll$RPL_THEMES, 7, pretty = TRUE)

Color the Map

leaflet(census_shp_ll) %>% setView(lng = center_long, lat = center_lat, zoom = map_zoom) %>%
  addTiles() %>%
  addPolygons(stroke = TRUE,
              color = ~pal2(census_shp_ll$RPL_THEMES),
              
              fillOpacity = 0.7,
              smoothFactor = 0.2, 
              label = census_shp_ll$NAME,
              labelOptions = labelOptions(noHide = T, textsize = "8px", textOnly = TRUE)) %>% 
  addPolygons(data = wwtp_shp_ll$geometry,
              color = "#000000", # you need to use hex colors
              fillOpacity = 0,
              weight = 2,
              smoothFactor = 0.2) %>%
  addLegend("bottomright",
            pal = pal2,
            values = svi_ct$RPL_THEMES,
            title = "Census Tract SVI",
            opacity = 1)