How to Change the Reference Map in Choroplethr

Last week I released an update to choroplethr that lets you combine choropleth maps with reference maps. Since that post many people have asked if it’s possible to change the reference map that choroplethr uses. The answer is yes, but it requires some code. Here is the same choropleth (New York City ZIP Code Income) with three different reference maps:

[content_upgrade cu_id=”2707″]Bonus: Download the code from this post![content_upgrade_button]Download[/content_upgrade_button][/content_upgrade]

The first map has no reference map and is the default for choroplethr. You can learn how to make maps like that in my free email course Learn to Map Census Data in R.

The second map has a black and white terrain reference map. It is also easy to make, and I explain how to do it in my post Combining Choropleth Maps and Reference Maps.

Today I will explain how to create the third map, which uses a color satellite map as a reference map.

Step 1: Learn About Choroplethr’s Object Oriented System

There are several ways to change the reference map in choroplethr. In my opinion the easiest method involves using choroplethr’s object oriented system. This system is described in my article Object Oriented Choropleths.

Step 2: Identify the Function to Override

Object oriented programming allows us to selectively override portions of an object. In this case we want to override the function get_reference_map():

get_reference_map = function()
{
  # note: center is (long, lat) but MaxZoom is (lat, long)
  center = c(mean(self$choropleth.df$long),
             mean(self$choropleth.df$lat))
  max_zoom = MaxZoom(range(self$choropleth.df$lat),
                     range(self$choropleth.df$long))
  get_map(location = center,
          zoom     = max_zoom,
          color    = "bw")
}

Step 3: Learn ggmap

The part of get_reference_map() that we want to change is get_map(), which is part of the ggmap package. The best introductions to ggmap that I have seen are here and here. To create a color satellite reference map we need get_map to look like this

get_map(location = center,
        zoom     = max_zoom,
        maptype  = "satellite",
        color    = "color")

Step 4: Create Your Own Class

Combining the above information, if you want to create a ZIP Choropleth object that uses a color satellite reference map, type the following:

library(choroplethr)
library(choroplethrZip)
library(R6)
library(RgoogleMaps)
library(ggmap)
ZipChoroplethSatellite = R6Class("ZipChoroplethSatellite", inherit = ZipChoropleth,
  public = list(
    get_reference_map = function()
    {
      # note: center is (long, lat) but MaxZoom is (lat, long)
      center = c(mean(self$choropleth.df$long),
      mean(self$choropleth.df$lat))
      max_zoom = MaxZoom(range(self$choropleth.df$lat),
      range(self$choropleth.df$long))
      get_map(location = center,
        zoom    = max_zoom,
        maptype = "satellite",
        color   = "color")
    }
  )
)

Step 5: Run

To use ZipChoroplethSatellite type the following:

data(df_zip_demographics)
df_zip_demographics$value = df_zip_demographics$per_capita_income
nyc_fips = c(36005, 36047, 36061, 36081, 36085)
c = ZipChoroplethSatellite$new(df_zip_demographics)
c$set_zoom_zip(state_zoom=NULL, county_zoom = nyc_fips, zip_zoom=NULL, msa_zoom=NULL)
c$set_num_colors(4)
c$title  = "2013 New York City ZIP Code Tabulated Areas"
c$legend = "Per Capita Income"
c$render_with_reference_map()

satellite-color

Shiny App

I have also created a shiny app that lets you experiment with several different reference maps. You can see the source code here.

[content_upgrade cu_id=”2707″]Bonus: Download the code from this post![content_upgrade_button]Download[/content_upgrade_button][/content_upgrade]
2 comments
William Erdmann says July 26, 2016

Hi Ari,

I tried to download the code for this article, but cannot find it in my download folder. What could I do wrong?

Regards,

Bill

    Ari Lamstein says July 26, 2016

    After clicking the box and entering your email, you should have received an email with the code.

Comments are closed