Statebins heatmaps in R

Recently I came across this blog post on making heatmaps in R for the US states which are more heat than map. The idea is simple, the plot basic, the purpose straightforward, utility apparent. The statebins package is convenient, but I felt like making one using base graphics instead of ggplot2, which statebins relies on, among other packages.


I also felt like moving Alaska. Considering that the map is an efficient and effective blend of and compromise between geographical accuracy and easy reading, useful in cases where the geography itself is only of peripheral interest at best, it is rather silly to continue forcing Alaska into the bottom left corner. If geographical accuracy is critical, (1) no reason to make this heatmap and (2) you couldn’t place it in the given extent, hence the common choice of sticking it in the bottom left corner along with Hawaii. On the other hand, if geographical accuracy is not important at all, well, by definition it doesn’t matter where you put it. In fact just make a table! (This brings up another niche for such a plot. That is, when you don’t need a plot at all, and a table would do, or maybe, just maybe, a bar graph, but your client is insisting up and down that they want more of a “pretty picture”. In my everyday context, this is almost inevitably a map. One must pick their battles.).

With this type of plot, there is no reason to stick to the convention anymore. So I moved Alaska to the top left. I would move Hawaii too if I knew for sure of a better positioning, but not having lived there I don’t have a similar personal stake in its placement. As for the placement of the remaining 48 states, I am confident that whoever laid them out initially found what seemed to be the best closest packing arrangement for the lattice.

All that aside, there’s really nothing to this. I borrowed the matrix of state grid coordinates from the statebins package using statebins:::state_coords since it is not exported from the package namespace, made an empty matrix, filled the 50 state locations with data, and plotted it with image. Some matrix rotation was required as a result. Other than that it was just a matter over overlaying text and borders using text and rect multiple times (double border effect).

This entry was posted by Matt Leonawicz.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: