Function Repository Resource:

USElectionAtlasData

Source Notebook

Retrieve US election data by year and office from Dave Leip's Atlas of US Presidential Elections

Contributed by: Brian Wood

ResourceFunction["USElectionAtlasData"][]

retrieves per-state popular vote tallies for the most recent US presidential election.

ResourceFunction["USElectionAtlasData"][year]

retrieves per-state popular vote tallies for the US presidential election held in year.

ResourceFunction["USElectionAtlasData"][year,office]

retrieves per-state popular vote tallies for the US election for office held in year.

Details and Options

ResourceFunction["USElectionAtlasData"] retrieves data from the website and thus requires an internet connection.
Possible choices for office include "President", "House", "Senate" and "Governor".
Year/office combinations with no available data return Missing["NoData"].
Presidential elections go as far back as 1824; House and Senate elections go back to 1992; gubernatorial elections go back to 1990.
The Election Atlas provides candidate names for presidential elections only; other data only includes party counts (Democratic, Republican or Other).

Examples

Basic Examples (1) 

Get a Dataset of popular vote totals for the most recent presidential election:

In[1]:=
ResourceFunction["USElectionAtlasData"][]
Out[1]=

Scope (2) 

Get popular vote totals for the 2000 Bush versus Gore election:

In[2]:=
ResourceFunction["USElectionAtlasData"][2000]
Out[2]=

Look at House vote totals for the 2014 midterm elections:

In[3]:=
ResourceFunction["USElectionAtlasData"][2014, "House"]
Out[3]=

Applications (3) 

Get per-candidate popular vote totals from historical presidential races:

In[4]:=
ResourceFunction["USElectionAtlasData"][1860][Total, Rest]
Out[4]=

Compare voter turnout for the House between the two most recent midterm elections:

In[5]:=
house2018 = ResourceFunction["USElectionAtlasData"][2018, "House"]; house2014 = ResourceFunction["USElectionAtlasData"][2014, "House"];
In[6]:=
house2018[Total, #Total &] - house2014[Total, #Total &]
Out[6]=
In[7]:=
house2018[Total, #Total &]/house2014[Total, #Total &] // N
Out[7]=

Look at vote counts for your home state:

In[8]:=
ResourceFunction["USElectionAtlasData"][2014, "Governor"][
 Select[#State == $GeoLocationCity["AdministrativeDivision"] &]]
Out[8]=

Neat Examples (3) 

Create a map of the popular vote margin in the 2008 Obama-McCain race:

In[9]:=
votes = ResourceFunction["USElectionAtlasData"][2008];
In[10]:=
GeoRegionValuePlot[votes[All, #State -> #Obama - #McCain &], GeoRange -> \!\(\*
NamespaceBox["LinguisticAssistant",
DynamicModuleBox[{Typeset`query$$ = "united states", Typeset`boxes$$ =
       TemplateBox[{"\"United States\"", 
RowBox[{"Entity", "[", 
RowBox[{"\"Country\"", ",", "\"UnitedStates\""}], "]"}], "\"Entity[\\\"Country\\\", \\\"UnitedStates\\\"]\"", "\"country\""}, "Entity"], Typeset`allassumptions$$ = {}, Typeset`assumptions$$ = {}, Typeset`open$$ = {1, 2}, Typeset`querystate$$ = {"Online" -> True, "Allowed" -> True, "mparse.jsp" -> 0.717496, "Messages" -> {}}}, 
DynamicBox[ToBoxes[
AlphaIntegration`LinguisticAssistantBoxes["", 4, Automatic, 
Dynamic[Typeset`query$$], 
Dynamic[Typeset`boxes$$], 
Dynamic[Typeset`allassumptions$$], 
Dynamic[Typeset`assumptions$$], 
Dynamic[Typeset`open$$], 
Dynamic[Typeset`querystate$$]], StandardForm],
ImageSizeCache->{182.25, {8.125, 17.125}},
TrackedSymbols:>{Typeset`query$$, Typeset`boxes$$, Typeset`allassumptions$$, Typeset`assumptions$$, Typeset`open$$, Typeset`querystate$$}],
DynamicModuleValues:>{},
UndoTrackedVariables:>{Typeset`open$$}],
BaseStyle->{"Deploy"},
DeleteWithContents->True,
Editable->False,
SelectWithContents->True]\), ColorFunction -> (Blend[Lighter /@ {Red, Blue}, #] &)]
Out[10]=

Count up the total votes cast for Theodore Roosevelt in his four electoral wins:

In[11]:=
Dataset[Merge[
   Table[Normal@
     ResourceFunction["USElectionAtlasData"][year][Total, Rest], {year, Range[1932, 1944, 4]}], Total]]["Roosev."]
Out[11]=

Plot vote totals for the popular vote winners of all presidential elections for the last century:

In[12]:=
ListPlot@
 Table[{year, Max@ResourceFunction["USElectionAtlasData"][year][Total, 3 ;;]}, {year, Range[1920, 2020, 4]}]
Out[12]=

Publisher

Brian Wood

Version History

  • 1.0.0 – 02 December 2020

Related Resources

Author Notes

The Atlas of U.S. Presidential Elections website states that permission is required if using data for "a commercial publication or project."

License Information