library(leaflet)
library(sf)
library(tidyverse)
library(USAboundaries)
library(leafem)
library(leaflet)
library(leafpop)
## systemfonts and textshaping have been compiled with different versions of Freetype. Because of this, textshaping will not use the font cache provided by systemfonts
library(here)
library(htmlwidgets)
leaflet() %>%
addTiles() %>%
setView(lng = -3.5, lat = 54.5, zoom = 6) # Whole UK
(starbucks = read_csv("directory.csv") |>
filter(Country == "GB") |> # whole country of Great Britain
filter(!is.na(Longitude) & !is.na(Latitude)) |> # remove bad coordinates
st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) |> # save as spatial object
select(store_name = `Store Name`,
phone = `Phone Number`,
address = `Street Address`,
city = City,
brand = Brand,
owner = `Ownership Type`))
## Rows: 25600 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): Brand, Store Number, Store Name, Ownership Type, Street Address, C...
## 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.
## Simple feature collection with 901 features and 6 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -7.32 ymin: 50.26 xmax: 1.75 ymax: 57.65
## Geodetic CRS: WGS 84
## # A tibble: 901 × 7
## store_name phone address city brand owner geometry
## <chr> <chr> <chr> <chr> <chr> <chr> <POINT [°]>
## 1 Shire Hotels Aztec… 0145… Aztec … Almo… Star… Lice… (-2.57 51.54)
## 2 Altrincham - Sains… 1619… Lloyd … Altr… Star… Comp… (-2.35 53.38)
## 3 SSP Ashford <NA> Ashfor… Ashf… Star… Lice… (0.88 51.14)
## 4 Ashford - High Str… <NA> 89-91 … Ashf… Star… Fran… (0.87 51.15)
## 5 WB Warwick North 0192… M40 Mo… Asho… Star… Lice… (-1.51 52.22)
## 6 WB Warwick Kiosk 0192… M40 Mo… Asho… Star… Lice… (-1.5 52.22)
## 7 Baldock Services -… 1462… MSA ar… Bald… Star… Comp… (-0.2 52.01)
## 8 Banbury - Banbury … <NA> Unit 1… Banb… Star… Fran… (-1.32 52.08)
## 9 Barnes 2087… 147 Ch… Barn… Star… Comp… (-0.24 51.48)
## 10 Barnet High Street 2084… 113 Ba… Barn… Star… Comp… (-0.2 51.66)
## # ℹ 891 more rows
Starbucks locations are displayed as clusters by region with the number of stores within circles. Zoom in to reveal exact store locations.
leaflet(data = starbucks) |>
addProviderTiles(providers$CartoDB) |>
addMarkers(clusterOptions = markerClusterOptions())
Zoom in to individual stores and hover over a point to see its name.
Click on a point to see full store details, including address, phone number, and ownership type.
Visual examination shows that most store locations around Great Britain are franchises and are clustered around the central and southern regions.
# define a palette for the ownership types
owner_levels <- c("Company Owned", "Licensed", "Franchise")
pal <- colorFactor(
palette = c("#1b9e77", "#d95f02", "#e7298a"),
domain = owner_levels,
na.color = "grey50"
)
map<- leaflet(data = starbucks) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
radius = 6,
stroke = FALSE,
fillOpacity = 0.9,
fillColor = ~pal(owner), # color by ownership type
label = ~store_name, # hover label
popup = ~paste0( # popup label with store details
"<b>", store_name, "</b><br/>",
address, "<br/>",
city, "<br/>",
"<b>Owner:</b> ", owner, "<br/>",
"<b>Phone:</b> ", phone
)
) %>%
addLegend(
position = "bottomright",
pal = pal,
values = ~owner,
title = "Ownership type",
opacity = 0.9
)
map
saveWidget(map, file = here("map.html"))