Skip to content

Instantly share code, notes, and snippets.

@jshannon75
Created November 12, 2022 18:53
Show Gist options
  • Save jshannon75/e554c405e8a6a2bb9662d35f3a397433 to your computer and use it in GitHub Desktop.
Save jshannon75/e554c405e8a6a2bb9662d35f3a397433 to your computer and use it in GitHub Desktop.
Animated Cartogram of the Nov 2022 Georgia Senate election
US Senate (Vote For 1)
Herschel Junior Walker (Rep) Raphael Warnock (I) (Dem) Chase Oliver (Lib)
County Registered Voters Election Day Votes Absentee by Mail Votes Advance Voting Votes Provisional Votes Total Votes Election Day Votes Absentee by Mail Votes Advance Voting Votes Provisional Votes Total Votes Election Day Votes Absentee by Mail Votes Advance Voting Votes Provisional Votes Total Votes Total
Appling 0 2569 224 2550 0 5343 411 155 694 0 1260 28 4 33 0 65 6668
Atkinson 0 866 33 807 0 1706 189 22 291 0 502 15 2 10 0 27 2235
Bacon 0 829 74 2301 0 3204 134 41 283 0 458 17 6 25 0 48 3710
Baker 0 352 35 322 2 711 163 87 272 4 526 7 0 4 1 12 1249
Baldwin 0 2717 296 4403 0 7416 2163 818 4343 0 7324 107 20 87 0 214 14954
Banks 0 2275 186 3887 0 6348 242 84 446 0 772 71 11 77 0 159 7279
Barrow 0 8934 662 10531 0 20127 3007 691 4534 0 8232 450 43 400 0 893 29252
Bartow 0 13580 972 14783 0 29335 3182 888 5267 0 9337 570 65 418 0 1053 39725
Ben Hill 0 900 67 2266 0 3233 434 149 1184 0 1767 31 2 40 0 73 5073
Berrien 0 2337 166 2498 0 5001 361 106 441 0 908 41 10 28 0 79 5988
Bibb 0 10647 1021 8511 0 20179 11491 3529 17977 0 32997 415 54 225 0 694 53870
Bleckley 0 1397 140 2137 1 3675 418 117 504 0 1039 32 6 39 0 77 4791
Brantley 0 2598 96 2581 0 5275 202 28 247 0 477 55 1 38 0 94 5846
Brooks 0 1906 77 1532 0 3515 675 262 1056 0 1993 41 6 30 0 77 5585
Bryan 0 3901 245 6995 0 11141 1401 409 3591 0 5401 161 15 203 0 379 16921
Bulloch 0 7780 479 6580 0 14839 2897 759 4526 0 8182 245 20 151 0 416 23437
Burke 0 2440 142 1877 0 4459 1690 450 1739 0 3879 65 8 29 0 102 8440
Butts 0 1661 194 4926 0 6781 546 214 1903 0 2663 52 5 125 0 182 9626
Calhoun 0 336 29 359 0 724 478 100 384 0 962 7 3 1 0 11 1697
Camden 0 4955 333 6407 0 11695 1748 631 3377 0 5756 205 12 200 0 417 17868
Candler 0 735 51 1764 0 2550 265 82 603 0 950 17 2 32 0 51 3551
Carroll 0 15048 650 14133 0 29831 4506 915 7155 0 12576 626 36 425 0 1087 43494
Catoosa 0 7509 413 10623 0 18545 1733 430 3117 0 5280 309 14 235 0 558 24383
Charlton 0 1260 60 1227 5 2552 259 58 463 0 780 22 2 31 0 55 3387
Chatham 198440 22096 1870 17168 0 41134 25009 6466 31384 0 62859 1172 120 519 0 1811 105804
Chattahoochee 0 288 17 233 0 538 170 27 256 0 453 14 1 9 0 24 1015
Chattooga 0 2848 160 2969 0 5977 583 118 811 0 1512 84 11 77 0 172 7661
Cherokee 0 32401 3498 44885 0 80784 11415 3962 19584 0 34961 1943 241 1647 0 3831 119576
Clarke 0 4459 831 5514 0 10804 9512 2811 16228 0 28551 473 55 271 0 799 40154
Clay 0 173 25 329 0 527 222 75 275 0 572 8 3 9 0 20 1119
Clayton 0 3188 587 5662 0 9437 19392 4428 49490 0 73310 482 65 542 0 1089 83836
Clinch 0 735 33 862 0 1630 190 27 222 0 439 14 0 21 0 35 2104
Cobb 0 48601 7609 69114 0 125324 51547 16478 107362 0 175387 4161 585 3681 0 8427 309138
Coffee 0 3309 162 4971 0 8442 1100 235 1878 0 3213 88 11 89 0 188 11843
Colquitt 0 4847 256 4286 0 9389 1128 256 1580 0 2964 90 10 92 0 192 12545
Columbia 0 18774 1291 20088 0 40153 8098 2016 12827 0 22941 749 58 450 0 1257 64351
Cook 0 1626 124 2194 0 3944 554 154 789 0 1497 47 10 39 0 96 5537
Coweta 0 18955 1486 21058 0 41499 6934 1972 11241 0 20147 875 92 706 0 1673 63319
Crawford 0 1645 116 1677 0 3438 342 152 801 0 1295 41 6 31 0 78 4811
Crisp 0 1418 195 2444 0 4057 696 204 1263 0 2163 39 3 42 0 84 6304
Dade 0 2570 93 2032 0 4695 379 70 547 0 996 78 2 47 0 127 5818
Dawson 0 3583 352 7233 0 11168 548 217 1492 0 2257 152 19 236 0 407 13832
Decatur 0 2239 212 2952 0 5403 1078 349 1866 0 3293 45 7 50 0 102 8798
DeKalb 0 17120 2518 22243 0 41881 65344 20091 164629 0 250064 2484 341 2018 0 4843 296788
Dodge 0 2559 192 2143 0 4894 592 182 866 0 1640 44 2 27 0 73 6607
Dooly 0 782 55 947 0 1784 567 151 743 0 1461 19 6 7 0 32 3277
Dougherty 0 4641 325 2783 0 7749 7602 1583 9380 0 18565 168 17 70 0 255 26569
Douglas 0 5887 670 11016 0 17573 9221 2234 22640 0 34095 453 53 492 0 998 52666
Early 0 943 77 1185 0 2205 721 143 786 0 1650 15 3 10 0 28 3883
Echols 0 334 31 571 1 937 24 2 74 0 100 9 1 13 0 23 1060
Effingham 0 9286 360 8574 0 18220 2681 546 3241 0 6468 340 24 240 0 604 25292
Elbert 0 2533 156 2440 0 5129 699 247 1123 0 2069 51 5 34 0 90 7288
Emanuel 13719 2983 169 2170 0 5322 896 199 1062 0 2157 30 3 25 0 58 7537
Evans 0 770 125 1419 0 2314 281 127 560 0 968 19 9 26 0 54 3336
Fannin 0 4952 538 4530 0 10020 652 332 1245 0 2229 138 32 120 0 290 12539
Fayette 0 10865 1172 18125 0 30162 7265 2048 18937 0 28250 728 56 709 0 1493 59905
Floyd 0 11091 700 10094 0 21885 3341 817 4975 0 9133 470 42 301 0 813 31831
Forsyth 0 21319 2336 42298 0 65953 9767 2967 20078 0 32812 1299 136 1542 0 2977 101742
Franklin 0 3609 264 3501 0 7374 466 121 579 0 1166 86 9 69 0 164 8704
Fulton 0 31998 3842 66797 0 102637 77096 18545 211041 0 306682 3552 367 3897 0 7816 417135
Gilmer 0 5169 446 5528 0 11143 765 308 1440 0 2513 141 19 165 0 325 13981
Glascock 0 540 46 585 0 1171 54 10 49 0 113 1 0 10 0 11 1295
Glynn 0 7072 789 12842 7 20710 3106 1256 7435 2 11799 266 30 300 0 596 33105
Gordon 0 7824 402 6717 0 14943 1375 254 1720 0 3349 268 24 183 0 475 18767
Grady 0 2557 183 2638 0 5378 817 252 1480 0 2549 48 6 45 0 99 8026
Greene 0 1776 306 4658 0 6740 1026 383 2111 0 3520 56 9 91 0 156 10416
Gwinnett 0 47652 5790 61468 0 114910 55086 13348 106911 0 175345 3768 503 3042 0 7313 297568
Habersham 0 3993 567 8937 0 13497 707 336 1841 0 2884 178 28 272 0 478 16859
Hall 0 18102 2301 31218 0 51621 5823 2414 10936 0 19173 960 147 1055 0 2162 72956
Hancock 0 385 62 542 0 989 798 419 1154 0 2371 10 5 15 0 30 3390
Haralson 0 5320 323 4143 0 9786 602 139 755 0 1496 189 9 110 0 308 11590
Harris 0 6393 430 5245 0 12068 1682 477 2383 0 4542 206 13 119 0 338 16948
Hart 0 3589 243 4145 0 7977 805 232 1282 0 2319 84 7 101 0 192 10488
Heard 0 2073 203 1396 0 3672 266 119 289 0 674 49 15 28 0 92 4438
Henry 0 11400 1087 20330 0 32817 14491 3783 42371 0 60645 717 87 851 0 1655 95117
Houston 0 11028 1279 19918 0 32225 6799 2392 16438 0 25629 470 72 553 0 1095 58949
Irwin 0 1123 60 1417 1 2601 273 59 421 4 757 26 2 18 0 46 3404
Jackson 0 8870 720 14783 0 24373 2015 621 3862 0 6498 381 32 409 0 822 31693
Jasper 0 2317 163 2464 0 4944 480 199 785 0 1464 54 10 56 0 120 6528
Jeff Davis 0 1871 74 1792 0 3737 265 58 415 0 738 26 2 24 0 52 4527
Jefferson 0 1578 123 1339 0 3040 1236 384 1538 0 3158 33 6 15 0 54 6252
Jenkins 0 766 75 983 0 1824 292 65 555 0 912 17 2 6 0 25 2761
Johnson 0 1105 105 1272 0 2482 254 94 521 0 869 12 0 5 0 17 3368
Jones 0 3693 298 4098 0 8089 1254 499 2236 1 3990 113 10 68 0 191 12270
Lamar 0 4087 153 1170 0 5410 1228 284 626 0 2138 123 4 31 0 158 7706
Lanier 0 719 35 1106 0 1860 255 40 430 0 725 18 3 17 0 38 2623
Laurens 0 6718 490 4856 0 12064 2364 761 3158 0 6283 87 19 49 0 155 18502
Lee 0 4598 192 4742 0 9532 1473 263 2039 0 3775 122 9 90 0 221 13528
Liberty 0 2203 199 3085 0 5487 2273 923 6408 0 9604 115 19 129 0 263 15354
Lincoln 0 1274 94 1478 1 2847 368 178 531 0 1077 25 4 20 0 49 3973
Long 0 1331 48 1374 0 2753 540 67 936 0 1543 54 1 40 0 95 4391
Lowndes 0 7805 490 11865 0 20160 3630 1067 9094 0 13791 278 31 236 0 545 34496
Lumpkin 0 3148 331 6280 0 9759 555 261 1720 0 2536 146 19 203 0 368 12663
Macon 0 524 37 899 0 1460 799 197 1273 0 2269 18 1 13 0 32 3761
Madison 0 4433 341 4571 0 9345 1133 299 1429 0 2861 149 17 96 0 262 12468
Marion 0 950 54 746 0 1750 287 159 572 0 1018 31 6 29 0 66 2834
McDuffie 0 1613 219 3235 0 5067 787 477 1837 0 3101 70 5 55 0 130 8298
McIntosh 0 1186 154 2004 0 3344 574 231 1242 1 2048 51 6 38 0 95 5487
Meriwether 0 2955 170 2209 0 5334 1542 390 1429 0 3361 100 6 69 0 175 8870
Miller 0 659 80 878 0 1617 211 54 244 0 509 11 2 9 0 22 2148
Mitchell 0 2053 168 1763 2 3986 1040 396 1517 0 2953 41 7 33 0 81 7020
Monroe 0 4454 201 5014 0 9669 1194 387 2154 0 3735 140 6 109 0 255 13659
Montgomery 0 1418 58 974 0 2450 308 56 378 0 742 18 6 10 0 34 3226
Morgan 0 2967 186 4162 0 7315 779 265 1714 0 2758 91 5 78 0 174 10247
Murray 0 5214 165 4357 0 9736 723 106 704 0 1533 120 4 75 0 199 11468
Muscogee 0 9546 1159 10778 0 21483 9935 4126 22746 0 36807 500 51 334 0 885 59175
Newton 0 9339 770 7437 0 17546 7667 2345 14297 0 24309 451 54 276 0 781 42636
Oconee 0 4357 505 9815 0 14677 1686 690 4608 0 6984 292 23 300 0 615 22276
Oglethorpe 0 1692 226 2718 0 4636 619 223 1153 0 1995 58 20 74 0 152 6783
Paulding 0 14674 1598 24375 0 40647 7237 2214 14885 0 24336 707 94 831 0 1632 66615
Peach 0 1650 130 3369 0 5149 1201 403 3091 0 4695 57 11 75 0 143 9987
Pickens 0 5263 328 6455 2 12048 788 302 1498 0 2588 176 17 171 0 364 15000
Pierce 0 2053 185 4087 0 6325 186 73 520 0 779 25 3 35 0 63 7167
Pike 0 3398 194 4355 0 7947 403 166 688 0 1257 93 4 94 0 191 9395
Polk 0 3837 273 6288 0 10398 782 246 1763 0 2791 156 19 167 0 342 13531
Pulaski 0 655 101 1562 0 2318 265 118 618 0 1001 22 1 21 0 44 3363
Putnam 0 2536 318 4298 0 7152 771 370 1701 0 2842 73 12 106 0 191 10185
Quitman 0 186 20 305 0 511 146 36 196 0 378 5 1 7 0 13 902
Rabun 0 1396 415 4368 0 6179 321 254 1229 0 1804 57 20 146 0 223 8206
Randolph 0 492 37 628 0 1157 484 193 688 0 1365 10 1 12 0 23 2545
Richmond 0 11094 883 7510 0 19487 16022 3866 23614 0 43502 547 70 263 0 880 63869
Rockdale 0 3318 419 4971 0 8708 6021 1753 17647 0 25421 239 46 287 0 572 34701
Schley 0 501 44 922 0 1467 94 54 236 0 384 5 1 13 0 19 1870
Screven 0 1531 71 1599 0 3201 694 151 1156 0 2001 36 3 33 0 72 5274
Seminole 0 1257 63 831 0 2151 362 54 411 0 827 32 4 8 0 44 3022
Spalding 0 5878 567 7514 0 13959 3129 1009 5612 0 9750 270 40 224 0 534 24243
Stephens 0 1873 324 5177 0 7374 409 190 1161 0 1760 59 15 123 0 197 9331
Stewart 0 277 35 320 0 632 358 141 416 0 915 7 0 6 0 13 1560
Sumter 0 1986 180 2420 0 4586 1336 646 2908 0 4890 46 2 48 0 96 9572
Talbot 0 634 39 445 0 1118 769 229 690 0 1688 26 1 16 0 43 2849
Taliaferro 0 134 14 162 0 310 153 94 235 1 483 2 0 2 0 4 797
Tattnall 0 2053 142 2670 0 4865 495 134 896 0 1525 59 11 39 0 109 6499
Taylor 0 659 70 1285 6 2020 384 165 588 8 1145 13 0 22 0 35 3200
Telfair 0 1120 102 1118 0 2340 429 133 470 0 1032 19 3 6 0 28 3400
Terrell 0 886 77 732 0 1695 676 253 974 0 1903 19 4 16 0 39 3637
Thomas 0 3870 280 6326 0 10476 1889 524 4018 0 6431 127 15 120 0 262 17169
Tift 0 3601 184 5129 0 8914 1272 326 2274 0 3872 92 6 78 0 176 12962
Toombs 0 2305 251 3740 0 6296 617 282 1160 0 2059 45 4 51 0 100 8455
Towns 0 1539 275 3751 0 5565 186 210 962 0 1358 57 15 106 0 178 7101
Treutlen 0 644 58 1017 2 1721 187 81 440 2 710 4 0 8 0 12 2443
Troup 0 6870 508 6549 0 13927 3558 840 4399 0 8797 247 23 156 0 426 23150
Turner 0 628 30 1231 0 1889 364 60 727 0 1151 13 2 27 0 42 3082
Twiggs 0 1069 51 766 0 1886 625 277 694 0 1596 28 6 11 0 45 3527
Union 0 4603 409 6170 0 11182 591 333 1696 0 2620 175 22 181 0 378 14180
Upson 0 2197 248 4239 0 6684 869 383 1919 0 3171 85 18 102 0 205 10060
Walker 0 8060 562 8431 0 17053 1725 488 2164 0 4377 315 26 190 0 531 21961
Walton 0 13332 921 16291 0 30544 3468 895 6328 0 10691 478 61 465 0 1004 42239
Ware 0 3410 331 3822 0 7563 716 361 1899 0 2976 62 14 85 0 161 10700
Warren 0 318 63 626 0 1007 392 141 583 0 1116 12 0 13 0 25 2148
Washington 0 1789 175 1937 0 3901 1351 534 1906 0 3791 35 5 26 0 66 7758
Wayne 0 3689 276 3872 0 7837 618 141 1253 0 2012 71 11 54 0 136 9985
Webster 0 205 60 411 0 676 166 73 202 0 441 3 2 2 0 7 1124
Wheeler 0 585 47 632 2 1266 214 42 277 1 534 10 1 6 0 17 1817
White 0 4360 408 5479 0 10247 590 224 1248 0 2062 177 22 148 0 347 12656
Whitfield 0 11685 544 7140 0 19369 3363 480 3050 0 6893 369 21 207 0 597 26859
Wilcox 0 1019 71 857 0 1947 301 44 299 0 644 11 2 9 0 22 2613
Wilkes 0 1122 73 1206 1 2402 598 189 892 1 1680 16 2 28 0 46 4128
Wilkinson 0 1561 74 636 0 2271 741 203 783 1 1728 31 2 10 0 43 4042
Worth 0 3187 102 2121 6 5416 680 181 979 2 1842 52 3 45 0 100 7358
Total: 212159 795387 74322 1036626 39 1906374 575944 164282 1201462 28 1941716 40506 4784 35897 1 81188 3929278
library(tidyverse)
library(cartogram)
library(sf)
library(tmap)
library(gganimate)
library(tweenr)
library(transformr)
library(gifski)
#Set color scheme and get county boundaries
pal<-c("#4175b5","#b8cae0","#f2f7f3","#e38a84","#a11208")
ga_cty<-tigris::counties(state="GA")
#Read and clean election data
#Downloaded from Georgia Secretary of State and saved the senate tab
election1<-read_csv("walkerwarnock_2022.csv")[1,] %>%
pivot_longer(3:18,names_to="var1",values_to="values1") %>%
select(4) %>%
fill(values1)
election2<-read_csv("walkerwarnock_2022.csv",skip=2) %>%
pivot_longer(`Election Day Votes...3`:Total,names_to="vars",values_to="values")
#Two row headers, so combined those
names1<-paste(election2$vars,election1$values1)
election2$fullname<-names1
election<-election2 %>%
select(-vars) %>%
filter(str_detect(fullname,"Total")) %>%
pivot_wider(names_from=fullname,values_from="values")
#Make the names shorter
names(election)<-c("county","voters","walker_tot","warnock_tot","oliver_tot","total")
#Calculate rates
election<-election %>%
mutate(pct_walker=walker_tot/total*100,
pct_warnock=warnock_tot/total*100)
#Join to sf data
election_sf<-ga_cty %>%
rename(county=NAME) %>%
left_join(election) %>%
st_transform(32616)
#Create the cartogram based on total votes
ga_carto<-cartogram_cont(election_sf,"total",itermax=7)
tm_shape(ga_carto)+tm_borders()
# Calculate the transition between these 2 objects
dt <- tween_sf(election_sf,ga_carto,ease='cubic-in-out', nframes = 50)
#Create a function to plot the data in tmap
sfplot <- function(frame_sel) {
data_map<-dt %>%
filter(.frame==frame_sel) %>%
st_as_sf()
p <- tm_shape(data_map)+
tm_polygons("pct_walker",breaks=c(0,30,45,55,70,100),palette=pal,
title="Pct. voting for Walker")+
tm_layout(main.title="Nov. 2022 Senate election in Georgia",
frame=FALSE,
main.title.position = "center")+
tm_legend(position = c(0.7,0.8))+
tm_credits("Data: Georgia SOS | Map by @jerry_shannon",
position=c("left","bottom"),
size=0.9)
tmap_save(p,paste("election_map/election_frame_",str_pad(frame_sel,2,pad=0),".png",sep=""))
}
#Calculate the number of frames and save jpg versions
#sfplot(40)
frames<-unique(dt$.frame)
map(frames,sfplot)
#Create a gif with gifski
files<-list.files(path="election_map",full.names = TRUE)
files1<-rev(files) #Reverse the order to go back
files2<-c(files,files1) #Combine the lists
gifski(files2,gif_file="warnockwalker.gif",loop=TRUE,delay=0.1,width=664,height=737)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment