How to Change the Reference Map in Choroplethr

[This article was first published on AriLamstein.com » R, 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.

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:

no-reference-map default-reference-map satellite-color

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.

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 Change the Reference Map in Choroplethr 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 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)