For this Leaflet practice I created an interactive point map showing gas station locations across Palm Beach County, Florida. There are two purposes of this map, the first is to apply the basic functionality of the leaflet package by using a real point data set while also making the map visually clear and interactive for the user. The second purpose, on a less professional note, is because gas prices are rising so I intend on adding in prices into this interactive map for my own personal use and curious mind. The final map within this practice includes the base components practiced and utilized within the lesson: Customized circle markers, a legend, popup information and labels. I symbolized the points by gas station brand so that the map would show variation across the county rather than simply plotting all locations the same way.
The data set used for this lab contains gas station point locations in Palm Beach County. After downloading the data from ArcGIS, I cleaned the table down to the most relevant fields for the map: address, city, brand, site name, longitude, and latitude. Since some of the original brand names were inconsistent or abbreviated, I also cleaned those values in R so that the legend would be easier to read and interpret.
The data is available from ArcGIS REST Services and is filtered from the CSV for Palm Beach County and saved it to R.
Data source: Florida Gas Stations FeatureServer https://services3.arcgis.com/lq5SzPW1rLR0h1u9/arcgis/rest/services/fl_gas_stations/FeatureServer
In this following section I prepared the data being used in order for it to be more easily molded within Leaflet. Of course we begin by first loading in the packages that are going to be used for the overall lab. Before map creation I first had to read in the data that was downloaded prior and following this the data began as a normal table rather than a spatial object so I took the table and converted it into an sf object by using the longitude and latitude fields as coordinates. I then took the brand names and cleaned them since some of the values were inconsistent or abbreviated. I then created a color pallet to associate to each gas station brand and built the popup text that will appear on each marker. I did all of this in order to make the future legend easier to read and make the overall map look more organized. The following sections of code will show this same process with each individual section titled accordingly.
library(tidyverse)
library(sf)
library(leaflet)
gas_stations <- read_csv("data/PalmBeachGasStations.csv")
## Rows: 65 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): address, city, brand, site_name
## dbl (2): longitude, latitude
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
gas_stations_sf <- gas_stations %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
# Standardizing of the brand names for clear legend, combining similar and equivalent fields for easier interpretation
gas_stations_sf <- gas_stations_sf %>%
mutate(
brand = str_trim(brand),
brand = case_when(
str_detect(brand, "EXXON") ~ "Exxon",
str_detect(brand, "SHELL") ~ "Shell",
str_detect(brand, "MARATHON") ~ "Marathon",
str_detect(brand, "CHEVRON|CHVRN") ~ "Chevron",
str_detect(brand, "CITGO") ~ "Citgo",
str_detect(brand, "RACETRAC") ~ "RaceTrac",
str_detect(brand, "PAC") ~ "Pac Pride",
str_detect(brand, "CIRCLE") ~ "Circle K",
str_detect(brand, "WAWA|7-ELEVEN") ~ "Convenience Chain",
str_detect(brand, "CUMBERLAND") ~ "Cumberland Farms",
str_detect(brand, "UNBRANDED|PRIVATE") ~ "Independent/Unbranded",
str_detect(brand, "SPEEDWAY") ~ "Speedway",
str_detect(brand, "MURPHY USA") ~ "Murphy USA",
TRUE ~ brand
)
)
pal <- colorFactor(
palette = "Dark2",
domain = gas_stations_sf$brand
)
# This is the popup text so users can click each point and view basic station information
gas_stations_sf <- gas_stations_sf %>%
mutate(
popup_text = paste0(
"<b>", site_name, "</b><br/>",
"<b>Brand:</b> ", brand, "<br/>",
"<b>Address:</b> ", address, "<br/>",
"<b>City:</b> ", city
)
)
The map below shows gas stations located across Palm beach county. Each point is colored by each brand and users can simply click on each marker to get a detailed view on that specific gas station. Labels were also added so that station names can be viewed in an easier fashion while exploring the map.
# creation of the interactive Leaflet map with distinct label positioning
leaflet(data = gas_stations_sf) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
color = ~pal(brand),
fillOpacity = 0.9,
radius = 7,
stroke = FALSE,
popup = ~popup_text,
label = ~site_name,
labelOptions = labelOptions(direction = "top")
) %>%
addLegend(
position = "bottomright",
pal = pal,
values = ~brand,
title = "Gas Station Brand",
opacity = 1
)
Figure 1. Interactive Leaflet map showing gas station locations across Palm Beach County, Florida. Circle markers are symbolized by gas station brand, and popup windows display the site name, address, and city for each location.
The final map shows that gas stations are distributed widely across Palm Beach County but they are clearly more concentrated in the more urbanized eastern area of the county than in the western areas. Note that it is likely many Gas stations may not be visible as the data set seems to mostly include major brand names, excluding minor brands or more privatized stations as well as stations created past the year 2020. Symbolizing the points by brand helped show that certain brands appear multiple times across the county while others are less common and appear only in a few locations, for example the independent/unbranded stations. The popup information also makes it easier to inspect specific stations individually rather than only viewing the pattern at the county scale. The leaflet techniques here create a visual that is easily digestible despite the large amount of data being shown at once.