Purpose

The purpose of this exercise was to explore Alcohol Sales data in the State of Iowa for 2018 to see which brands were most popular, and which stores were selling most of the Top 2 selling brands. Going deeper, I was curious if some of these top performing stores were located near colleges. Some colleges tend to be large consumers of alcohol.

Research Questions

  1. Which counties are the top 10 in sales?
  2. What are the top 10 most valuable products in terms of sales?
  3. What are the most popular liqour brands?
  4. What is the spatial distirubtion of top selling locations of the top 2 selling brands?
  5. Are these stores located near college campuses?

Contents

This report will be organized in the following sections: 1. Data Cleaning and Pre-Processin 2. Exploratory Data Analysis (EDA) 3. Mapping

Data

Iowa Liquor Sales data was obtained from Kaggle. A more current version of the data can be obtained directly from the State of Iowa. This will provide more context and explanations of the Metadata.

1. Data Cleaning and Pre-Processing

# packages --------------------------------------------------------------
library(tidyverse)
library(lubridate)
library(Hmisc)

Bring in the data

retail_dataimport <- read_csv("./Iowa_Liquor_Sales.csv")

Create a fresh copy of the data so it can be reverted back to easily. Get a sense of the variables in the dataset and their structure.

retail <- retail_dataimport

glimpse(retail)
## Observations: 12,591,077
## Variables: 24
## $ `Invoice/Item Number`   <chr> "S29198800001", "S29195400002", "S29050300001…
## $ Date                    <chr> "11/20/2015", "11/21/2015", "11/16/2015", "11…
## $ `Store Number`          <dbl> 2191, 2205, 3549, 2513, 3942, 3650, 2538, 394…
## $ `Store Name`            <chr> "Keokuk Spirits", "Ding's Honk And Holler", "…
## $ Address                 <chr> "1013 MAIN", "900 E WASHINGTON", "1414 48TH S…
## $ City                    <chr> "KEOKUK", "CLARINDA", "FORT MADISON", "IOWA C…
## $ `Zip Code`              <chr> "52632", "51632", "52627", "52240", "52342", …
## $ `Store Location`        <chr> "1013 MAIN\r\nKEOKUK 52632\r\n(40.39978, -91.…
## $ `County Number`         <chr> "56", "73", "56", "52", "86", "47", "07", "86…
## $ County                  <chr> "Lee", "Page", "Lee", "Johnson", "Tama", "Ida…
## $ Category                <dbl> NA, NA, NA, NA, NA, NA, 1701100, NA, 1701100,…
## $ `Category Name`         <chr> NA, NA, NA, NA, NA, NA, "DECANTERS & SPECIALT…
## $ `Vendor Number`         <dbl> 255, 255, 130, 65, 130, 65, 962, 65, 65, 130,…
## $ `Vendor Name`           <chr> "Wilson Daniels Ltd.", "Wilson Daniels Ltd.",…
## $ `Item Number`           <dbl> 297, 297, 249, 237, 249, 237, 238, 237, 173, …
## $ `Item Description`      <chr> "Templeton Rye w/Flask", "Templeton Rye w/Fla…
## $ Pack                    <dbl> 6, 6, 20, 3, 20, 3, 6, 3, 12, 20, 20, 3, 3, 6…
## $ `Bottle Volume (ml)`    <dbl> 750, 750, 150, 1750, 150, 1750, 1500, 1750, 7…
## $ `State Bottle Cost`     <chr> "$18.09", "$18.09", "$6.40", "$35.55", "$6.40…
## $ `State Bottle Retail`   <chr> "$27.14", "$27.14", "$9.60", "$53.34", "$9.60…
## $ `Bottles Sold`          <dbl> 6, 12, 2, 3, 2, 1, 6, 2, 4, 2, 20, 2, 3, 1, 2…
## $ `Sale (Dollars)`        <chr> "$162.84", "$325.68", "$19.20", "$160.02", "$…
## $ `Volume Sold (Liters)`  <dbl> 4.50, 9.00, 0.30, 5.25, 0.30, 1.75, 9.00, 3.5…
## $ `Volume Sold (Gallons)` <dbl> 1.19, 2.38, 0.08, 1.39, 0.08, 0.46, 2.38, 0.9…

