In Part B we will be experimenting with the leaflet package. This package allows us to create a variety of interactive maps. For our purposes we will be looking at a data set that contains spatial and attribute information about the Starbucks Coffee Chain. We will be taking a particularly close look at Starbucks establishments in the Bay Area of California.
library(leaflet)
library(sf)
library(tidyverse)
library(dplyr)
library(here)
After loading in our packages we need to load in our data and prepare it to be mapped. In the code below we read in our starbucks store csv file and filter it to only hold Starbucks with the country value of US or CA for United States and Canada. The line that begins st_as_sf converts our regular starbucks data frame into a spatial data frame. Lastly we select only to store the columns of store name, phone number, address, city, and country.
(starbucksBayArea <- read_csv(here("Data", "directory.csv")) %>%
filter(Country == "US", `State/Province` == "CA", `City` == c("Berkeley", "El Cerrito", "Oakland", "San Francisco")) %>%
st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) %>%
select(store_name = `Store Name`, phone = `Phone Number`, address = `Street Address`, city = City))
## Simple feature collection with 30 features and 4 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -122.49 ymin: 37.74 xmax: -122.24 ymax: 37.92
## Geodetic CRS: WGS 84
## # A tibble: 30 × 5
## store_name phone address city geometry
## <chr> <chr> <chr> <chr> <POINT [°]>
## 1 Safeway - Berkeley #691 510-… 1444 S… Berk… (-122.27 37.88)
## 2 Safeway-El Cerrito #2940 510-… 11450 … El C… (-122.32 37.92)
## 3 2nd & Broadway 510-… 200 Br… Oakl… (-122.28 37.8)
## 4 Broadway & 8th Street - Oakland 510-… 801 Br… Oakl… (-122.27 37.8)
## 5 315 1/2 20th St. (510… 3 15 … Oakl… (-122.27 37.81)
## 6 Broadway & Grand - Oakland 510-… 420 W.… Oakl… (-122.27 37.81)
## 7 3347 Lakeshore Avenue (510… 3347 L… Oakl… (-122.24 37.81)
## 8 Church & Market - S.F. (415… 2018 M… San … (-122.43 37.77)
## 9 Safeway - Noriega #985 <NA> 2350 N… San … (-122.49 37.75)
## 10 California & Battery 415-… 295 Ca… San … (-122.4 37.79)
## # ℹ 20 more rows
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>% #apply a tile base map
addMarkers(data = starbucksBayArea, popup = ~store_name, label = ~city) #add markers and popups
Let’s start with a very simple leaflet map that depicts Starbucks locations in the Bay Area of California. Even with this simple map we can already begin to see the versatility and power of the leaflet package. In the code above we call our leaflet package and addProviderTiles() and addMarkers. AddProviderTiles supplies us with a base map which we specify in the paranthesis. AddMarkers creates symbols for the data we wish to plot, in our case Starbucks store locations. Within each marker we also supply it with a popup that shows the Starbucks store name and city if we hover over the symbol.
This is great but, let’s see how this will look if we customize our map by adding in a few more map features and color schemes.
In the first chunk of code below we define an object called cityColors. We then call the colorFactor function from the leaflet package. ColorFactor allows us to assign colors to entities on the map. We specify the colors we are assigning in the pallete variable where each color corresponds to the a city in our select list above. Blue is for Berkeley, purple is for El Cerrito, red is for Oakland, and dark green is for San Francisco.
#create a color palette variable for our starbucks symbols
cityColors <- colorFactor(
palette = c("blue", "purple", "red", "darkgreen"), #list of colors for each city
domain = starbucksBayArea$city)
In this next chunk of code we handle our popup messages. We define an object called popupText. This object stores the popup information for each marker denoting a Starbuck’s location. Each marker will contain a pop up that holds the store’s name, address, city, and phone number. We create these popups with HTML tags.
#create our popup text for our starbucks symbols
popupText <- paste0(
"<b>", starbucksBayArea$store_name, "</b><br>",
"<b>Address:</b> ", starbucksBayArea$address, "<br>",
"<b>City:</b> ", starbucksBayArea$city, "<br>",
"<b>Phone:</b> ", starbucksBayArea$phone)
This last chunk of code creates our actual map by calling the leaflet package on our Bay Area Starbucks data set while provding the base map for our map. Within our leaflet map creation we addCircleMarkers to denote our Starbuck’s locations on the maps. Our circle markers are filled with our created color pallete from earlier with some other modifications to help them stand out a little better against the backdrop of the base map. After adding our markers we also add in a legend for our symbols and a scale bar for our map.
#create our leaflet map
leaflet(data = starbucksBayArea) %>%
addProviderTiles("CartoDB.Positron") %>%
#add our markers to our leaflet map
addCircleMarkers(
radius = 6,
fillColor = ~cityColors(city),
color = "black",
stroke = 2,
weight = 1,
fillOpacity = 0.8,
popup = popupText,
label = ~store_name,
clusterOptions = markerClusterOptions() #cluster overlapping markers
) %>%
#add a legend
addLegend(
"bottomright",
pal = cityColors,
values = ~city,
title = "City",
opacity = 1
) %>%
#add a scale bar
addScaleBar(position = "bottomleft") %>%
addEasyButton(easyButton(
icon = "fa-crosshairs",
title = "Zoom to full extent"))