Visualizing Political Polarization in the U.S. with Shiny

Posted on Nov 13, 2018

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 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, 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:

  1. Introductory page, laying out the rationale as I have in this blog post
  2. 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
  3.  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:

Thank you!


About Author


David Corrigan

Data Scientist and Researcher. Graduate of Columbia University (PhD) and certified Data Scientist (NYC Data Science Academy, Fall 2018)
View all posts by David Corrigan >

Leave a Comment

No comments found.

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python python machine learning python scrapy python web scraping python webscraping Python Workshop R R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp