US State Maps using map_data()

December 11, 2012
By

(This article was first published on is.R(), and kindly contributed to R-bloggers)

image

Today’s short post will show how to make a simple map using map_data().

Let’s assume you have data in a CSV file that may look like this:

image

Notice the lower case state names; they will make merging the data much easier. The variable of interest we’re going to plot is the relative incarceration rates by race (whites and blacks) across each of the fifty states (we’ll remove DC once we load the data). Using the map_data(“state”) command, we can load a data.frame called “all_states”, shown below:

image

Merging that data with the data frame we have as a CSV produces:

image

We can then plot each state and shade it by our variable of interest:

image

Full code is below:

To leave a comment for the author, please follow the link and comment on his blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.