Cleaning - Check for missing data.

Identify which columns have missing values and how many.

retail %>% 
  map(.,~sum(is.na(.)))
## $`Invoice/Item Number`
## [1] 0
## 
## $Date
## [1] 0
## 
## $`Store Number`
## [1] 0
## 
## $`Store Name`
## [1] 0
## 
## $Address
## [1] 2376
## 
## $City
## [1] 2375
## 
## $`Zip Code`
## [1] 2420
## 
## $`Store Location`
## [1] 2375
## 
## $`County Number`
## [1] 79178
## 
## $County
## [1] 79178
## 
## $Category
## [1] 8020
## 
## $`Category Name`
## [1] 16086
## 
## $`Vendor Number`
## [1] 3
## 
## $`Vendor Name`
## [1] 1
## 
## $`Item Number`
## [1] 0
## 
## $`Item Description`
## [1] 0
## 
## $Pack
## [1] 0
## 
## $`Bottle Volume (ml)`
## [1] 0
## 
## $`State Bottle Cost`
## [1] 10
## 
## $`State Bottle Retail`
## [1] 10
## 
## $`Bottles Sold`
## [1] 0
## 
## $`Sale (Dollars)`
## [1] 10
## 
## $`Volume Sold (Liters)`
## [1] 0
## 
## $`Volume Sold (Gallons)`
## [1] 0
#We ignore the entire row(ie observation), if any column has a missing value
retail <- retail[complete.cases(retail), ]

# Check to see if we have any NA's

retail %>% 
  map(., ~sum(is.na(.)))
## $`Invoice/Item Number`
## [1] 0
## 
## $Date
## [1] 0
## 
## $`Store Number`
## [1] 0
## 
## $`Store Name`
## [1] 0
## 
## $Address
## [1] 0
## 
## $City
## [1] 0
## 
## $`Zip Code`
## [1] 0
## 
## $`Store Location`
## [1] 0
## 
## $`County Number`
## [1] 0
## 
## $County
## [1] 0
## 
## $Category
## [1] 0
## 
## $`Category Name`
## [1] 0
## 
## $`Vendor Number`
## [1] 0
## 
## $`Vendor Name`
## [1] 0
## 
## $`Item Number`
## [1] 0
## 
## $`Item Description`
## [1] 0
## 
## $Pack
## [1] 0
## 
## $`Bottle Volume (ml)`
## [1] 0
## 
## $`State Bottle Cost`
## [1] 0
## 
## $`State Bottle Retail`
## [1] 0
## 
## $`Bottles Sold`
## [1] 0
## 
## $`Sale (Dollars)`
## [1] 0
## 
## $`Volume Sold (Liters)`
## [1] 0
## 
## $`Volume Sold (Gallons)`
## [1] 0
## Boom yes! We have considerably reduced misssing values.

Inspect the dataset

