Visualizing Political Polarization in the U.S. with Shiny
The Shiny app presented in this blog post can be found here:
A quick glance at election results over the last 20 years shows some major changes that have taken place among the American electorate in only a few short election cycles. This is easiest to visualize when looking at county maps, because there are over 3,000 counties in the U.S., and only 50 states. Looking at electoral maps from 20 years ago (1996, for example), one can see a relative balance of blue (Democratic) and red (Republican) areas. Fast forward to 2016, and blue counties now appear as small, remote islands in a sea of red, as highly-populated cities and their immediate suburbs have become the center of the Democratic electorate, in contrast to the Republican electorate, which is more sparsely spread out among the majority of the area in the country.
Red counties keep getting redder, and highly populated blue counties continue to become bluer. As discussed below, there is the potential for this to have profound effects on the United States as a whole. I designed a Shiny App to explore this in more detail, using data from both uselectionatlas.org and the U.S. census.
Data Acquisition and Processing
Election Data Scraping
Election data, at the county level from 1960 to 2016, was scraped from uselectionatlas.org, an excellent resource to study historical election data. Data scraped included raw vote totals, candidate names, state and county names, and county FIPS code, which is a unique identifier that was used for joining to Census data and for mapping. Election data was obtained for approximately 3,100 counties in 49 states (Alaska does not tabulate election data by county and has a different structure from other US States).
Demographic Data from the US Census
I then downloaded a CSV of demographic data from the US Census, at the county level, from 1980 to 2016. Before 1980, the availability of certain demographic information was lower. Furthermore, a number county boundaries changed in the U.S. from 1960-1980. In contrast, only two US counties have been altered since 1980. Because of this, I decided to focus on the time period from 1980-2016 for my project going forward.
I then used Pandas to join election data and census data into a combined CSV file, which contained all necessary data for this project in one data frame.
Data Mapping in Ggplot2
This CSV file was then imported into R using dplyr. I also installed the ChoroplethMaps library, which contains useful mapping boundaries for many geographies, including U.S. Counties. I then created some unique features in my data frame, most importantly including the "relative margin" in any given election, which is how a county voted (Democratic/Republican) in an election compared to the national average (for example, if the Democratic candidate won nationally by 5%, and in a given county, that candidate won by 20%, that county would be +15 Democratic. Positive numbers (and blue coloring) indicated a Democratic lean, while negative numbers (and red coloring) indicated a Republican lean. Using ggplot, ChoroplethMaps, the FIPS codes for each county, and my calculated data, we could then visualize the political map for any geography in any year. As an example, here is a map of the relative margins for Ohio in 1988:
I incorporated these visualizations into a Shiny app. I also designed it so that, in parallel, with one click, you can also see how populated each county is (in green). Typically, we find that more populated counties (deep green) tend to vote more Democratic than rural counties.
Shiny App Design
With this combined data, I created a Shiny app that would help users to make visualize the changes that have taken place politically over the past 40 years. I used data from 10 total presidential elections (1980-2016). Here is a summary of the different Shiny pages I created:
- Introductory page, laying out the rationale as I have in this blog post
- Numerical graphs depicting change in polarization over a choice of years. In this section, you can choose any combination of years and get a boxplot returned showing how all counties in the US have skewed over time. Because most counties are smaller and rural, we see that the "average" county has gone from relatively balanced to highly skewed Republican over the years, and this has been balanced out by a small number of highly populated counties becoming more Democratic
- The option to select a state, and see a county map showing how that state has changed (Democratic/Republican) over time, from 1980 to 2016. For an example, here is a look at Colorado:
4. The option to choose on individual county, instead of a whole state, and see how that county has changed over time. Doing this, you will see small rural counties have become redder, core urban cities were blue and have gotten slightly bluer. Some of the most striking counties, however, are suburban counties, which used to be Republican-leaning but have become reliably Democratic in recent years. Here, for example, is Fairfax County, Virginia, with a population of 1,000,000 located outside of Washington, DC:
5. Finally, in part 5 you can perform a demographic analysis using a scatterplot of a subset of all U.S. counties, based on given criteria. You have the option to subset the counties based on demographic information (age, race, income, urban/rural, education, etc) and see how that subset of counties has changed over time. This is the visualization for all 3,100 counties:
We can see that in general, red counties have become redder (lower left quadrant), and blue counties have gotten bluer (upper right quadrant). These are not many counties in the other two quadrants.
This app presents a significant and powerful tool to easily visualize the changes in political polarization that have taken place in the United States in the last 40 years. Users have the ability to select almost any geography and see how it has changed over time, and also to perform statistical analysis by visualizing scatter plots of subsets of U.S. counties. Political polarization is not an ideal situation for a nation -- it leads to groups of people becoming more isolated from each other in terms of ideas and philosophies, and leads to an increasing sense that there are "two Americas,", which have vastly different views on how our country should move forward. My hope is that leaders will work to shrink this divide over time, and I hope that this Shiny app will help users to visualize the increasing gap we are experiencing and take actions to make improvements.
My Shiny app can be found here:
My Github code can also be found here: