Creating a HeatMap from 2012 Starbucks locations in CT, MA and RI

Here are the steps on how to build the HeatMap:

  1. Upload libraries.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(mapview)
library(viridis)
## Loading required package: viridisLite
library(leaflet)
library(leaflet.extras)
  1. Load Starbucks locations data.
starbucks <- read_csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv", show_col_types = FALSE)
head(starbucks)
## # A tibble: 6 × 23
##   Brand     `Store Number` Name                   `Ownership Type` `Facility ID`
##   <chr>              <dbl> <chr>                  <chr>                    <dbl>
## 1 Starbucks          11854 Epping Main St         Company Owned            18228
## 2 Starbucks          79420 Stop & Shop-Exeter #2… Licensed                  9956
## 3 Starbucks           3872 Target Greenland T-25… Licensed                 10933
## 4 Starbucks          13448 Seabrook, Lafayette Rd Company Owned            16980
## 5 Starbucks          10325 Portsmouth, Woodbury … Company Owned            17269
## 6 Starbucks           7459 Portsmouth/Market Squ… Company Owned            18462
## # ℹ 18 more variables: `Features - Products` <chr>, `Features - Service` <chr>,
## #   `Features - Stations` <chr>, `Food Region` <dbl>, `Venue Type` <chr>,
## #   `Phone Number` <chr>, Location <chr>, `Street Address` <chr>,
## #   `Street Line 1` <chr>, `Street Line 2` <chr>, City <chr>, State <chr>,
## #   Zip <chr>, Country <chr>, Coordinates <chr>, Latitude <dbl>,
## #   Longitude <dbl>, `Insert Date` <chr>
ls(starbucks)
##  [1] "Brand"               "City"                "Coordinates"        
##  [4] "Country"             "Facility ID"         "Features - Products"
##  [7] "Features - Service"  "Features - Stations" "Food Region"        
## [10] "Insert Date"         "Latitude"            "Location"           
## [13] "Longitude"           "Name"                "Ownership Type"     
## [16] "Phone Number"        "State"               "Store Number"       
## [19] "Street Address"      "Street Line 1"       "Street Line 2"      
## [22] "Venue Type"          "Zip"
  1. Subset Starbucks locations data to CT, MA and RI.
starbucksCTRIMA <- starbucks  %>% 
  filter(State %in% c("CT","RI","MA"))
  1. Get a glimpse of your new dataset.
starbucksCTRIMA %>% 
  glimpse()
## Rows: 320
## Columns: 23
## $ Brand                 <chr> "Starbucks", "Starbucks", "Starbucks", "Starbuck…
## $ `Store Number`        <dbl> 11182, 7329, 76864, 7570, 76855, 7773, 885, 7450…
## $ Name                  <chr> "Haverhill, 1116 Main St", "Newburyport", "Targe…
## $ `Ownership Type`      <chr> "Company Owned", "Company Owned", "Licensed", "C…
## $ `Facility ID`         <dbl> 18106, 16075, 6510, 11932, 6511, 8898, 13579, 12…
## $ `Features - Products` <chr> "Lunch, Oven-warmed Food, Reserve Amenity", "Clo…
## $ `Features - Service`  <chr> "Starbucks Card Mobile, Wireless Hotspot", "Star…
## $ `Features - Stations` <chr> "Drive-Through", NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Food Region`         <dbl> 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, …
## $ `Venue Type`          <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unk…
## $ `Phone Number`        <chr> "978-372-3485", "978-462-4475", "978-377-9003", …
## $ Location              <chr> "1116 Main Street\nHaverhill, MA 01830-1413\n(42…
## $ `Street Address`      <chr> "1116 Main Street", "23 Market Square", "35 Comp…
## $ `Street Line 1`       <chr> "1116 Main Street", "23 Market Square", "35 Comp…
## $ `Street Line 2`       <chr> NA, NA, NA, "Unit 170", NA, NA, NA, NA, NA, NA, …
## $ City                  <chr> "Haverhill", "Newburyport", "Haverhill", "Methue…
## $ State                 <chr> "MA", "MA", "MA", "MA", "MA", "MA", "MA", "MA", …
## $ Zip                   <chr> "01830-1413", "01950-2571", "01832-1236", "01844…
## $ Country               <chr> "US", "US", "US", "US", "US", "US", "US", "US", …
## $ Coordinates           <chr> "(42.807206, -71.102071)", "(42.811138, -70.8695…
## $ Latitude              <dbl> 42.80721, 42.81114, 42.78695, 42.74415, 42.74188…
## $ Longitude             <dbl> -71.10207, -70.86950, -71.11584, -71.15735, -71.…
## $ `Insert Date`         <chr> "06/22/2012 06:17:10 PM", "06/22/2012 06:17:10 P…
  1. Make map of Starbucks locations using mapview().
library(mapview)
# View map options and select "OpenStreetMap"
# mapviewOptions()
starbucksCTRIMA %>% 
mapview(xcol = "Longitude", ycol = "Latitude", crs = 4326, grid = FALSE, map.types = "OpenStreetMap")
  1. Finally, create the HeatMap for CT, RI and MA Starbucks locations.
library(leaflet)
library(leaflet.extras)
starbucksCTRIMA %>% 
  leaflet() %>% 
  addTiles() %>% 
  addHeatmap(lng=~Longitude, lat=~Latitude, blur= 5, max = 5, radius = 30)

A.M.D.G.