Introduction

This document presents a spatial analysis of Starbucks locations in the Toronto and Mississauga regions, including the creation of interactive leaflet maps and visualization of Starbucks locations using mapview.

1. Visualizing Starbucks Locations with Leaflet

# Load necessary libraries
library(sf)
library(leaflet)
library(leafem)
library(dplyr)
library(here)
library(mapview)

Data Preparation

The data preparation process involved loading the Starbucks locations dataset and filtering it to include only stores located in Toronto and Mississauga. This filtering ensured that the analysis focused solely on these two regions of interest. Additionally, the dataset was converted into a spatial object for visualization purposes, enabling the creation of maps to display the distribution of Starbucks stores effectively.

# Load the dataset
starbucks_data <- read.csv(here("r_data", "directory.csv"))
head(starbucks_data)
##       Brand Store.Number               Store.Name Ownership.Type
## 1 Starbucks 47370-257954            Meritxell, 96       Licensed
## 2 Starbucks 22331-212325         Ajman Drive Thru       Licensed
## 3 Starbucks 47089-256771                Dana Mall       Licensed
## 4 Starbucks 22126-218024               Twofour 54       Licensed
## 5 Starbucks 17127-178586             Al Ain Tower       Licensed
## 6 Starbucks 17688-182164 Dalma Mall, Ground Floor       Licensed
##                    Street.Address             City State.Province Country
## 1               Av. Meritxell, 96 Andorra la Vella              7      AD
## 2            1 Street 69, Al Jarf            Ajman             AJ      AE
## 3    Sheikh Khalifa Bin Zayed St.            Ajman             AJ      AE
## 4                 Al Salam Street        Abu Dhabi             AZ      AE
## 5 Khaldiya Area, Abu Dhabi Island        Abu Dhabi             AZ      AE
## 6            Dalma Mall, Mussafah        Abu Dhabi             AZ      AE
##   Postcode Phone.Number                Timezone Longitude Latitude
## 1    AD500    376818720 GMT+1:00 Europe/Andorra      1.53    42.51
## 2                          GMT+04:00 Asia/Dubai     55.47    25.42
## 3                          GMT+04:00 Asia/Dubai     55.47    25.39
## 4                          GMT+04:00 Asia/Dubai     54.38    24.48
## 5                          GMT+04:00 Asia/Dubai     54.54    24.51
## 6                          GMT+04:00 Asia/Dubai     54.49    24.40
# Filter data for a region of interest (e.g., Toronto and Mississauga)
region_data <- starbucks_data %>%
  filter(City %in% c("Toronto", "Mississauga"))

head(region_data)
##       Brand Store.Number                          Store.Name Ownership.Type
## 1 Starbucks 75482-115327 Compass @ University of Toronto Mis       Licensed
## 2 Starbucks 75924-102701 YYZ Toronto Int'l Term Pier F Upper       Licensed
## 3 Starbucks 23283-228465                 1016 Eglinton Ave E  Company Owned
## 4 Starbucks 16013-164904 Compass @ Sheridan College, Mississ       Licensed
## 5 Starbucks 20593-206884                  5029 Hurontario St  Company Owned
## 6 Starbucks  4665-100880                    3035 Argentia Rd  Company Owned
##                                           Street.Address        City
## 1                                  3359 Mississauga Rd N Mississauga
## 2                     6301 Silver Dart Drive, Terminal 1 Mississauga
## 3                              1016 Eglinton Avenue East Mississauga
## 4                                 4180 Duke of York Blvd Mississauga
## 5                         5029 Hurontario Street, Unit 1 Mississauga
## 6 3035 Argentia Road, unIT 7, Square One Shopping Centre Mississauga
##   State.Province Country Postcode   Phone.Number                  Timezone
## 1             ON      CA  L5L 1C6                GMT-05:00 America/Toronto
## 2             ON      CA  L5P 1B2   416-776-3100 GMT-05:00 America/Toronto
## 3             ON      CA  L4W 1K3 (905) 624-9088 GMT-05:00 America/Toronto
## 4             ON      CA  L5B 0G5                GMT-05:00 America/Toronto
## 5             ON      CA  L4Z 3X7     9055070561 GMT-05:00 America/Toronto
## 6             ON      CA  L5N 8P7   905-785-7667 GMT-05:00 America/Toronto
##   Longitude Latitude
## 1    -79.66    43.55
## 2    -79.62    43.68
## 3    -79.63    43.63
## 4    -79.64    43.59
## 5    -79.65    43.61
## 6    -79.79    43.60

