Create Easy US Maps in R - Thanksgiving Edition
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.
R 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"
Use gTrendsR Packge To 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")
Use FIPs Function to 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)
Use Plot_Usmap to 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")
Use Plot_Usmap to 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")
Next Steps
If you liked this tutorial, you may be interested in a few other tutorials on this blog. Please check out:
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)