Press "Enter" to skip to content

Creating a 3D Precinct Level Map

Time sure flies.

It has been a while since I have posted a tutorial, but I think I have a good one. The idea of 3d representations for population data has been around for some years, but I wanted to look exclusively at the winning votes after all other votes have been cancelled out.

Visualizing Precinct Level Election Results in 3D

Project Setup

We will need both the shapefile for the precinct as well as the election results for each precinct. Thankfully Minnesota provides a lot of transparency and the data is readily available. The shapefiles are available at gisdata.mn.gov, and the precinct level results are available at electionresults.sos.mn.gov.

Required Libraries

I typically will load more libraries than required, just because I like using them at some point.

Geospatial Fun

As you can see, the precinct_name has been capitalized while removing any non-alphanumeric characters. This is done to prevent potential issues when joining the election results.

Voting Districts

Precinct Information

Now we need to read in the precinct data. I am reading directly from the url, but you can save the file locally to work offline. This data will be used to walk between the voting data and the geospatial data.

As you can see, while after reading in the file I renamed all of the columns since the file didn’t contain a header. I pulled those columns from this description which contains the layout of many of the election result text files. As you can see, the precinct_name has been capitalized while removing any non-alphanumeric characters. This is done to prevent potential issues when joining the geospatial results.

Voting Information

Once again, thank goodness for that media layout file above that describes the columns. As you can see, the voting file contains precinct_id and county_id. There was no need to read in the precinct file. Sometimes that happens, and time is spent down a rabbit hole you never needed to go down. That is okay though.

Building the initial Matrix

You can make this larger, which will look better. I am keeping this at 2000 for simplicity’s sake. This mat will be used a couple more times, once for the color/fill and once for the elevation matrix.

Building Additional Matrices

We will need both a color matrix and an elevation matrix.

Color Matrix

The export should show a rasterized version, and the white fills are where Trump and Harris tied in total votes for those precincts.

Rasterized color coding

Elevation Matrix

Now for the elevation matrix

Yeah, that was easy. Winning votes are winning votes, whoever they are for. Elevation, in the context of this plot, should always be positive.

Positioning, Rendering, and Saving

This is only a fraction of the power of https://www.rayrender.net/. Be sure to check that site out and the render_highquality reference. This tutorial was inspired by https://justjensen.co/making-population-density-maps-with-rayrender-in-r/.

3D Election Result Plot MN

Be First to Comment

    Leave a Reply

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