Overview

Have you ever wondered how far the SPAM® Museum is from large cities in the United States? Of course you have! Well, wonder no longer; the map below will sate your curiosity.

Process

I acquired population and geolocation data from the World Cities Database and pared down the data set to include only the 100 most populous cities in the United States. I identified the latitude and longitude of the SPAM® Museum from the URL in Google Maps.

Using this information and a function in the geosphere package I calculated the distance from each city to the SPAM® Museum in both kilometers and miles. Table 1 presents the first five cities:

Table 1: Geolocation of Highest Population Cities in the United States and Distance to the SPAM® Museum
city lat lng km miles
New York 40.6943 -73.9249 1602 996
Los Angeles 34.1139 -118.4068 2434 1512
Chicago 41.8373 -87.6861 478 297
Miami 25.7840 -80.2102 2302 1430
Dallas 32.7937 -96.7662 1255 780

Map

I found the SPAM® Museum logo via Google Images and used it completely without permission. Cities are color-coded based on distance in miles:

Code Appendix

Setup code (libraries, data cleaning, etc.):

library(tidyverse) # dplyr etc.
library(knitr) # for kable
library(leaflet) # for making the map
library(geosphere) # for calculating distances

SPAMlnglat <- c(-92.9746576, 43.6693306) # SPAM Museum longitude and latitude

data <- read.csv("worldcities.csv")

data2 <- data %>% 
  select(c(city_ascii, lat, lng, country, population)) %>% # keep the desired variables
  filter(country == "United States") %>% # keep only United States rows
  arrange(-population) %>% # arrange by population in descending order
  slice(1:100) %>% # keep the top 100
  select(c(city = city_ascii, lat, lng)) # retain only necessary columns; rename the city variable

datafordistance <- as.matrix(select(data2, c(lng, lat))) # make a matrix of longitude and latitude values for the distance function

km <- distm(datafordistance, SPAMlnglat, fun = distHaversine)/1000 # run the distance function; divide by 1000 to get kilometers

data3 <- data2 %>%
  mutate(km) %>% # add the distance in kilometers to the tibble
  mutate(miles = km * 0.62137) # convert kilometers to miles 

data4 <- data3 %>%
  mutate_at(4:5, round, 0) %>% # round the distances to nearest whole number
  mutate(color = case_when( # add the color column
    miles < 251 ~ "green",
    miles < 501 ~ "blue",
    miles < 1001 ~ "orange",
    miles > 1000 ~ "red"
  ))

kable(head(data4[1:5], 5), caption = "Table 1: Geolocation of Highest Population Cities in the United States and Distance to the SPAM<sup>&reg;</sup> Museum") # make a table but don't show the color column

Map code

SPAMicon <- makeIcon(iconUrl = "C:/Users/djbauer/Dropbox/Coursera/09 - dataproducts/spam_museum_logo.png", iconWidth = 56, iconHeight = 56) # make the SPAM Museum icon
SPAMmuseum <- data.frame(lng = -92.9746576, lat = 43.6693306) # SPAM Museum geolocation

map <- data4 %>% # make the map!
  leaflet(width = 900) %>%
  addTiles() %>%
  addMarkers(data = SPAMmuseum, icon = SPAMicon, popup = "Visit the SPAM<sup>&reg;</sup> Museum in historic Austin, Minnesota.") %>%
  addCircleMarkers(popup = paste(data4$city, "is only", data4$miles, "miles (or", data4$km, "km) to the SPAM<sup>&reg;</sup> Museum!"), color = data4$color) %>%
  addLegend(labels = c("Day Trip", "Overnight Excursion", "Weekend Getaway", "Road Trip"), colors = c("green", "blue", "orange", "red"))

map # display the map