Build a Simple Heatmap Using R

Heatmaps have historically been given a bad name in the web analytics industry but they can be a powerful tool for data visualization.
noun ˈhētmap
1 a : A heat map is a graphical representation of data where the individual values contained in a matrix are represented as colors.
In this blog post, I’m going to show you how easy it is to create a simple heatmap using R.
NOTE: This tutorial assumes you already have R installed. We will also be using RStudio in this example.

1. Import Your Dataset

The data for this example are the total player stats to date (current as of 2.5.2013) for the Utah Jazz. The data is already prepared for import and is available here.
Load the data into R using read.csv:
jazz <- read.csv("", sep=",")

If you did this correctly, a new variable called ‘jazz’ will appear in your Workspace as shown below.
Supercharging Your Optimization Practice With Satellite


2. Sort the Data

The dataset is currently shorted by Total Points scored but let’s say we wanted to sort it by Games Played instead. You can sort the data in any fashion you see fit, simply reference the Column Name you wish to sort by.
Supercharging Your Optimization Practice With Satellite
jazz <- jazz[order(jazz$G),]

Supercharging Your Optimization Practice With Satellite

3. Massage the Data

The file has already been prepared with the appropriate column names however our heatmap is going to make a lot more sense if we use the Player name to name the rows rather than a number.
row.names(jazz) <- jazz$Player

Now that we have used the Player name to name the rows, we can get rid of it from our Dataset.

Note: You can simply reuse the ‘jazz’ variable for this but for safety, I’m placing the results in a new variable named ‘jazz1’


4. Create a Data Matrix

When we imported the data, it was imported it a dataframe variable. In order to use the built in heatmap function, we need to convert our dataframe to a datamatrix.
Remember, we put our massaged dataframe into a new variable named ‘jazz1’.
jazz_matrix <- data.matrix(jazz1)


5. Generate the Heatmap

Now for the fun part, this one line of code will generate the heatmap visual display.
jazz_heatmap <- heatmap(jazz_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(3,3))

and….voila…we have a heatmap.


6. Make It Pop With Color

The pastel colors in the heatmap shown above are from the default color pallet (cm.colors) however you can colorize your heatmap in anyway you want. I like the out-of-the-box heatmap ready pallet (heat.colors) so let’s apply that now.
jazz_heatmap <- heatmap(jazz_matrix, Rowv=NA, Colv=NA, col = heat.colors(256), scale="column", margins=c(3,3))

Awesome!!! But wait, the players at the bottom of the chart seem to be warmer than the players at the top. This isn’t logically matching up to my expectations of red showing the most density (points, rebounds, etc.)
We can fix this. Let’s reverse the color pallet.
color = rev(heat.colors(256))
Now, rerun the heatmap with our new color pallet.
jazz_heatmap <- heatmap(jazz_matrix, Rowv=NA, Colv=NA, col = color, scale="column", margins=c(3,3))

From here, you can pull it into your favorite photo editor and add a title and other descriptors.

I hope you found this little tutorial helpful and most of all, I hope this makes you think about expanding the frameworks you use when you are building your next analysis.