A DataViz Weekly Medium post pointing to a nice roundup of 2024 U.S. Election maps, as well as Flourish's neat 16 Ways to Visualize US Election Data, prompted my above SAS ODS Graphics state-by-state hexagon recap of the 2024 Presidential election, which uses hexagonal images of Kamala Harris and Donald Trump (created using MS-Word - insert hexagon shape, rotate 90 degrees, insert cropped-to-fit width-and-height photos from Wikipedia as shape fill, save shapes as .png files). And that's the last of my 2024 Election Charts - back to Holiday-themed charts this week for Thanksgiving!
* Fun With SAS ODS Graphics: Hexagon Map State-by-State 2024 Election Recap;
* Get Electoral College Votes and Most Recent Results by State
https://www.nbcnews.com/politics/2024-elections/president-results;
proc import datafile='/home/ted.conway/PresidentialElectionResults2024.xlsx' out=ElectionResults dbms=xlsx replace;
* Get state name to state code mapping;
proc import datafile='/home/ted.conway/state2statecd.xlsx' out=state2statecd dbms=xlsx replace;
proc sql; * Append state abbreviation (special join logic for split states, DC);
create table StateCodesElectionResults as
select statecd, winner, votes from
(select state, case when harris>trump then 'Harris' else 'Trump' end as Winner, sum(votes) as Votes
from ElectionResults group by 1, 2) er
left join state2statecd s2c on translate(er.state,' ','123')=s2c.state or scan(er.state,-1,' ,')=s2c.statecd
order by statecd, votes desc;
data StateCodesElectionResultsDeduped; * De-dupe split states, pick candidate with most votes as the winner;
set StateCodesElectionResults;
by statecd;
if first.statecd;
data StateGrid; * Get state x/y coordinates for hexagon (courtesy of Matt Chambers, sirvizalot.com/2016/05/how-to-small-multiple-tile-map-in.html);
input statecode : $2. row column@@;
row=-row; * Flip map's y-coordinates (FL at bottom, ME at top!);
ty=row-.5;
cards;
AK 0 0.5 ME 0 11.5
VT 1 10 NH 1 11
WA 2 1.5 MT 2 2.5 ND 2 3.5 MN 2 4.5 WI 2 5.5 MI 2 7.5 NY 2 9.5 MA 2 10.5 RI 2 11.5
ID 3 2 WY 3 3 SD 3 4 IA 3 5 IL 3 6 IN 3 7 OH 3 8 PA 3 9 NJ 3 10 CT 3 11
OR 4 1.5 NV 4 2.5 CO 4 3.5 NE 4 4.5 MO 4 5.5 KY 4 6.5 WV 4 7.5 MD 4 8.5 DE 4 9.5
CA 5 2 AZ 5 3 UT 5 4 KS 5 5 AR 5 6 TN 5 7 VA 5 8 NC 5 9 DC 5 12
NM 6 3.5 OK 6 4.5 LA 6 5.5 MS 6 6.5 AL 6 7.5 SC 6 8.5
TX 7 4 GA 7 8
HI 8 0.5 FL 8 8.5
;
run;
proc sql; * Join state vote winner data with hexagon state x/y coordinates;
create table states2map as /* Highlight states that flipped from Democrat to Republican in 2024 with plus (+) signs */
select *, case when statecd in ('AZ', 'GA', 'MI', 'NV', 'PA', 'WI') then '+'||trim(statecd)||'+' else statecd end as statecd2
from StateCodesElectionResultsDeduped t1
join stategrid t2 on t1.statecd=t2.statecode
order by t1.statecd;
data myattrmap; * Specify images, colors assoicated with candidates;
input ID $ value $ markersymbol $ textcolor $;
datalines;
myid Harris harris blue
myid Trump trump red
; * Let's create a hexagon map (scatter/text plots using images of candidates)!;
ods graphics / width=21.5in height=16in noborder imagefmt=png;
proc sgplot data=states2map nowall dattrmap=myattrmap noautolegend noborder;
inset " " "2024 U.S. Presidential Election Results" / textattrs=(size=32pt) position=top;
inset "Plus signs (+) denote six states that flipped to Republican in 2024" / position=bottomright textattrs=(size=14pt);
symbolimage name=harris image='/home/ted.conway/harrishex.png'; * Kamala Harris photo (source: en.wikipedia.org/wiki/2024_United_States_presidential_election);
symbolimage name=trump image='/home/ted.conway/trumphex.png'; * Donald Trump photo (source: en.wikipedia.org/wiki/2024_United_States_presidential_election);
scatter x=column y=row / markerattrs=(size=78pt) attrid=myid group=winner; * Plot candidate images;
text x=column y=ty text=statecd2 / position=bottom attrid=myid group=winner textattrs=(size=12pt weight=bold) contributeoffsets=none strip; * Add state abbreviations below images;
xaxis display=none values=(.25 to 12.25); * Suppress axes, limit bounds of axes;
yaxis display=none values=(0 to -9);
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.