glimpse(retail)
## Observations: 12,495,974
## Variables: 24
## $ `Invoice/Item Number`   <chr> "S28865700001", "S29339300091", "S28866900001…
## $ Date                    <chr> "11/09/2015", "11/30/2015", "11/11/2015", "11…
## $ `Store Number`          <dbl> 2538, 2662, 3650, 3723, 2642, 3842, 2539, 460…
## $ `Store Name`            <chr> "Hy-Vee Food Store #3 / Waterloo", "Hy-Vee Wi…
## $ Address                 <chr> "1422 FLAMMANG DR", "522 MULBERRY, SUITE A", …
## $ City                    <chr> "WATERLOO", "MUSCATINE", "HOLSTEIN", "ONAWA",…
## $ `Zip Code`              <chr> "50702", "52761", "51025", "51040", "50219", …
## $ `Store Location`        <chr> "1422 FLAMMANG DR\r\nWATERLOO 50702\r\n(42.45…
## $ `County Number`         <chr> "07", "70", "47", "67", "63", "55", "42", "50…
## $ County                  <chr> "Black Hawk", "Muscatine", "Ida", "Monona", "…
## $ Category                <dbl> 1701100, 1701100, 1701100, 1081200, 1701100, …
## $ `Category Name`         <chr> "DECANTERS & SPECIALTY PACKAGES", "DECANTERS …
## $ `Vendor Number`         <dbl> 962, 65, 962, 305, 962, 962, 962, 962, 65, 96…
## $ `Vendor Name`           <chr> "Duggan's Distillers Products Corp", "Jim Bea…
## $ `Item Number`           <dbl> 238, 173, 238, 258, 238, 238, 238, 238, 173, …
## $ `Item Description`      <chr> "Forbidden Secret Coffee Pack", "Laphroaig w/…
## $ Pack                    <dbl> 6, 12, 6, 1, 6, 6, 6, 6, 12, 6, 1, 1, 12, 1, …
## $ `Bottle Volume (ml)`    <dbl> 1500, 750, 1500, 6000, 1500, 1500, 1500, 1500…
## $ `State Bottle Cost`     <chr> "$11.62", "$19.58", "$11.62", "$99.00", "$11.…
## $ `State Bottle Retail`   <chr> "$17.43", "$29.37", "$17.43", "$148.50", "$17…
## $ `Bottles Sold`          <dbl> 6, 4, 1, 1, 6, 3, 6, 2, 36, 12, 1, 1, 1, 2, 1…
## $ `Sale (Dollars)`        <chr> "$104.58", "$117.48", "$17.43", "$148.50", "$…
## $ `Volume Sold (Liters)`  <dbl> 9.00, 3.00, 1.50, 6.00, 9.00, 4.50, 9.00, 3.0…
## $ `Volume Sold (Gallons)` <dbl> 2.38, 0.79, 0.40, 1.59, 2.38, 1.19, 2.38, 0.7…

We need to change Date from “chr” to “mdy”. Then change Item_Description, County, Invoice Number, Category Name, to a Factor for analysis. We will also need to make some minor manipulations using gsub to remove underscores and slashes in variable names.

#Replace spaces in variable names with underscores.
names(retail) <- gsub("/", "_", names(retail))
names(retail) <- gsub(" ", "_", names(retail))

# Also remove the slashes between dates, they make formatting difficult.
retail$Date <- gsub("/","",retail$Date)

names(retail) <- gsub("[()]", "", names(retail))
retail$Sale_Dollars <- as.numeric(gsub("\\$", "", retail$Sale_Dollars))
retail$State_Bottle_Cost <- as.numeric(gsub("\\$", "", retail$State_Bottle_Cost))
retail$State_Bottle_Retail <- as.numeric(gsub("\\$", "", retail$State_Bottle_Retail))


# The last step is to convert county names to uppercase. They were recorded incorrectly with upper and lowercase.
retail <- retail %>% mutate_each(funs(toupper),County)
## Warning: funs() is soft deprecated as of dplyr 0.8.0
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once per session.
# Coerce more variables into factor form
retail_cleaned <- retail %>%
  mutate(Date = mdy(Date)) %>% #coerces InvoiceDate in a Date Time format
  mutate(Item_Description = factor(Item_Description, levels = unique(Item_Description))) %>% 
#Coerces Description as a factor with each item as individual level of a factor
  
  
  mutate(County = factor(County, levels = unique(County)))%>%
  mutate(Invoice_Item_Number = factor(Invoice_Item_Number, levels = unique(Invoice_Item_Number))) %>%
  mutate(Category_Name = factor(Category_Name, levels = unique(Category_Name)))

Inspect the data again to examine new variables and changed data types.

