Press "Enter" to skip to content

Create 3D County Maps Using Density as Z-Axis

This is going to be a bit longer than some of my previous tutorials as it covers a walkthrough for sourcing data, scraping tables, cleaning, and generating the 3D view below which you can springboard from with the help of the rgl package. The heavy lifting is done with ggplot and rayshader.

Rendered Visualization of MN Election Results in 2018 for Governor

Rayshader

rayshader is an open source R package for producing 2D and 3D hillshaded maps of elevation matrices using a combination of raytracing, spherical texture mapping, and ambient occlusion.

Rayshader on GitHub

This amazing package is what inspired this tutorial. I saw one of the author’s tweets, remembered the 3D map from BlueShift I saw from the 2016 election results.

One thing to keep in mind is this will not be as fast as some other solutions (plotly for example), and the interactive element will likely not be shareable. There is a function called writeWebGL within rgl that I have not had much luck with, but it does exist. Rasyshader allows for creating programatic flyovers and animations though.

The R Script

Including packages

These are the packages I used for the majority of the code. I will include the ones for creating rasters and rayshading later in this write-up.

County Information

This tutorial is for Minnesota, but other regions can be used without changing too much. The biggest difference I noticed was abbreviations for county names.

The dataframe mn_county will be reused as the primary dataframe in this tutorial, and will have information from difference sources appended to this dataframe.

Population Information

We only need one table. To determine which table we print out the table. It is the one with sortable in the class name, but there are other ways of determining which one. The results from tbls will give something that looks like this…

To make this easier I just chose to use the first table to read, and used the first element (dataframe) from the html_table return.

The county density is used for our “elevation”.

MN Density

Obviously this isn’t really a worthwhile result yet, but it is getting there. The code for the image above is essentially the same as the code near the end, but with the color layer ommited.

Governor Information

This output two different tables that looked similar to the following…

We want the second table with county results, which has an actual index of 11 as seen at the top of the output. This might not be the best way, but it is quick. Without the %>% print() %>% we just see [1] or [2], which would give us the wrong table.

The above was just to get the margins to diverge from 0 (which will be used later for ggplot.

If you were wondering, that ~1.68 came from Ramsey County, voting 170,391 to 63,653 for Walz. The min() came from Morrison with 9,711 to 4,123 for Johnson.

Time to set the limits. I chose .3 in either direction, which seemed good.

3D Rendering

First up, the packages unique to this portion

If you haven’t installed rayshader, use

Generating Elevation

To simplify the process of generating the elevation and color overlay I made two ggplots hiding typical elements. The first is for elevation.

This gives us just the rough outline for the state of Minnesota

MN ggplot

Plot the elevation using the county density.

Elevation 2D

Save the plot, make sure to use the same dimensions for both elevation and color overlay.

Generating Color Overlay

This is where the limits come into play.

MN Governor

Convert Plots to Matrix Values

For some reason ggplot seems to keep a bit of a border when using ggsave, so we will need to trim off a little from the matrix.

I chose to trim by 10 on every side, for both elevation and color overlay.

Now we get this

Do the Same for Color Overlay

The Payoff

Here it is, the final piece from Rayshader. If you just want the elevation, remove the line for add_overlay().

Rendering the water by changing that flag can be a good way to segment or filter out low density counties.

Election Results with Water

Some Useful Commands

Sharing is caring!

One Comment

  1. Armin Kakas Armin Kakas November 30, 2018

    This is excellent. Thank you for posting.

Leave a Reply

Your email address will not be published. Required fields are marked *