2
$\begingroup$

I want to use Mathematica to plot a map of the 2016 US presidential votes, shaded by county rather than state - i.e. like the wikipedia image: enter image description here

...but zoomed in on just the south-east of the US. Is someone able to help with this please? AdministrativeDivision entities doesn't seem to hold voting information.

$\endgroup$
4
  • 6
    $\begingroup$ You are asking us to: 1. find properly formatted data on our own; 2. build a plot for you. This is not the preferred form of questions here. Perhaps you could show us what you have done so far, where you got stumped, and we may be able to help. $\endgroup$
    – MarcoB
    May 1 '18 at 12:33
  • $\begingroup$ Hello @Lewis, are you aware of a reputable data source with cleaned and easily accessible data which we could use for this? $\endgroup$
    – ktm
    May 1 '18 at 19:03
  • $\begingroup$ Hi MarcoB and user6014, thanks ever so much for getting back to me so swiftly on this: greatly appreciated! I appreciate that I'm asking a v. naive question, so apologies for that. I don't know where to find a database of US election results, & I'd hoped that someone on this forum may have extracted and plotted election data in the past. $\endgroup$
    – Lewis
    May 2 '18 at 11:49
  • $\begingroup$ I guess as a rough-and-ready solution, I could simply plot a blank map of the US with the county boundaries marked with black lines, and use image editing software to manually colour in the counties of interest. Is anyone able to help me with the straight-forward Mathematica code to display county boundaries? Many thanks... $\endgroup$
    – Lewis
    May 2 '18 at 11:52
3
$\begingroup$

After 30 minutes Googling for a nice spreadsheet with county data on the 2016 election, I was disappointed that I could only find data sources unique to each state. (The most helpful link I found was at SimonRogers.net 1) For the state of Maine, I found this state government source of data.

It abbreviated the county names and skipped a helpful marker of "Total:" for one county but the following code read it in and fixed the oversight:

  mainedata = 
  Import["https://www.maine.gov/sos/cec/elec/results/2016/president.xlsx"][[1]];
TableForm[mainedata[[1 ;; 20]], 
  TableHeadings -> {Range[30], Range[20]}]; (* Check out the top of the table *)
Do[ (* Search for missing total flags and insert it this way *)
 If[ mainedata[[i, {1, 2}]] == {"", ""} \[And] 
   NumberQ[ maindata[[i, 3]] ], 
  mainedata[[i, 2]] = "Total:"; Print[i, "\t:", maindata[[i]]] ],
 {i, Length[mainedata]}]

The line numbers of the county totals and the county abbreviations are found with:

totallines = Position[mainedata, "Total:"][[All, 1]];
ctys = mainedata[[# - 1, 1]] & /@ totallines

A list of Mathematica's entities for the counties is created using this command:

mainecounties = 
 EntityList[EntityClass["AdministrativeDivision", "USCountiesMaine"] ]

From this one can make a simple table of vote counts for two of the many candidates, along with their color codes. We visually confirm that the county abreviations are alphabetically in the same order as the county Entities:

TableForm[ 
 simpeltable = 
  Table[{ctys[[i]], mainecounties[[i]], 
    mainedata[[ totallines[[i]], 3]], 
    mainedata[[totallines[[i]], 6]],
    If[mainedata[[totallines[[i]], 6]] > 
      mainedata[[ totallines[[i]], 3]], Red, Blue]}, {i, 
    Length[ctys]}],
 TableHeadings -> {Range[Length[ctys] ], {"CTY", "County Name", 
    "HRC Votes", "DT Votes", "Color"}}]

enter image description here

A simple way to map out these counties colored by who got the greater popular votes is done below:

GeoRegionValuePlot[((Tooltip[ #[[2]], 
       StringReplace[ #[[2]]["Name"], ", United States" -> ""] <> 
        " HC:" <> ToString[#[[3]] ] <> " DT:" <> 
        ToString[#[[4]] ] ] -> #[[5]]) & /@ simpeltable),
 PlotLabel -> Style["Who was more popular by County", Bold, 16]
 ]

By using Tooltip you can mouse over a county and see the name of the county and the two candidate's vote totals.

Map with the results displayed with Tooltip

You will want to map more carefully by considering the many other candidate votes as well as ballots where no presidential candidates were selected. Note that there were many counties where no candidate had the majority vote but I colored the county by whoever received the larger vote. Also, consider what to do with the Uniformed and Overseas Citizens Absentee Voting Act votes, (I for one was surprised to see the votes from those in uniform and overseas went 75% for Hillary Clinton).

Perhaps you will want to shade the counties by the fraction of the differences instead of purely red or blue. You'll also want to search for a more complete data source for the states that you are interested in. I suggest you start at [Github].5

Good luck!

$\endgroup$
1
  • $\begingroup$ Hi Stephen, thank you ever so much for this - really informative and helpful, cheers! $\endgroup$
    – Lewis
    Feb 26 '20 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.