glimpse(retail_cleaned)
## Observations: 12,495,974
## Variables: 24
## $ Invoice_Item_Number <fct> S28865700001, S29339300091, S28866900001, S291343…
## $ Date                <date> 2015-11-09, 2015-11-30, 2015-11-11, 2015-11-18, …
## $ Store_Number        <dbl> 2538, 2662, 3650, 3723, 2642, 3842, 2539, 4604, 2…
## $ Store_Name          <chr> "Hy-Vee Food Store #3 / Waterloo", "Hy-Vee Wine &…
## $ Address             <chr> "1422 FLAMMANG DR", "522 MULBERRY, SUITE A", "118…
## $ City                <chr> "WATERLOO", "MUSCATINE", "HOLSTEIN", "ONAWA", "PE…
## $ Zip_Code            <chr> "50702", "52761", "51025", "51040", "50219", "505…
## $ Store_Location      <chr> "1422 FLAMMANG DR\r\nWATERLOO 50702\r\n(42.459938…
## $ County_Number       <chr> "07", "70", "47", "67", "63", "55", "42", "50", "…
## $ County              <fct> BLACK HAWK, MUSCATINE, IDA, MONONA, MARION, KOSSU…
## $ Category            <dbl> 1701100, 1701100, 1701100, 1081200, 1701100, 1701…
## $ Category_Name       <fct> DECANTERS & SPECIALTY PACKAGES, DECANTERS & SPECI…
## $ Vendor_Number       <dbl> 962, 65, 962, 305, 962, 962, 962, 962, 65, 962, 3…
## $ Vendor_Name         <chr> "Duggan's Distillers Products Corp", "Jim Beam Br…
## $ Item_Number         <dbl> 238, 173, 238, 258, 238, 238, 238, 238, 173, 238,…
## $ Item_Description    <fct> Forbidden Secret Coffee Pack, Laphroaig w/ Whiske…
## $ Pack                <dbl> 6, 12, 6, 1, 6, 6, 6, 6, 12, 6, 1, 1, 12, 1, 6, 1…
## $ Bottle_Volume_ml    <dbl> 1500, 750, 1500, 6000, 1500, 1500, 1500, 1500, 75…
## $ State_Bottle_Cost   <dbl> 11.62, 19.58, 11.62, 99.00, 11.62, 11.62, 11.62, …
## $ State_Bottle_Retail <dbl> 17.43, 29.37, 17.43, 148.50, 17.43, 17.43, 17.43,…
## $ Bottles_Sold        <dbl> 6, 4, 1, 1, 6, 3, 6, 2, 36, 12, 1, 1, 1, 2, 1, 2,…
## $ Sale_Dollars        <dbl> 104.58, 117.48, 17.43, 148.50, 104.58, 52.29, 104…
## $ Volume_Sold_Liters  <dbl> 9.00, 3.00, 1.50, 6.00, 9.00, 4.50, 9.00, 3.00, 2…
## $ Volume_Sold_Gallons <dbl> 2.38, 0.79, 0.40, 1.59, 2.38, 1.19, 2.38, 0.79, 7…

The data has been satisfactorily cleaned and processed. Save as an .Rdata file to make it easy to import for the EDA.

save(retail_cleaned, file = "retail_cleaned.RData")

2. Exploratory Data Analysis (EDA) and Visualization

Questions

Now that the data has been cleaned and process let’s explore and visualize some of the trends we see in the data. Specifically, we should investigate:

  1. Which counties are the top 10 in sales?
  2. What are the top 10 most valuable products in terms of sales?
  3. What are the most popular liquor brands?
# packages --------------------------------------------------------------

library(forcats)

Load the data and inpsect the data.

load("./retail_cleaned.RData")

Exploratory Data Analysis (EDA)

Summarize the data according to the counties that have the most sales and identify the Top 10 Best Selling Counties.

# Summarize the data by the counties that have the most sales.
county_mostsales<-retail_cleaned %>% 
  group_by(County) %>% 
  summarise(countywise_sales = sum(Sale_Dollars)/1000000) %>% 
  arrange(desc(countywise_sales))

