/NC-Elections-Demographics-Dashboard

This project contains a dashboard allowing the user to analyze N.C. county-level voting results alongside demographic trends.

Primary LanguageJupyter NotebookMIT LicenseMIT

NC-Elections-Demographics-Dashboard

The dashboard is hosted on binder. To launch, click the Binder button and allow 30+ seconds to load, then click Web-Dashboard.ipynb. Finally, Cell > Run All in the toolbar to launch the dashboard.

Binder

This project contains a dashboard allowing the user to analyze county-level voting results alongside demographic trends. Use the inputs to specify the data you want to return, then click Update to generate maps, charts, and tables. These demonstrate county level voting patterns and demographic trends. Counties voting more Democratic are shown in blue and Republican are shown in red. Hover over the map uncover demographic data. The bar charts showcases the largest counties in the state (having 100,000+ residents). In the Table tab, use the controls to filter the tables in place. All data is downloadable and verifiable via the data source links below.

NC-Elections-Demographics-Dashboard-GIF

Usage

The program accomplishes four main things: (1) reads in election results data from North Carolina State Board of Elections (NCSBE) text files, (2) queries data from the American Community Survey (ACS) API, (3) joins those data sets at a county level, and (4) builds an ipywidgets-powered dashboard to interrogate the data.

NCSBE Election Results

The primary goal of this dashboard is to give the user election results data, as defined by votes cast for Democratic and Republican candidates (excludes other parties). The NCSBE provides county level election results with detailed breakdowns here as text files, which can be read similarly to a csv using pandas or other packages. We read these in and filter to the five races we are interested in: Presidential, US Senate, US House, NC Senate, and NC House.

2020_election_results

American Community Survey (ACS) Demographic Trends

Looking at results alone tells us very little about voting trends. The dashboard also showcases demographic trends, such as age, race, gender, and income, which can inform movement towards one party or another. The Decenniel Census would be the natural first place to look for this data, and it is the most robust, but as of the creation of this project the last available was 2010, which is far too stale. Alternatively, the American Community Survey is conducted every year, as well as more robustly on five year intervals. For this dashboard we leverage the yearly survey data, which is convenient available through the ACS API. The API can be confusing at first but is extremely useful once you've learned it. Try this query as an example, retrieving North Carolina's population by county for 2019.

acs_api_population

For more on learning the ACS API, I recommend starting with this tutorial video.

Building the ipywidgets Dashboard

So we've acquired and joined the data to power our dashboard. The next step is to build the interactive dashboard, made of ipywidgets, to empower the user to interrogate.

The Dashboard, Exhibits, and Tables

The dashboard loads with four selection options: (1) metric, (2) race aka contest, (3) demographic overlay, and (4) voting method. The user begins by making their selections and hitting the Update button. After a few seconds, the Exhibits and Table tabs load with the requested data.

dashboard_wake_up

Metrics:

  • Margin Percent - the percentage vote that the Democratic candidate exceeded/(trailed) the Republican candidate.
  • Margin Count - the raw vote count that the Democratic candidate exceeded/(trailed) the Republican candidate.
  • Votes Cast - the raw vote count cast in that contest regardless of party
  • Vote % of Pop - the percentage of people casting votes of the population (note: percent of registered voting population would be a better metric, but I have not integrated that data yet. Stay tuned.)

Races:

  • Presidential
  • US Senate
  • US House
  • NC Senate
  • NC House

Demographics:

  • Gender
  • Age
  • Race
  • Educational Attainment
  • Income
  • Health Insurance

Voting Methods:

  • Total (Early + Election Day)
  • Early
  • Election Day

The first exhibit is a map, showing the voting results on a county level. Bluer counties had more Democratic vote, redder had more Republican vote. (Note: there are some counties missing in the dataset, causing omissions in the map. Stay tuned for this fix.) Hovering over a county reveals the FIPS code, election results data, and demographic data.

map

Where the map is useful for comparing counties geographically, the first bar chart is a better view of magnitude. It filters to only counties having 100,000+ residents, for the sake of having a concise graph (N.C. has 100 counties, far too many to fit cleanly on a single bar chart).

elec_bar

The second bar chart shows the magnitude of the demographic trend, also for the counties having 100,000+ residents.

demo_bar

The next tab, Tables, contains the same data as the exhibits but laid out in tabular form with filters to modify the tables in place. The controls include: (1) years multi-select, (2) columns multi-select, (3) winning party radio buttons, (4) population slider, (5) margin count slider, (6) margin percent slider, (7) votes cast slider, and (8) votes & of population slider. Engaging with these widgets filters the tables, shown below.

table_controls

The Yearly Summary Table shows the voting and demographic results grouped at an annual level.

summary_table

The County Detail Table shows the voting and demographic results at a county level, across years.

detail_table

The data behind the exhibits and tables is downloadable as a csv from the Download button, in case the user needs to analyze the data in a style or precision not possible within the dashboard.

csv_download

And that wraps it up! This is a decent example of the power of ipywidgets and interactive controls. Demographics are commonly modeled by political scientists and operatives to inform their campaign and policy decisions. This dashboard offers the opportunity to interrogate North Carolina county trends.

License

MIT © Dave Russell