Created
November 4, 2020 22:47
-
-
Save Riebart/1a2080028749a69c320f11762087886e to your computer and use it in GitHub Desktop.
Wolfram code to generate a few maps that show election results, scaling states to show actual electoral power.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
votesPerState = {{"Alabama", 9.`}, {"Alaska", 3.`}, {"Arizona", | |
11.`}, {"Arkansas", 6.`}, {"California", 55.`}, {"Colorado", | |
9.`}, {"Connecticut", 7.`}, {"Delaware", | |
3.`}, {"DistrictOfColumbia", 3.`}, {"Florida", 29.`}, {"Georgia", | |
16.`}, {"Hawaii", 4.`}, {"Idaho", 4.`}, {"Illinois", | |
20.`}, {"Indiana", 11.`}, {"Iowa", 6.`}, {"Kansas", | |
6.`}, {"Kentucky", 8.`}, {"Louisiana", 8.`}, {"Maine", | |
4.`}, {"Maryland", 10.`}, {"Massachusetts", 11.`}, {"Michigan", | |
16.`}, {"Minnesota", 10.`}, {"Mississippi", 6.`}, {"Missouri", | |
10.`}, {"Montana", 3.`}, {"Nebraska", 5.`}, {"Nevada", | |
6.`}, {"NewHampshire", 4.`}, {"NewJersey", 14.`}, {"NewMexico", | |
5.`}, {"NewYork", 29.`}, {"NorthCarolina", 15.`}, {"NorthDakota", | |
3.`}, {"Ohio", 18.`}, {"Oklahoma", 7.`}, {"Oregon", | |
7.`}, {"Pennsylvania", 20.`}, {"RhodeIsland", | |
4.`}, {"SouthCarolina", 9.`}, {"SouthDakota", 3.`}, {"Tennessee", | |
11.`}, {"Texas", 38.`}, {"Utah", 6.`}, {"Vermont", | |
3.`}, {"Virginia", 13.`}, {"Washington", 12.`}, {"WestVirginia", | |
5.`}, {"Wisconsin", 10.`}, {"Wyoming", 3.`}} // Sort; | |
statePolygons = {#, | |
Cases[AdministrativeDivisionData[{#, "UnitedStates"}, | |
"Polygon"], {{_Real, _Real} ..}, \[Infinity]]} & /@ | |
votesPerState[[All, 1]] // Sort; | |
voteStatus = | |
Import["https://fcast.espncdn.com/FastcastService/pubsub/profiles/\ | |
11001/topic/elections-2020-national-map-electoral/message/3427/\ | |
checkpoint", | |
"RawJSON"];(* Note that the event ID (3427) increments over time *) | |
\ | |
voteStatusSum = { | |
Capitalize[#["displayName"], "AllWords"] // | |
StringReplace[" " -> ""], | |
Select[#["candidates"], #["id"] == 1036 &][[1]]["statistics"][ | |
"percent"]["value"]/100, | |
Select[#["candidates"], #["id"] == 8639 &][[1]]["statistics"][ | |
"percent"]["value"]/100, | |
#["reporting"]["value"]/100 | |
} & /@ | |
voteStatus["states"] // Sort; | |
stateSum = {#[[1, 1]], #[[1, 2]], #[[2, 2]], | |
Hue[If[#[[3, 2]] > #[[3, 3]], 0.7, 0], #[[3, 4]], 1]} & /@ | |
Select[Transpose[{votesPerState, statePolygons, voteStatusSum}], | |
And[#[[1, 1]] != "Alaska"] &]; | |
Transpose[{votesPerState[[All, 1]], statePolygons[[All, 1]], | |
voteStatusSum[[All, 1]]}] // TableForm; | |
ContractPoints = Function[{pts, r, mrm}, | |
Module[{ | |
centroid = RegionCentroid[Region[Polygon[pts]]], | |
measure = RegionMeasure[Region[Polygon[pts]]]}, | |
Sqrt[mrm/measure] | |
Clip[r, {0, 1}] (# - ConstantArray[centroid, Length[#]]) + | |
ConstantArray[centroid, Length[#]] & /@ pts | |
] | |
]; | |
maxRegionMeasure = | |
Max[RegionMeasure[Region[Polygon[#]]] & /@ stateSum[[All, 3]]] | |
GraphicsColumn[{ | |
GeoGraphics[ | |
{EdgeForm[Black], FaceForm[#[[4]]], | |
Polygon[GeoPosition[#[[3]]]]} & /@ stateSum, | |
GeoRange -> Entity["Country", "UnitedStates"], | |
PlotLabel -> | |
Style["When all states are their geographic size", White], | |
GeoZoomLevel -> 6, | |
ImageSize -> 1000 | |
], | |
GeoGraphics[ | |
{EdgeForm[Black], FaceForm[#[[4]]], | |
Polygon[GeoPosition[ | |
ContractPoints[#[[3]], #[[2]]/Max[votesPerState[[All, 2]]], | |
RegionMeasure[Region[Polygon[#[[3]]]]]]]]} & /@ stateSum, | |
GeoRange -> Entity["Country", "UnitedStates"], | |
PlotLabel -> Style["When all states are scaled \!\(\* | |
StyleBox[\"from\",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\"their\",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\"grographic\",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\* | |
StyleBox[\"size\",\nFontSlant->\"Italic\"]\) | |
down based on their relative number of electoral seats", White], | |
GeoZoomLevel -> 6, | |
ImageSize -> 1000 | |
], | |
GeoGraphics[ | |
{EdgeForm[Black], FaceForm[#[[4]]], | |
Polygon[GeoPosition[ | |
ContractPoints[#[[3]], #[[2]]/Max[votesPerState[[All, 2]]], | |
maxRegionMeasure]]]} & /@ stateSum, | |
GeoRange -> Entity["Country", "UnitedStates"], | |
PlotLabel -> | |
Style["When all states are scaled so their relative \!\(\* | |
StyleBox[\"area\",\nFontSlant->\"Italic\"]\) is based | |
solely on their relative number of electoral seats", White], | |
GeoZoomLevel -> 6, | |
ImageSize -> 1000 | |
] | |
}] // Export["out.png", #] & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment