How to Create a US Heatmap in R

Creating a simple US map in R can be done in a number of ways. Two popular packages for this type of project are ggplot2 and plotly. In this case, I used plotly.

The data for my map is a list of US state codes (NE, IL, MA, CA, etc.). A second variable gives a count of how many players the Nebraska football team is targeting in each state. In order to follow my example with your own data, you will need to have the state code variable and some numeric variable to map it against.

Once you have your data in a table and are ready to use it, create the following styling options for the map, which we will apply later:

mapDetails <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')

As you may have guessed, “scope” determines the type of map, in this case a map of the USA. We will also determine here what to do with lakes and how to color them.

usaMap <- plot_geo(X2018targets, locationmode = 'USA-states') %>%
z = X2018targets$Targets, locations = X2018targets$`State Code`,
color = X2018targets$Targets, colors = 'Blues'
) %>%
colorbar(title = "Targets") %>%
title = '2018 Nebraska Football Targets by State (February 2018)',
geo = g

The code above connects my data to the map and allows me to modify text within the plot area. My data frame is called “X2018targets,” so you’ll need to replace this with your data frame name. You’ll also need to set “z” to your numeric data and “locations” to your state code variable.

When you’re finished, simply type “usaMap” and hit enter to see your plot appear (I use R Studio, by the way, assuming you likely do as well). If you have any trouble or questions, let me know in the comments.

Data Science Course Recommendation: Udemy Data Science A-Z

I want to give some props to a course I recently took online at The course is called Data Science A-Z and is taught by someone by the name of Kirill Eremenko.

First, I just want to stress that I am not being paid for this endorsement in any way. Just want to share my review with you all.

The price was right at a mere $10. Not sure if that was a short-term promotional price or how long it will last, but it’s well worth it — even as a refresher.

There are three sections: data visualization with Tableau, Statistics/Modeling, and Data Preparation. The sections are not dependent on each other and can be taken in any order, which adds a nice element of flexibility to the whole thing.

As you probably know, there are countless courses out there but what I appreciate about this one is that it was easy to digest if you have any sort of background in these areas and it explains not only how to approach these disciplines but why you are doing them at all.

During the course, I was also introduced to a great free statistical program called Gretl. You can download it here. If you have used SPSS or SAS, you’ll pick it up in no time at all.

Find out more here:

I also really like Data Camp, but there is a monthly fee associated with membership. I believe it’s somewhere between $20-30/month.

Thanks for reading.

Some Basic Thoughts on Data Visualization

Data visualization is the art and science of clearly communicating data in a way that is easily digestible to the end user. It goes without saying that there is so much data available now. But effectively making sense of that data is critically important. That’s when data really becomes useful information.

Excel can do some things. You are all likely familiar with their built-in line graphs, pie charts and other visualizations. But it is limited in the amount of data you can work with and the customization of visuals. For many scenarios it just fine.

But there are times when one might have more data to work with or perhaps does not have a great way to get data to Excel in the first place. There are tools out there like Tableau that can connect to APIs (or you can import the data) and have a rich library of visualizations and ways to customize them. Tableau in particular has a free “Public” version available however the output will be placed on their website for anyone to see. If you are a business or agency that wants to keep your data private, a paid version is out there as well.

Recently Google announced their version of Tableau — Data Studio. There is a free version here as well and I think you can choose to keep your data private. However, at least as of June 2016, it only connects to other Google platforms. If you want to pull other data into the user interface, it is possible, but you first need to get it into Google Sheets or Big Query, which is a Google SQL database more or less. Big Query is its own topic for another day.

I have only scratched the surface on what is out there. For R users, you can download and work with libraries like ggplot2; QlikView is another Tableau-esque tool; and on and on. Check out this website for a few more examples, which are cleverly broken out tools for developers and non-developers: