Visualize violent crime rates in US with choroplethr package

[This article was first published on My Life as a Mock Quant in English, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Visualize violent crime rates in different US States with choroplethr package

Visualize violent crime rates in different US States with choroplethr package

I knew choroplethr package by the blog post Animated Choropleths in R a few days ago. As a another visualization tool in R language, I wana try this one.

To install the latest stable release(CRAN) type the following from an R console:

install.packages("choroplethr")<br />

To install the development version using the devtools package from github:

library(devtools)<br />install_github("choroplethr", "trulia")<br />library(choroplethr)<br />

It's not interesting for me to run just example codes written in choroplethr package, I used other data from rMaps package as a quick data source and visualize it!

library(devtools)<br />install_github("ramnathv/[email protected]")<br />install_github("ramnathv/rMaps")<br />

Now we can use violent crime rates data in 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 follwing simple code:

# load packages<br />library(rMaps)<br />library(choroplethr)<br /># initialization list and get years from violent_crime data<br />choropleths = list()<br /># Get years for loop<br />years <- sort(unique(violent_crime$Year))<br /># convert to level data<br />violent_crime$Crime <- cut(violent_crime$Crime, 9)<br /># Create choropleth component.<br />for (i in 1:length(years)) {<br />    df <- subset(violent_crime, Year == years[i])<br />    # We need to change the column names for choroplethr function<br />    colnames(df) <- c("Year", "region", "value")<br />    # Cut decimal off df$value <- round(df$value)<br />    title <- paste0("Violent crime rates: ", years[i])<br />    choropleths[[i]] = choroplethr(df, "state", title = title)<br />}<br /># Vizualize it!<br />choroplethr_animate(choropleths)<br />

The result is published via Dropbox as the following (image)link.

Enjoy!

To leave a comment for the author, please follow the link and comment on their blog: My Life as a Mock Quant in English.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)