Growth of Starbuck’s Stores since 2013-Animation

Animation ( GIF) on US Map using R

Mohit Singh
Datafloq
3 min readSep 24, 2018

--

Animation created using R to show the growth of Starbucks stores in 2016

Visualizing data on a map is certainly the best way to represent any geo-spatial data. I am a Starbucks fan, well because I am a coffee addict and Starbucks is the most convenient place to get my morning coffee from because well, its everywhere. Though my favorite coffee is from Blue Bottle Coffee (NYC). When I was in New York City, I almost saw Starbucks store in like every corner, sometimes two in a block, I was curious to find out where and how many new stores are opened recently to see how they are expanding.

Growth of Starbucks stores since 2013 in US

The above plot showcases the exact locations of the stores opened since 2013 across US based on the data from opendata.socrata.com.

As we can see majority of stores were opened in New York, Los Angeles, Houston, Dallas, Chicago, Denver, Portland, Detroit etc.

NEXT STEP: To show the growth in form of animation using R

For this step, i narrowed down the data to just 2016 (memory issues). I also mapped the data to the cities with population > 300k and the cities where more than 4 stores were opened to appear on the map.

Just to see more smooth transition, another clip showing the stores opened in 2 months ( June & July), 2016.

Growth of stores in 2 months — June & July 2016

R Code for the plot and the Animation (GIF):

library(ggplot2)
library(tidyverse)
library(lubridate)
library(animation)
library(stringr)
library(ggrepel)
library(maptools)
library(ggmap)
starbucks <- read.csv(“starbucks.csv”)starbucks_dat <- starbucks %>%
filter(Country.Subdivision != “AK”,Country.Subdivision != “HI” )
starbucks_dat <-na.omit(starbucks_dat)
starbucks_dat$First.Seen <- format(as.POSIXct(starbucks_dat$First.Seen,format=’%m/%d/%Y’),format=’%m/%d/%Y’)
starbucks_dat$First.Seen <-as.Date(starbucks_dat$First.Seen,format=’%m/%d/%Y’)
starbucks_dat <- starbucks_dat[starbucks_dat$First.Seen>ymd(“2015–01–01”),]# Get the coordinates of major cities of US (more than 300k inhabitants)
major_cities <- maps::us.cities %>%
filter(country.etc != “HI”, country.etc != “AK”, pop > 300000) %>%
mutate(name = str_replace(name, “ [A-Z][A-Z]”, “”))
major_cities <-na.omit(major_cities)starbucks_dat$City <-as.character(starbucks_dat$City)
starbucks_dat$Country.Subdivision <-as.character(starbucks_dat$Country.Subdivision)
# Get coordinates of US cities where more than 2 stores were opened from 2013–2016
starbucks_dat1 <- maps::us.cities %>%
mutate(city = str_replace(name, “ [A-Z][A-Z]”, “”)) %>%
inner_join(starbucks_dat, c(“city” = “City”, “country.etc” = “Country.Subdivision”)) %>%
group_by(city, country.etc) %>%
summarise(count = n(), long = Longitude[1], lat = Latitude[1]) %>%
ungroup() %>%
filter(count > 2)
mm <-data.frame(map_data(“usa”))# Create base layer of US map
plot_star_loc <- ggplot() +
geom_polygon(data = map_data(“usa”), aes(long, lat, group = group), fill = “#00704a”) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.line = element_blank(), axis.ticks = element_blank(),
panel.background = element_blank(), panel.border = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.position = “none”) +
coord_quickmap()
plot_star_loc# Plot the store locations and show major cities
plot_star_loc +
geom_text_repel(data = major_cities, aes(long, lat, label = name), size = 5) +
geom_point(data = starbucks_dat, aes(Longitude, Latitude), alpha = 0.3, color = “#d42426”) +
ggtitle(“Starbucks store growth 2013–2016”)
# Plot deaths and show most deadly cities
plot_star_loc +
geom_text_repel(data = starbucks_dat1, aes(long, lat, label = city), size = 4) +
geom_point(data = starbucks_dat, aes(Longitude, Latitude), alpha = 0.3, color = “#d42426”) +
ggtitle(“Starbucks Store Openings since 2013”)
#Animation — Showing the growth of stores in 2016
saveGIF(for (i in 0:1000) {
starbucks_time <- starbucks_dat %>%
filter(First.Seen >= ymd(“2016–01–01”) + i)

starbucks_cities <- starbucks_dat1 %>%
left_join(starbucks_time, c(“city” = “City”, “country.etc” = “Country.Subdivision”)) %>%
group_by(city, country.etc) %>%
summarise(count = n(), long = Longitude[1], lat = Latitude[1]) %>%
ungroup() %>%
mutate(alph = count > 3)
print(plot_star_loc +
geom_text_repel(data = starbucks_cities, size = 5, segment.alpha = 0,
aes(long, lat, label = city, alpha = factor(alph))) +
scale_alpha_manual(values = c(0, 1)) +
geom_point(data = starbucks_time, aes(Longitude, Latitude), alpha = 0.4, color = “#d42426”) +
ggtitle(paste0(“Starbucks Stores as first seen on “, ymd(“2016–01–01”) + i)))

}, “starbucks_locations2016.gif”, interval = 0.004, ani.width = 800, ani.height = 530)

--

--