Easy US Maps in R – Thanksgiving Edition

[This article was first published on Blog - Little Miss Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve created a number of blog tutorials on the subject of creating maps in R. Specifically, I’ve shared blogs on ggmap basics, icon maps with ggmap and more.

Today, I’d like to share the package ‘usmap’ which enables incredibly easy and fast creation of US maps in R.

In honor of US Thanksgiving tomorrow, I’m going to make this blog Thanksgiving themed! In this tutorial, we will use the gTrendsR package to pull US Google search results on the keyword “thanksgiving” and plot the popularity by state.

Set Up

Identify which R packages we would like loaded. Check to see if the packages are installed, install if needed and load packages. I found this code to efficiently meet the need on Vikram Baliga’s blog.

#Specify the packages of interest
packages = c("gtrendsR","tidyverse","usmap")

#Use this function to check if each package is on the local machine
#if a package is installed, it will be loaded
#if any are not, the missing package(s) will be installed and loaded
package.check <- lapply(packages, FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
        install.packages(x, dependencies = TRUE)
        library(x, character.only = TRUE)
    }

Set a specific color variable for the upcoming maps

orange <- "#C9592E"

Get Thanksgiving Trends

Use the gTrendsR package to obtain the query trends for thanksgiving in the US for the past 24 hours

thanksgiving <- gtrends("thanksgiving",geo = "US", time = "now 1-d")

Gather Interest by State

Select the data frame which holds the keyword interest by state and then convert the names of the states to FIPS codes (2 characters for the state, 5 characters for the county) using the fips() function.

thanksgivingStates <- thanksgiving$interest_by_region
thanksgivingStates$fips <-fips(thanksgivingStates$location)

Plot “thanksgiving” interest by state

Create a US heatmap using the usmap package to plot Google search popularity for the keyword "thanksgiving". We can clearly see from the graph below, how excited the east coast has been for thanksgiving in the past 24 hours. New Jersey is the winning state with Massachusetts, Connecticut,
Rhode Island and New York following closely behind!

plot_usmap(data = thanksgivingStates, values = "hits",  color = orange, labels=FALSE) + 
  scale_fill_continuous( low = "white", high = orange, 
                         name = "Popularity", label = scales::comma
  ) + 
  theme(legend.position = "right") + 
  theme(panel.background = element_rect(colour = "black")) + 
  labs(title = "Popularity of Thanksgiving Google Search by State", caption = "Source: @littlemissdata")
unnamed-chunk-4-1.png

Plot “thanksgiving” interest for select states

Drill in on the seemingly most popular regions using the "include" parameter in the plot_usmap() function. Regional divisions can be found in the docs here.

plot_usmap(data = thanksgivingStates, values = "hits", include =  c(.south_atlantic, .mid_atlantic, .new_england ), color = orange, labels=TRUE) + 
  scale_fill_continuous( low = "white", high = orange, 
                         name = "Popularity", label = scales::comma
  ) + 
  theme(legend.position = "right") + 
  theme(panel.background = element_rect(colour = "black")) + 
  labs(title = "US East Coast Popularity of Thanksgiving Google Search", caption = "Source: @littlemissdata")
rplot_usmap.png

Thank You

Thank you for following along on our Thanksgiving themed map tutorial. Please comment below if you enjoyed this blog, have questions, or would like to see something different in the future.  Note that the full code is available on my  github repo.  

If you have trouble downloading the files or cloning the repo from github, please go to the main page of the repo and select "Clone or Download" and then "Download Zip". Alternatively or you can execute the following R commands to download the whole repo through R

use_course("https://github.com/lgellis/MiscTutorial/archive)

To leave a comment for the author, please follow the link and comment on their blog: Blog - Little Miss Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)