## Identify Top 10 Counties
by_top10_counties <- county_mostsales %>% 
  top_n(n = 10, wt = countywise_sales)

Visualize the top 10 counties as per total sales.

by_top10_counties %>% 
    mutate(County = fct_reorder(County, countywise_sales)) %>% 
    ggplot(aes(County, countywise_sales))+
    geom_bar(stat = "identity",fill = "green4")+
  theme_minimal()+
  ggtitle('Top 10 Liquor Selling Counties')+
  coord_flip()+
  ylab('Total Sales ($M)')+
  theme(plot.title = element_text(hjust = 0.5))

So Polk county has the most sales by an order of magnitude. Polk County contains the state capital, Des Moines, and is the most populous county in Iowa according to World Population Review. Let’s remove Polk county and then look at the rest of the data in the top 10 counties.

county_mostsales_without_polk <-  county_mostsales[-1,]  
  
by_top10_counties_without_polk <- county_mostsales_without_polk %>% 
  top_n(n=10, wt = countywise_sales )

by_top10_counties_without_polk %>% 
  mutate(County = fct_reorder(County, countywise_sales)) %>% 
  ggplot(aes(County, countywise_sales))+
  geom_bar(stat = "identity",fill = "green4")+
  theme_minimal()+
  ggtitle('Top 10 Liquor Selling Counties')+
  coord_flip()+
  ylab('Total Sales ($M)')+
  theme(plot.title = element_text(hjust = 0.5))

So without Polk county, Linn county is the second leader in Liquor sales in the state of Iowa with $125M in sales for the reporting period. That is a lot of alcohol!

What are the most valued products? What are the Top 10 most valuable products?

mostvalued_product <- retail_cleaned %>% 
  group_by(Item_Description) %>%
  summarise(value_of_product = sum(Sale_Dollars)/1000000) %>% 
  arrange(desc(value_of_product)) 

top10_mostvalued_products <- mostvalued_product %>% 
  top_n(n = 10, wt = value_of_product)

The categorical variables Item_Description does not have an inherent order. Reorder it as an increasing count. Visualize Top 10 Best Selling Products.

top10_mostvalued_products %>% 
  mutate(Item_Description = fct_reorder(Item_Description, value_of_product)) %>% 
  
  ggplot(aes(Item_Description, value_of_product))+
   
    geom_bar(stat = "identity",fill = "green4")+
    theme_minimal()+
    ggtitle('Top 10 Most Popular Products')+
    coord_flip()+
    ylab('Total Sales ($M)')+
    theme(plot.title = element_text(hjust = 0.5))

Interesetingly Canadian Whisky is the most valued Liquor product in America’s Heartland. Followed by Jack Daniels, then Captain Morgan.

What are the best selling liquor brands in Iowa? What are the top 10 best brands?

# What are the best selling liquor brands in Iowa

mostsold_brand <- retail_cleaned %>% 
  group_by(Item_Description) %>% 
  summarise(total_units_sold = sum(Bottles_Sold)/1000000) %>% 
  arrange(desc(total_units_sold))

top10_mostsold_brand <- mostsold_brand %>% 
  top_n(n=10,wt=total_units_sold)

most_sold<- top10_mostsold_brand %>% 
  mutate(Item_Description = fct_reorder(Item_Description, total_units_sold)) %>% 
  ggplot(aes(Item_Description, total_units_sold))+
  geom_bar(stat = "identity",fill = "green4")+
  theme_minimal()+
  ggtitle('Top 10 Most Sold Brands')+
  coord_flip()+
  ylab('Total Sales ($M)')+
  theme(plot.title = element_text(hjust = 0.5))

most_sold

Black Velvet again. It is the most sold and accounts for the most value followed by Hawkeye Vodka (This seems logical given their passion for college football) and Captain Morgan Spiced Rum.

Which stores sold the most Black Velvet and Hawkeye Vodka? First we must remove empty spaces in Item_Description names.

