US State Maps using map_data()

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:

By use-r-friendly

Tags: graphics rstats ggplot2 AdventCalendaR