I had heard that the lines to get some food at Shake Shack are long. So when I saw a new location opening in downtown LA, I wondered how many locations does it have and how fast are they spreading across the US. The answers surprised me. Using R and previous code, I created a few maps:
Read on to learn how I got the data and plotted them.
First, let’s load our favourite libraries.
library(rvest) library(readr) library(tidyverse) library(scales) library(ggmap)
Figure out locations
On its site, Shake Shack fortunately has all the locations and opening dates, going back to April 23, 2012. The archive pages run from 1 to 20 with this URL structure:
Using SelectorGadget, I figured out the XPath and CSS code to find the opening date, location name, and location page link. Then, I wrote a function to retrieve these values from a given archive page.
get_locations % html_text(trim = TRUE), store_loc_link = html_nodes(x = nodes, css = 'h2 a') %>% html_attr("href"), stringsAsFactors = FALSE) }
I applied this function to retrieve all location opening dates, names, and individual location urls:
Find addresses of all locations
If you visit an individual location’s page, such as this Tokyo Dome page, you will see that often the exact address is not listed, or if it is, you can’t directly geocode it. But, luckily, there’s a Google Map right below the location. I thought, they must be passing some parameters to Google Maps API. I spend a good amount of time, but couldn’t figure out how they were getting the map. And. Then. I found out that the text “CLICK MAP FOR DIRECTIONS” block had a valid address as part of the hyperlink!!
I wrote another simple function to get the addresses from the given URL:
get_loc_cords % html_attr("href"), stringsAsFactors = FALSE) } location_google_maps_address
Then I joined the location name with the address data frame:
Geocoding the addresses
Using the fantastic ggmap library and mutate_geocode function, I geocoded all the addresses:
all_locations % mutate(google_addr_string = str_sub(goog_map_url, start = 36)) %>% mutate_geocode(google_addr_string, output = "latlon")
Here’s what the data frame looks like now:
You may want to create a Google developer key for mass geocoding. Since the mutate_geocode function is used by many people, sometimes you may not get all the addresses geocoded. Use register_google(key =
Now that we have all the geographical coordinates, we just need to do some clean-up to get the data ready for plotting.
First, get the date field in order and add opening month and year columns:
all_locations % mutate(open_date = as.Date(opdate, "%B %d, %Y"), open_month = lubridate::month(open_date), open_year = lubridate::year(open_date))
Second, get the cumulative count of store openings:
ss_op_data_smry % count(open_date) %>% ungroup() %>% arrange(open_date) %>% mutate(cumm_n = cumsum(n))
Third, join the summary back to the locations data frame:
Get the maps ready
Using the ggmap library, I got the US map and a world map:
Create functions to plot each location
Repurposing my code from the Walmart spread across the US, I wrote a similar function to plot locations with two different sizes: big, if the locations opened during the mapped month, and small, if the locations opened before the mapped month. I did so that we could notice the new locations.
I modified this function to map the world:
Now, the exciting part: create month-by-month maps.
all_locations_smry %>% mutate(mapid = group_indices_(all_locations_smry, .dots = 'open_date')) %>% group_by(open_date) %>% do(pl = my_us_plot(all_locations_smry, unique(.$open_date), unique(.$mapid)))
all_locations_smry %>% mutate(mapid = group_indices_(all_locations_smry, .dots = 'open_date')) %>% group_by(open_date) %>% do(pl = my_world_plot(all_locations_smry, unique(.$open_date), unique(.$mapid)))
Create a movie
Using ffmpeg, we can put all the images together to create a movie:
# works on a mac makemovie_cmd
We can use the convert function to create a gif:
# https://askubuntu.com/a/43767 makegif_cmd
That’s it! We get nice looking videos showing location openings by each month. I was surprised to see how fast the company is opening the locations as well as how many locations it has in Asia!
Using the ggimage library, I tried creating the maps using Shake Shack’s burger icon, but they didn’t turn out as good:
What do you think? How else would you visualize these data points?
The post Do know Shake Shack’s locations outside of the US? You’d be surprised appeared first on nandeshwar.info.