New Function: calculate_percent_change

To facilitate calculating the percent change between two “choroplethr-style” dataframes I created the function calculate_percent_change.

This function assumes that it is given two dataframes, each with a column called region and value. The dataframes are joined by the region column, and the percent change between their value columns is computed. The result is rounded to two decimal places.

calculate_percent_change itself returns a dataframe with region and value columns. This makes the result easy to map with choroplethr functions.

With the divergent scale, it’s clear where both the negative and positive outliers are.

Closing Notes

It has always been possible to use a divergent scale via choroplethr’s object-oriented system. Functions like state_choropleth exist to hide those details. That, in turn, allows beginners to create maps with minimal work. Adding support for divergent scales with the num_colors parameter is another step in that direction.

The only drawback is that the num_colors parameter is now overloaded:

num_colors=0 will use a divergent scale. As we’ve seen above, this is useful when the values are both positive and negative.

num_colors=1 will use a continuous scale. This is useful for detecting outliers.

Setting num_colors to values in [2, 7] will use that many quantiles. This is a useful catch-all for detecting patterns within the data.

At some point it might make sense for me to rethink choroplethr’s API for setting scales. But at this point I wanted to add the new functionality without breaking any existing code.