library(sf)
library(tidyverse)
library(leaflet)
# Read and convert trail data to spatial format
trails <- read_csv('Traildata.csv') %>%
st_as_sf(coords = c("StartX", "StartY"), crs = 4326) %>%
select(Trail_Name, Difficulty, Access, Miles)
# Round length to two decimal places
trails$Miles <- round(trails$Miles, 2)
# Define difficulty colors as a named list
difficulty_colors <- list("green" = "green", "blue" = "blue", "black" = "black")
# Write function to get color based on difficulty, return yellow if it does not fit in list
get_color <- function(difficulty) {
if (difficulty %in% names(difficulty_colors)) {
return(difficulty_colors[[difficulty]])
} else {
return("yellow") # Default color for all other values
}
}
# Ensure the Color column is a standard character vector (not named)
trails$Color <- unname(sapply(trails$Difficulty, get_color))
# Create the Leaflet map and add circle markers with color based on difficulty
leaflet(trails) %>%
setView(lng = -120.66, lat = 35.279, zoom = 13) %>%
addProviderTiles(providers$Esri.WorldImagery, options = providerTileOptions(opacity = 0.6)) %>%
addCircleMarkers(
radius = 6,
color = ~Color,
fillOpacity = 0.8,
popup = ~paste("<b>Trail:</b>", Trail_Name, "<br><b>Difficulty:</b>", Difficulty,"<br><b>Access:</b>", Access, "<br><b>Trail Length (mi):</b>", Miles),
label = ~Trail_Name
) %>%
# Add a legend
addLegend(
position = "bottomright",
title = "Trail Difficulty",
colors = c("green", "blue", "black", "yellow"),
labels = c("Green (Easy)", "Blue (Moderate)", "Black (Difficult)", "Other (No Rating)"),
opacity = 0.8
)