Wrangle and Visualize Election Data Easily analyze your election results data with the election2 package. One command calculates winners and percents for each election district row as well as overall totals.

Then, take those results and turn them into interactive tables, bar charts, and maps with functions like bargraph_of_margins(), quick_table(), and map_election_results().

election2 is designed for journalists, hobbyists, analysts, and data geeks who want to quickly go from a spreadsheet or CSV file with raw vote totals to visualizations that help you see trends behind the data.

Note: This package is not on CRAN, so you need to install it from GitHub with the R package and function of your choice, such as

devtools::install_github("smach/elections2", build_vignettes = TRUE)

Data wrangling

To use elections2 for your elections data, start with one of the package functions that wrangle a spreadsheet or CSV file with election results. You’ve got two options: wrangle_results() to generate detailed results from a file with only two candidates or choices; and wrangle_more_cols() for results with more than two choices.

Data with only 2 choices

wrangle_results() is for when you want to compare the performance of only two candidates or a yes-no ballot question. That means you don’t wish to include additional results such as third parties, write-ins, or blanks.

The function returns winner, percents, and percent and vote margins for each row as well as an overall total. The function also has a turnout_columns option if your data includes columns for vote totals and total registered voters and you’d like to calculate turnout percentage.

After running wrangle_results() on your data, you can use this package’s visualization functions on the results to create tables, bar graphs, and maps.

Data format

wrangle_results() needs your data to be in a very specific format.

If your CSV file or spreadsheet doesn’t include turnout data, that election results file must be in the following 3-column format: