L4 Mapping Starbucks locations in Minnesota
Based on ownership status
Import necessary libraries
packages <- c("tidyverse", "here", "ggplot2", "dplyr", "tidyr", "scales", "RColorBrewer", "units", "cowplot", "sf", "leaflet")
invisible(lapply(packages, library, character.only=TRUE)) # Dont display library import
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ 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
## here() starts at C:/Users/kmayo/Documents/588
##
##
## Attaching package: 'scales'
##
##
## The following object is masked from 'package:purrr':
##
## discard
##
##
## The following object is masked from 'package:readr':
##
## col_factor
##
##
## udunits database from C:/Users/kmayo/AppData/Local/Programs/R/R-4.3.1/library/units/share/udunits/udunits2.xml
##
##
## Attaching package: 'cowplot'
##
##
## The following object is masked from 'package:lubridate':
##
## stamp
##
##
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
Read in Starbucks data
# Read data
directory_raw <- read_csv(here('data', 'directory.csv'),show_col_types=FALSE)
Clean Starbucks data
Simplify naming conventions of relevant attributes for easier
coding.
# Data prep
# clean column names and output new directory
directory<-directory_raw%>%
rename(lon=Longitude,lat=Latitude,name='Store Name',storenum='Store Number', type='Ownership Type', state = 'State/Province') # simplify relevant attribute names
Filter data to Minnesota locations
Filter and extract only starbucks locations in MN.
directory_msp<-directory%>% # create new data for MSP locations
filter(state == 'MN')
Set color scheme
There are only two ownership types: licensed and company owned, so
we will only need to use two colors.
# Select symbology colors, blue and green. Symbology will represent ownership type
# There are only two ownership types (Company Owned and Licensed)
pal <-colorFactor(c("blue","green"), domain=directory_msp$type) # set palette
Create leaflet map
Create a webmap that displays starbucks locations in MN. Each
location will be color coded to specify ownership. Each location
labelled by official stor number and popups display official store name
+ ownership.
leaflet(directory_msp)%>% # using MN data,
addProviderTiles(providers$CartoDB)%>% # and carto db basemap,
addCircleMarkers(~lon,~lat, # zoom to MN data area
radius=2, # configure point symbology
stroke=TRUE,
color=~pal(type),
opacity=1,
fillColor = ~pal(type),
fillOpacity=0.75,
# configure pop up
popup = ~paste(name,"Ownership Type: ", type), #concatenate store name and ownership type
# add label
label=~paste("Store:",storenum))%>% # name it after the store name.
# Add legend
addLegend(position="bottomright", #put it in the bottom right
pal=pal, # use existing palette
values=~type, # display based on ownership type
title="Ownership Type")%>%
# Define map extents based on coords of data.
fitBounds( lng1 = min(directory_msp$lon),
lat1 = min(directory_msp$lat),
lng2 = max(directory_msp$lon),
lat2 = max(directory_msp$lat))