Mapping Starbucks Locations

Welcome to my R tutorial series

This is where I’ll be posting tutorials on how to use R and Rstudio to create some amazing graphics and visualizations. If you are completely new to R, don’t worry, I will post guides to explain how to start form scratch. This post assumes you have R and Rstudio installed and know how to install packages. For this tutorial you will need to download tidyverse, RCurl, and leaflet which you can do in the bottom right panel of Rstudio by clicking the install panel.

Starbucks locations in the US

This was created in Rstudio using the leaflet package , the data was obtained from a .csv (comma separated values) containing store location information from all Starbucks in the US.

I got inspired by compciv.org which posted a tutorial on how to map out Starbucks locations in the US and calculate the distance to the closest store. I wanted to recreate a similar map in R and it turns out that you with very little code thanks to the leaflet package.

1. Load tidyverse, RCurl and leaflet

library(tidyverse)
library(RCurl)
library(leaflet)

tidyverse- one of the best packages for data science, it includes everything you need to import, tidy and graph data
RCurl- allows you to pull data from websites like csv files

leaflet- is the package that allows you to make powerful maps without having to know javacript

2. Import online csv

starbucks_data <- getURL("http://www.compciv.org/files/datadumps/practicum/us-starbucks-distant.csv")

starbucks <- read_csv(starbucks_data)
head(starbucks)
## # A tibble: 6 x 12
##   City  Name  Latitude Longitude `Store Number` `Street Combine…
##   <chr> <chr>    <dbl>     <dbl> <chr>          <chr>           
## 1 Pitt… Carp…     42.2     -83.7 13531-106377   3650 Carpenter …
## 2 Wilm… 1515…     42.1     -87.7 224-119704     1515 North Sher…
## 3 Spar… S Mc…     39.5    -120.  10913-101634   1560 S. Stanfor…
## 4 Yuba… Hwy …     39.1    -122.  14071-108147   1615 Colusa Hwy…
## 5 OFal… Gree…     38.6     -89.9 14263-116745   1126 Central Pa…
## 6 Meta… Vete…     30.0     -90.2 11875-105707   4312 Veterans B…
## # ... with 6 more variables: `Postal Code` <chr>, `Country
## #   Subdivision` <chr>, Country <chr>, `Ownership Type` <chr>,
## #   nearest_store_number <chr>, nearest_store_distance_km <dbl>

3. Create a map using leaflet and Starbucks Longitude and Latitude data

map <- leaflet() %>%
  addTiles() %>%
  addMarkers(starbucks$Longitude,
  starbucks$Latitude,
  popup=starbucks$Name,
  clusterOptions = markerClusterOptions())

leaflet- blank canvas map

addMarkers- creates the actual points on the map from the starbucks.csv

popup- specifies what information the popup should have

clusterOptions- creates the dynamic clustering for when you zoome and pan around the webapp

comments powered by Disqus