How to Remove State Abbreviations from a Choroplethr Map

August 20, 2015
By

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

Today I will demonstrate how to remove state abbreviations from a choroplethr map. I will also demonstrate how to combine multiple maps into a single image. This is what the final image looks like:

abbr-comparison

In this case the abbreviations obscure information for the northeast part of the country, so it’s useful to remove them.

Step 1: Create a Map With Abbreviations

The map with abbreviations is fairly straightforward to create. In fact, it is very similar to the map created in the first lesson of my course Learn to Map Census Data in R.  Let’s create the map, render it, and then save it as a variable:

library(choroplethr)
library(choroplethrMaps)

data(df_pop_state)
with_abbr = state_choropleth(df_pop_state,
                title = "2012 State Population Estimates",
                legend = "Population")

with_abbr

w-abbr

Step 2: Learn About Object-Oriented Choropleths

To make the map without abbreviations we need to use the object oriented features of choroplethr. This is much easier than it sounds; in fact, all that the function state_choropleth does is create a StateChoropleth object:

> state_choropleth
function (df, title = "", legend = "", num_colors = 7, zoom = NULL) 
{
    c = StateChoropleth$new(df)
    c$title = title
    c$legend = legend
    c$set_num_colors(num_colors)
    c$set_zoom(zoom)
    c$render()
}

If this type of code is new to you, then I recommend taking a few minutes to run it yourself from an R command line.

Step 3: Create a Map Without Abbreviations

The StateChoropleth object has a member variable called show_labels, which defaults to TRUE. You can see it in the source code here. If we set it to FALSE before calling render() then the map will render without labels.

c = StateChoropleth$new(df_pop_state)
c$title = "2012 State Population Estimates"
c$legend = "Population"
c$set_num_colors(7)
c$set_zoom(NULL)
c$show_labels = FALSE
without_abbr = c$render()

without_abbr

without-abbr

Step 4: Merge the Maps

A common task when doing data analysis is to compare two maps side by side. We can accomplish this with the grid.arrange function in the gridExtra package:

library(gridExtra)
grid.arrange(with_abb, without_abb)

 

abbr-comparison

 

In Other News

  1. Vivek Patial was kind enough to mention choroplethr in his recent talk Display of Geographic Data in R.
  2. I have been mentioning for a while that I want to create an online course. As a concrete step in that direction I created my first YouTube video: How to Make a State Choropleth Map in R. Feedback is welcome!
  3. KDnuggets, one of the top data mining blogs, was kind enough to cover my free course “Learn to Map Census Data in R”.

LEARN TO MAP CENSUS DATA
Subscribe and get my free email course: Mapping Census Data in R!

100% Privacy. We don’t spam.


The post How to Remove State Abbreviations from a Choroplethr Map appeared first on AriLamstein.com.

To leave a comment for the author, please follow the link and comment on their blog: AriLamstein.com » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Search R-bloggers


Sponsors

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)