First term Presidential Referendum through Congressional Midterm Elections: Shiny App Analysis

Posted on Feb 6, 2017


“Midterm elections are regarded as a referendum on the sitting president's and/or incumbent party's performance.” (source: Wikipedia, “United States midterm elections”,

Utilizing data visualization through a Shiny app, this analysis reviews the composition of the US congress (Senate and House of Representatives) in the first year of a presidential term and two years after the presidential election at the midterm elections.  The analysis includes the congressional composition and seat changes over time by state and by party represented in a plot encompassing the United States as a whole. Each house of congress is segmented in its own plot.

The analysis also visually summarizes the change in congressional seats by party at the midterm elections thereby indicating whether a president has gained or lost party strength in the congress - either in the senate, house or both.

The gain or loss in congressional party seats is the referendum on the sitting president. The midterm elections indicate how well the president is doing his job. Any changes in party strength based on congressional seat changes has a profound impact on the power and ability of a president’s performance. The ramifications are widespread affecting every American and the world too.

Data Description

The data set utilized for this analysis was the Kaggle “Party Strength by State” data set available at The data set begins with President Ronald Reagan’s first term in 1980 moving through present day.

The data set also includes state level gubernatorial and state legislature data. This information was not utilized for the sake of this particular data visualization analysis.

Independents were also included in the data set. However, the observations of independents were insignificant and were disregarded with the primary focus of the analysis on the two main political parties in the United States: Republicans and Democrats.

Data Visualization 

The data visualization from the Shiny app shows two primary graphical renderings by congressional house. The first group of renderings illustrates a map of the US showcasing the composition of the congress during presidential and midterm elections. Utilizing the primary widget, the date of the data corresponding to a specific president of interest may be changed using the drop-down menu.

With Reagan’s first term as the start of the data set, the second graphical rendering to the right of each US map shows the changes in congressional seat by party. To render this graph, click the second widget “Select Congressional Mid Term Election” to highlight seat changes by party at the midterms.

Please note: the coloring of these plots is consistent with other political plots: red represents the Republican party; blue represents the Democrat party; and white represents an even split between the two parties where senators or representatives are 50/50 in their party affiliation by state. The white representation is much more prevalent in the Senate than the House of Representatives due to the smaller sample size by state, e.g. two senators to numerous representatives by state population.

Following is the data visualization of the Reagan-era midterm election results:


Shiny App Summarized Analysis

Analyzing the data through Shiny app visualizations over time, President Ronald Reagan, the only president since 1980, gained strength in his party after being in the White House for the first two years of his first term. Given his positive midterm referendum, the change in party seats from democrat to republican indicates that the electorate felt comfortable with Reagan’s leadership providing more voting seats within his party in congress. To this day, history looks back upon President Ronald Reagan as one of the greatest presidents of the 20th century.

Reagan was the last president to enjoy such a positive midterm referendum in his first term. Since Reagan, all other presidents have consistently lost seats in the midterms, thus losing power and finding increasing difficulty in working with congress to legislate for the electorate.

Charted Analysis using the Shiny App

The following chart summarizes the data visualized in the Shinny app by president and election.

Slide4D = Democrats; R = Republicans; Independents were excluded from this analysis given insignificant observations.


Based on the data visualized in the Shiny app and summarized in the chart above, there are two key-takeaways:

  • As indicated previously, in his first term as president, President Ronald Reagan gained seats at the midterm elections (positive referendum) furthering his republican base and support in congress. He gained seats in both the Senate and House of Representatives thereby supporting his initiatives as president.
  • Since Reagan, no other president has seen such strong partisan support. Consistently, other presidents have seen a loss in congressional seats in one house of congress or both furthering the erosion in their party base. Two potential factors help explain this phenomena.

One, fewer voters take a stance at the midterms, whereas in main stream presidential elections there is greater voter turn-out. Essentially, the midterms are biased as the ‘motivated voter’ turns up to vote whereas others do not.  

Second, historically, presidents lose ground with voters once they begin making policy. Simply put, a president cannot support all of those campaign promises and the electorate lets him know by culling his power through congressional party seat changes at the midterms.

Project Iterations

Any data visualization analysis requires iteration to fully tell the story of ‘why'. In this case, there are several data points that may be added to this analysis to make the data more robust and to take the analytical conclusions to another level. Those data iterations include:

  • Economic Data: Many voters are likely to ‘vote with their wallet.’ Given this trend, adding jobs data and consumer sentiment during each presidential term would help illustrate ‘why’ a president gained or lost seats during the midterms. Other potential data to consider: crude oil and gas price data, tracking recessions, US GDP and spending on wars to fight terrorism (Iraq I, Iraq II and Afghanistan). All of these data points directly impact US tax payers and influence the sentiment of the electorate.
  • Voter Demographic Data: Include the difference between presidential and midterm voter demographics and voter turn-out.
  • Regression Analysis: Projection of President Trump’s midterm congressional elections using an improved data set and multivariate regression analysis.

R Code

The following code represents the R code derived to create multiple data frames calculating the difference in congressional seats - core to this analysis. Other R code was utilized through the ui and server files to plot the geographical maps based on the primary data set.

Calculating the congressional seat differences required the creation and mutation of multiple data frames. Once created, the difference between the data frames was calculated and new integers were stored for analysis and graphical use. As a result, new visualizations of the congressional seat changes by presidential midterm election were charted and rendered using googleVis and the Shiny app.

The baseline code for these graphical renderings follows:


About Author

Related Articles

Leave a Comment

No comments found.

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 2020 Revenue 3-points agriculture air quality airbnb airline alcohol Alex Baransky algorithm alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus ames dataset ames housing dataset apartment rent API Application artist aws bank loans 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 boston safety Bundles cake recipe California Cancer Research capstone car price Career Career Day citibike classic cars classpass clustering Coding Course Demo Course Report covid 19 credit credit card crime frequency crops D3.js data data analysis Data Analyst data analytics data for tripadvisor reviews data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization database Deep Learning Demo Day Discount disney dplyr drug data e-commerce economy employee employee burnout employer networking environment feature engineering Finance Financial Data Science fitness studio Flask flight delay gbm Get Hired ggplot2 googleVis H20 Hadoop hallmark holiday movie happiness healthcare frauds higgs boson Hiring hiring partner events Hiring Partners hotels housing housing data housing predictions housing price hy-vee Income Industry Experts Injuries Instructor Blog Instructor Interview insurance italki Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter las vegas airport lasso regression Lead Data Scienctist Lead Data Scientist leaflet league linear regression Logistic Regression machine learning Maps market matplotlib Medical Research Meet the team meetup methal health miami beach movie music Napoli NBA netflix Networking neural network Neural networks New Courses NHL nlp NYC NYC Data Science nyc data science academy NYC Open Data nyc property NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time performance phoenix pollutants Portfolio Development precision measurement prediction Prework Programming public safety PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis 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 seafood type Selenium sentiment analysis sentiment classification Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau teachers team team performance TensorFlow Testimonial tf-idf Top Data Science Bootcamp Top manufacturing companies Transfers tweets twitter videos visualization wallstreet wallstreetbets web scraping Weekend Course What to expect whiskey whiskeyadvocate wildfire word cloud word2vec XGBoost yelp youtube trending ZORI