blackvelvet <- retail_cleaned %>% 
   filter(Item_Description == "Black Velvet") %>% 
  group_by(Store_Number) %>% 
  summarise(allSold = sum(Bottles_Sold),
            totalSales = sum(Sale_Dollars),
            totalvolGal = sum(Volume_Sold_Gallons)) %>% 
  arrange(desc(allSold)) %>%
  top_n(n=10, wt = allSold)

Use store number for the join.Create data frame just with locations and pertinent summary information.

top10bvstores<- retail_cleaned %>% 
  select(Store_Number,Store_Name,Address,City,Zip_Code,Store_Location,
         County_Number,Category_Name,Vendor_Name,Item_Description) %>% 
  filter(Item_Description == "Black Velvet") %>% 
  arrange(desc(Store_Number)) %>% 
  distinct(Store_Number,.keep_all = TRUE)
  
bv_most_pop<- top10bvstores %>% 
  inner_join(blackvelvet,top10bvstores, by = "Store_Number")

Now find way to break out spatial coordinates to store locations. Turn string into data list, then dataframe.

bv_data_clean <- c("7205 MILLS CIVIC PKWY\nWEST DES MOINES 50266\n(41.561342, -93.806489)",
                  "305 AIRPORT RD\nAMES 50010\n(42.001123, -93.61365)",               
                  "210 EAST TOWER PARK DR\nWATERLOO 50702\n(42.456362, -92.352552)",
                  "4201 S. YORK ST.\nSIOUX CITY 51106\n(42.433711, -96.370146)",          
                  "1101 73RD STREET\nWINDSOR HEIGHTS 50311\n(41.599172, -93.718027)",     
                  "1610 OKOBOJI AVENUE\nMILFORD 51351\n(43.331525, -95.149955)",          
                  "2605 BLAIRS FERRY RD NE\nCEDAR RAPIDS 52402\n(42.034737, -91.679406)", 
                  "1511 2ND AVE NORTH\nFORT DODGE 50501\n(42.508344, -94.177165)",        
                  "3221 SE 14TH ST\nDES MOINES 50320\n(41.554101, -93.596754)",           
                  "1201 12TH AVE SW\nLEMARS 51031\n(42.778257, -96.18335)"               
                  )      
     
bv_data_clean <- as.data.frame(bv_data_clean)

Separate out lat and long and bind them to original Black Velvet Top 10 Dataset.Now write it to a .csv. Repeat process for Hawkeye Vodka.

bvdc_latlon <-bv_data_clean%>%
  mutate(bv_data_clean = gsub('\n', '', bv_data_clean)) %>%
  extract(bv_data_clean, into = c('address', 'lat', 'lon'), 
          regex = '(.*)\\((.*),\\s+(.*)\\)', convert = TRUE) %>% 
          bind_cols(bv_most_pop) %>% 
          write.csv(.,file = "bvdc_latlon.csv")

Continuing the same process for Hawkeye Vodka

hawkeye <- retail_cleaned %>% 
  filter(Item_Description == "Hawkeye Vodka") %>% 
  group_by(Store_Number) %>% 
  summarise(allSold = sum(Bottles_Sold),
            totalSales = sum(Sale_Dollars),
            totalvolGal = sum(Volume_Sold_Gallons)) %>% 
  arrange(desc(allSold)) %>%
  top_n(n=10, wt = allSold)


top10hvstores<- retail_cleaned %>% 
  select(Store_Number,Store_Name,Address,City,Zip_Code,Store_Location,
         County_Number,Category_Name,Vendor_Name,Item_Description) %>% 
  filter(Item_Description == "Hawkeye Vodka") %>% 
  arrange(desc(Store_Number)) %>% 
  distinct(Store_Number,.keep_all = TRUE)

hv_most_pop<- top10hvstores %>% 
  inner_join(hawkeye,top10hvstores, by = "Store_Number")

