This post uses animated choropleths to visualize violent crime rates in different US States across 5 decades (1961-2010). Data are retrieved from Quandl. Animation of rCharts and googleVis based choropleths is done in the shiny server environment and ggplot2 based choropleths are animated using the animation package. [Even though using shiny for ggplot2 based choropleths would’ve greatly reduced the effort required, the animation package usage made the effort worthwhile.] Rest of this post is generated using slidify and the code for doing so can be found on github. [In an update on 19 Jan, I had to move the shiny app to a different server because of some “technical glitches” with the previous server. All should be well now with the revised code and server.]
Getting Data from Quandl
Quandl provides violent crime rates (per 100,000 people) from 1960 through 2010 by State and the data are sourced from the FBI (Uniform Crime Reports as prepared by the National Archive of Criminal Justice Data).
There are 51 rows and 53 columns in the dataset. Data is for 51 years (2010-12-31 through 1960-12-31). 53 columns include a column for Year, each state and the District of Columbia, and the average for the country.
Let’s now do three things.
Create a column denoting the year
Drop the column involving date (the existing ‘Year’ column)
Drop the column involving the average for the country
Drop the column for District of Columbia, which is not a State.
Instead of looking at annual data, let’s focus on different decades (2001-2010 [rows 1-10], 1991-2000 [rows 11-20], 1981-1990 [rows 21-30], 1971-1980 [rows 31-40], and 1961-1970 [rows 41-50]). Let us compute the mean violent crime rates for each state for each decade. Since 1960 does not fit in to our plans, let us drop that row.
Choropleths of different decades using ggplot2 and animation
The first order of business was to get a US map. A typical map file generated from the maps package does not include Alaska and Hawaii. Scouring the web for someone who might’ve addressed this issue took me to this post of Kristopher Kapphahn. The post was terrific and provided the complete code for generating such a map. This code, which is in a separate file for those interested in it, was used to generated a file all_us.rda. We use this file below for the map.
A preliminary choropleth for the 2001-2010 decade
Tweaking Data and Animating Choropleths
To bring in more clarity, let us classify states into 3 different groups based on their violent crime rates for a decade and color code them differently. Top 1/3 in crime rates (high, denoted by number 3) are most dangerous and middle 1/3 (medium, denoted by number 2) are more dangerous than the bottom 1/3 (low, denoted by number 1).
On to the animation.
Choropleths of different decades using rCharts, googleVis, and Shiny
Before we proceed with these, let’s quickly modify the decadespct data frame and save it for use in this set of animations. Please see the comments for changes made.
Lastly, the app. (Please press the play button. You can toggle between the rCharts version and the googleVis version by selecting the relevant tab.) The app’s code can be found on github.