2 min read

Create beautiful cartogram heatmaps with Statebins

Statebins

Statebins is a neat R package that allows for the easy creation of beautiful cartogram heatmaps in the style of http://bit.ly/statebins. The cartograms statebins generates offer a visually appealing and somewhat minimal alternative to a typical cartogram in that the states are uniform squares aligned in a grid. Statebins has been updated recently and currently depends on a development version of ggplot2 so the following command is necessary to install it.

devtools::install_github("hrbrmstr/statebins")

Packages neccessary for this analysis are:

library(statebins)
library(tidyverse)

In this analysis we will examine illiteracy rates as a percentage of total population. The data comes from the US State Facts and Figures dataset included in base R which was sourced from the U.S. Department of Commerce, Bureau of the Census (1977) Statistical Abstract of the United States. The first thing to do is load the data and prepare it for the statebins package.

data(state)

state_facts <- as.data.frame(state.x77) %>%
  mutate(state = rownames(.))

Finally, we can generate the cartogram heatmap by passing our data frame to the statebins function. I personally like the look of rounded corners so I’ll set round = TRUE

state_facts %>%
  statebins(value_col = "Illiteracy", round = TRUE, name = "% of Pop.") +
  labs(title = "Illiteracy Rate (1970)",
       caption = "Source: U.S. Department of Commerce, Bureau of the Census (1977)") +
  theme_statebins(legend_position = "right")