hv_data_clean <- c( "1501 MICHIGAN AVE\nDES MOINES 50314\n(41.605561, -93.613738)",          
                    "1373 PIERCE ST\nSIOUX CITY 51105\n(42.504732, -96.405013)",             
                    "507 W 19th St\nSIOUX CITY 51103\n(42.510535, -96.420193)",              
                    "7205 MILLS CIVIC PKWY\nWEST DES MOINES 50266\n(41.561342, -93.806489)", 
                    "1101 73RD STREET\nWINDSOR HEIGHTS 50311\n(41.599172, -93.718027)",      
                    "1511 2ND AVE NORTH\nFORT DODGE 50501\n(42.508344, -94.177165)",         
                    "3221 SE 14TH ST\nDES MOINES 50320\n(41.554101, -93.596754)",            
                    "4100 UNIVERSITY AVE\nDES MOINES 50311\n(41.600361, -93.673223)",        
                    "2001 BLAIRS FERRY ROAD NE\nCEDAR RAPIDS 52402\n(42.034799, -91.668909)",
                    "551 S ILLINOIS AVE\nMASON CITY 50401\n(43.14623, -93.17114)"           
                    )

hv_data_clean <- as.data.frame(hv_data_clean)

hvdc_latlon <-hv_data_clean%>%
  mutate(hv_data_clean = gsub('\n', '', hv_data_clean)) %>%
  extract(hv_data_clean, into = c('address', 'lat', 'lon'), 
          regex = '(.*)\\((.*),\\s+(.*)\\)', convert = TRUE) %>% 
  bind_cols(hv_most_pop) %>% 
  write.csv(.,file = "hvdc_latlon.csv")

Good. Now both datasets have been exported and can be imported for spatial manipulation in a subsequent analysis.

Mapping

Let’s start fresh by just plotting colleges first and calling that our base map. First we need to generate some icons for universities. Some pretty neat icons can be found at Font Awesome.

college_icons <- awesomeIcons(icon='university',
                              library = 'fa',
                              markerColor = 'orange')
# This is our base map
map<- leaflet() %>% 
      addProviderTiles("CartoDB")

# Let's save out one for college to making plotting easier later.

college_map <- map

college_map

So now we have our base map. Now let’s add the icons we made.The clusterOptions argument clusters icons into regions so the map is more readable.

college_map <- college_map %>% 

  addAwesomeMarkers(data=ipeds_iowa,icon =college_icons, 
                   popup = ~INSTNM,group='Schools',
                   clusterOptions = markerClusterOptions())

college_map

Create a color pallette and plot the map.

pal <- colorFactor(palette = c("blue","red"),
                   domain = c("Black Velvet", "Hawkeye Vodka"))

#Add markers for Top 10 Black Velvet and Hawkeye Vodka selling stores.
liquor_collegemap <- college_map %>%
                    addCircleMarkers(data = blackvelvet,radius = 2,
                     color= ~pal(Item_Description),
                     group = "Black Velvet",
                      label = ~Store_Name) %>% 
  
                  addCircleMarkers(data = hawkeye,radius = 2,
                   color= ~pal(Item_Description),
                   group = "Hawkeye Vodka",
                   label = ~Store_Name) %>%
  
  #Add the ability to toggle layers on/off to help with visual analysis.
                  addLayersControl(
                    overlayGroups = c("Black Velvet",
                                      "Hawkeye Vodka")) %>% 
  #Add a legend to the lower right-hand side of the map.
                  addLegend("bottomright", colors= c("blue", "red"), 
                    labels=c("Black Velvet", "Hawkeye")) %>% 
  #Add a measuring tool to inspect distance between stores and schools.
                    addMeasure()
liquor_collegemap         

Conclusions

From this mapping exercise we can refute our hypothesis that the best selling stores will be near college campuses. It appears the more likely explanation is that the best selling store tend to be located in larger population centers near major roads and receive more customer traffic. Use the map to explore spatial relationships and possible correlations between the top two selling brands (Hawkeye Vodka and Black Velvet) and other phenomena that may result in high sales of these two particular products.