Implement a Choropleth Map

setwd("D:/Y2 S2/URP 535/Labs/Lab10")

library(leaflet)
library(sf)
## Warning: package 'sf' was built under R version 4.4.3
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(RColorBrewer)

mi_counties <- st_read("MI_counties.shp")
## Reading layer `MI_counties' from data source 
##   `D:\Y2 S2\URP 535\Labs\Lab10\MI_counties.shp' using driver `ESRI Shapefile'
## Simple feature collection with 83 features and 17 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -90.41839 ymin: 41.69612 xmax: -82.12297 ymax: 48.30606
## Geodetic CRS:  NAD83
mi_counties <- st_transform(mi_counties, crs = 4326)

mi_counties$ALAND <- as.numeric(mi_counties$ALAND)

pal <- colorQuantile(palette = "YlGnBu", domain = mi_counties$ALAND, n = 5)

leaflet(mi_counties) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    fillColor = ~pal(ALAND),
    weight = 1,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = ~NAME,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    )
  ) %>%
  addLegend(
    pal = pal,
    values = ~ALAND,
    opacity = 0.7,
    title = "Land Area (sq meters)",
    position = "bottomright"
  )