CH 18 Problem 2 part(b) - Leaflet map

Chapter 18

Problem 2 - Part(b)

df_trails <- as.data.frame(macleish_layers[["trails"]]) %>% 
  mutate(name = factor(name))

head(df_trails)
              name  color                       geometry
1  Porcupine Trail  White LINESTRING (-72.68291 42.45...
2     Western Loop    Red LINESTRING (-72.68111 42.45...
3 Poplar Hill Road   Road LINESTRING (-72.68155 42.45...
4 Vernal Pool Loop Yellow LINESTRING (-72.68265 42.44...
5     Eastern Loop   Blue LINESTRING (-72.68113 42.45...
6     Western Loop    Red LINESTRING (-72.68401 42.45...
border <- tibble(lat = 42.449167, lon = -72.679389)
color_pal <- colorFactor(palette = "Paired", domain = df_trails$name, n = 5)

leaflet(df_trails) %>%
  addTiles() %>%
  addPolygons(
    data = macleish_layers[["boundary"]],
    weight = 3, 
    color = "black"
  ) %>%
  addPolylines(
    data = macleish_layers[["trails"]],
    weight = 3, 
    color = ~color_pal(df_trails$name)
  ) %>% 
  addPolygons(
    data = macleish_layers[["buildings"]],
    weight = 5, color = "blue"
  ) %>% 
  addPolygons(
    data = macleish_layers[["forests"]],
    weight = 0.1,
    popup = ~type
  ) %>% 
  setView(lat = border$lat, 
          lng = border$lon, 
          zoom = 14) %>% 
  addLegend(pal = color_pal, 
            values = ~df_trails$name,
            title = "Trail name",
            opacity = 3)
Trail name
Driveway
Eastern Loop
Easy Out
entry trail
Poplar Hill Road
Porcupine Trail
Snowmobile Trail
Vernal Pool Loop
Western Loop
Leaflet | © OpenStreetMap contributors, CC-BY-SA