2014/04/12

Visualize violent crime rates in the US with choroplethr package

Visualize violent crime rates in different US States with choroplethr package I have read choroplethr package from the blog post Animated Choropleths in R a few days ago. As a another visualization tool in R language, I want to try this one.
To install the latest stable release(CRAN) type the following from an R console:
install.packages("choroplethr")
To install the development version using the devtools package from github, type the following::
library(devtools)
install_github("choroplethr", "trulia")
library(choroplethr)
It's not interesting for me to run just example codes written in choroplethr package. Instead, I used other data from rMaps package as a quick data source and visualize it!
library(devtools)
install_github("ramnathv/rCharts@dev")
install_github("ramnathv/rMaps")
Now we can use violent crime rates data in the US included in rMaps package.
We can create animated choropleths as the following page:
In my case, we just process the data and visualize it as the following simple code:
# load packages
library(rMaps)
library(choroplethr)
# initialization list and get years from violent_crime data
choropleths = list()
# Get years for loop
years <- sort(unique(violent_crime$Year))
# convert to level data
violent_crime$Crime <- cut(violent_crime$Crime, 9)
# Create choropleth component.
for (i in 1:length(years)) {
    df <- subset(violent_crime, Year == years[i])
    # We need to change the column names for choroplethr function
    colnames(df) <- c("Year", "region", "value")
    # Cut decimal off df$value <- round(df$value)
    title <- paste0("Violent crime rates: ", years[i])
    choropleths[[i]] = choroplethr(df, "state", title = title)
}
# Vizualize it!
choroplethr_animate(choropleths)
The result is published via Dropbox as the following (image)link.
Enjoy!

3 件のコメント:

  1. any idea how to make these in R? http://static3.businessinsider.com/image/51af64bdecad048b58000001-1200/united-states-dialect-map-language.jpg

    返信削除
  2. I think you can use maptools package to do this https://sites.google.com/site/spatialr/plottingmaps
    And also, this blog post might be useful for you http://www.analyticsandvisualization.com/2014/01/animated-choropleths-using-animation.html

    返信削除
  3. There used to be code for isarithmic choropleths on David Spark's blog. Not sure it's still there. Link at the bottom of this page.

    返信削除