In this section, we utilize the Leaflet package to create an interactive map illustrating the Starbucks locations in Toronto and Mississauga. The map highlights the geographical distribution of stores in the region and provides additional details about each outlet upon clicking.

# Check if the filtered dataset contains valid values
print(unique(region_data$City))
## [1] "Mississauga" "Toronto"
# Create a color palette and scheme according to the variable mapped
# Assuming there's a variable representing the number of stores in each city
color_palette <- colorFactor(c("blue", "red"), domain = c("Toronto", "Mississauga"))

# Create the leaflet map
toronto_map <- leaflet(data = region_data) %>%
  # Add light tiles for a clearer map view
  addProviderTiles("CartoDB.Positron") %>%
  # Add circle markers for Starbucks locations with color ramp based on City
  addCircleMarkers(
    lng = ~Longitude, 
    lat = ~Latitude,
    color = ~color_palette(City),
    fillOpacity = 0.5,
    radius = 5,
    popup = ~paste("<b>Store Name:</b>", `Store.Name`, "<br>",
                   "<b>Address:</b>", `Street.Address`, "<br>",
                   "<b>City:</b>", City)
  ) %>%
  # Add legend for the marker color ramp
  addLegend(
    position = "topright", 
    pal = color_palette, 
    values = ~City, 
    title = "City"
  ) %>%
  # Add Leaflet Map Caption
  addControl(
    html = "<div class='leaflet-control leaflet-control-custom'>This interactive Leaflet map displays the distribution of Starbucks locations in Toronto and Mississauga. Each marker represents a Starbucks store, with colors distinguishing between Toronto (blue) and Mississauga (red). Clicking on a marker reveals additional information such as store name, address, and city.</div>",
    position = "bottomleft"
  )

# Show the map
toronto_map

This interactive map visualizes the Starbucks locations in Toronto and Mississauga. Each circle marker represents a store, with the color indicating the respective city. Clicking on a marker reveals details such as the store name and address.

2. Visualizing Starbucks Locations with mapview

In this section, we employ the mapview package to create a spatial map displaying Starbucks locations in Toronto and Mississauga. The map offers a different perspective on the distribution of stores and allows for easy comparison between the two cities.

# Convert region_data to a spatial object
GTA_Region <- st_as_sf(region_data, coords = c("Longitude", "Latitude"), crs = 4326)

# Define colors for each city
colors <- c("blue", "red")  # Blue for Toronto, Red for Mississauga

# Define city names for the labels
city_labels <- c("Toronto", "Mississauga")

# Assign colors to cities
GTA_Region$city_color <- factor(GTA_Region$City, levels = city_labels)

# Display the map using mapview with colors for each city
map <- mapview(GTA_Region, zcol = "city_color", col.regions = colors, legend.title = "Cities")

# Display the map with caption
map

This map provides a spatial representation of Starbucks locations in Toronto and Mississauga. Each point corresponds to a store, with colors indicating the respective city. The map offers insights into the geographical spread of Starbucks outlets across the two regions.

Conclusion

The spatial analysis of Starbucks locations in Toronto and Mississauga reveals interesting insights into the distribution patterns of the popular coffee chain within these regions. Through the visualization of store locations using Leaflet and mapview, we observed the following:

Geographical Distribution: Both maps showcase the geographical spread of Starbucks stores, highlighting areas of high concentration as well as areas with fewer outlets.

City-wise Comparison: The maps allow for a comparison between Toronto and Mississauga, revealing differences in the density and distribution of Starbucks stores between the two cities.

Accessibility: Analyzing the spatial distribution of Starbucks outlets can provide insights into accessibility for residents and visitors, as well as potential market saturation in certain areas.