<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Fun With SAS ODS Graphics - Halloween 2020 Electoral Map in Graphics Programming</title>
    <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/695704#M20656</link>
    <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Halloween 2020 Electoral Map" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51279i25319FB1B4F7CE26/image-size/large?v=v2&amp;amp;px=999" role="button" title="PresidentialElection.png" alt="Halloween 2020 Electoral Map" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Halloween 2020 Electoral Map&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To mark Halloween 2020, here are the 10-31&amp;nbsp;&lt;A href="https://www.cnn.com/election/2020/electoral-college-interactive-maps#2020-CNN-ratings" target="_self"&gt;predicted electoral votes from CNN&lt;/A&gt; presented in a cartogram scatter plot of jittered pumpkin markers from &lt;A href="https://thenounproject.com/term/halloween-pumpkin/3435605/" target="_self"&gt;the Noun Project&lt;/A&gt;&amp;nbsp;together with a bar chart summary. Happy Halloween!&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SAS CODE&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Fun With SAS ODS Graphics - Halloween 2020 Electoral Map
  Data courtesy of CNN - cnn.com/election/2020/electoral-college-interactive-maps 
  Pumpkin icon courtesy of Saepul Nahwan - thenounproject.com/term/halloween-pumpkin/3435605/;

*==&amp;gt; Get CNN.com state-level election prediction data (JSON format);
 
filename cnn '/folders/myfolders/CNN2020JSON20201031.txt';
libname j json fileref=cnn;
proc sql;
create table CNNpredictions as 
select statecode, v.electoralVotes,
case when v.party='Dem' &amp;amp; v.type='win' then 'WIN (D)' when v.party='Dem' &amp;amp; v.type='lean' then 'LEAN (D)' 
     when v.party='Rep' &amp;amp; v.type='win' then 'WIN (R)' when v.party='Rep' &amp;amp; v.type='lean' then 'LEAN (R)' 
     when v.party='Tossup' then 'TOSSUP' else trim(v.party)||' '||v.type 
end as Prediction
from j.root j join j.electoralvotes v on j.ordinal_root=v.ordinal_root 
order by statecode, prediction;

*==&amp;gt; Generate USA cartogram map x/y coordinates from inline layout of state codes;

data states(keep=statecode x y y2);
input states $char80.;
y+1;
y2=y-.47;                            * y-axis location of state name;
x=mod(y-1,2)*.5;                     * Offset alternate rows of states by .5;
do c=1+mod(y-1,2)*2 to 80 by 4;
  statecode=substr(states,c,2);
  x+1; 
  if statecode^='' then output; 
end;
datalines;
AK                                          ME
                                      VT, NH
    WA, MT, ND, MN, WI,     MI,     NY, MA, RI
      ID, WY, SD, IA, IL, IN, OH, PA, NJ, CT
    OR, NV, CO, NE, MO, KY, WV, MD, DE
      CA, AZ, UT, KS, AR, TN, VA, NC,         DC       
            NM, OK, LA, MS, AL, SC
              TX              GA
HI                              FL    
;
proc sort data=states; by statecode;

*==&amp;gt; Create one observation per electoral vote, add sort seq;

data statesvotes(drop=v);
merge states cnnpredictions;
by statecode;                       * Assign sort sequence for bar chart;
sortseq=index('WIN (D), LEAN (D), TOSSUP, LEAN (R), WIN (R)', trim(prediction));
dummyvar='D';
do v=1 to electoralvotes;
  output;
end;

proc sort data=statesvotes; by sortseq; * Order for bar chart;

*==&amp;gt; Map and bar charts of predicted voting results (Scatter+Ellipse+Text+Bar plots);

ods listing image_dpi=300 gpath='/folders/myfolders';
ods graphics on / reset antialias width=11in height=8.5in imagename="PresidentialElection";

proc template;
define statgraph ustemplate;
  begingraph / border=true;         * Define Dem/Rep color and image attributes;
  symbolimage name=symPLD image="/folders/myfolders/PumpkinPaleBlue.png"; 
  symbolimage name=symPWD image="/folders/myfolders/PumpkinBlue.png";
  symbolimage name=symPLR image="/folders/myfolders/PumpkinLightRed.png";
  symbolimage name=symPWR image="/folders/myfolders/PumpkinRed.png";
  symbolimage name=symPTU image="/folders/myfolders/PumpkinOrange.png";
  discreteattrmap name="symbols" / ignorecase=true trimleading=true; 
    value "LEAN (D)" / markerattrs=(symbol=symPLD size=16pt) fillattrs=(color=cxA3A3FF);
    value "WIN (D)" / markerattrs=(symbol=symPWD size=16pt) fillattrs=(color=cx0000ff);
    value "LEAN (R)" / markerattrs=(symbol=symPLR size=16pt) fillattrs=(color=cxf1959b);
    value "WIN (R)" / markerattrs=(symbol=symPWR size=16pt) fillattrs=(color=cxff0000);
    value "TOSSUP" / markerattrs=(symbol=symPTU size=16pt) fillattrs=(color=cxFF9900);
  enddiscreteattrmap;              * Define custom legend for markers;
  discreteattrvar attrvar=groupmarkers var=prediction attrmap="symbols";
  legendItem type=marker name="pwd" / markerattrs=(symbol=symPWD size=28pt) label="WIN (D)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="pld" / markerattrs=(symbol=symPLD size=28pt) label="LEAN (D)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="ptu" / markerattrs=(symbol=symPTU size=28pt) label="TOSSUP" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="plr" / markerattrs=(symbol=symPLR size=28pt) label="LEAN (R)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="pwr" / markerattrs=(symbol=symPWR size=28pt) label="WIN (R)" labelattrs=(size=9pt weight=bold);

  layout lattice / border=false rows=2 rowweights=(0.95 0.05);   * Cartogram map + scatterplot of all electoral votes;

  layout overlayequated / border=false xaxisopts=(display=none) yaxisopts=(display=none reverse=true) walldisplay=none;
    entry "Halloween 2020 Electoral Map (CNN 10/31 Data)" / valign=top textattrs=(size=16pt weight=bold);
    scatterplot x=x y=y /  jitter=auto jitteropts=(width=1) group=groupmarkers;
    ellipseparm semiminor=.48 semimajor=.48 xorigin=x yorigin=y slope=0 / outlineattrs=(color=black);
    textplot x=x y=y2 text=statecode /  position=top textattrs=(color=black size=9pt);
    discretelegend "pwd" "pld" "ptu" "plr" "pwr" / location=inside border=false valign=bottom;
  endlayout;
                                                                 * Bar chart summary of electoral votes;
  layout overlay / border=false xaxisopts=(display=none) yaxisopts=(display=none reverse=true) walldisplay=none;
    barchart category=dummyvar / stat=freq display=(fill) displaybaseline=off group=groupmarkers groupdisplay=stack orient=horizontal segmentlabel=true segmentlabelattrs=(size=10pt color=white weight=bold);
    referenceline x=270 / lineattrs=(color=white);               * Takes 270 votes to win!;
  endlayout;

  endlayout;
  endgraph;
end;
run;

proc sgrender data=statesvotes template=ustemplate;              * Generate the charts!;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;STATE DETAIL&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Oregon Detail" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51282i16F8760A788A07ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="OR.png" alt="Oregon Detail" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Oregon Detail&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CNN JSON Snippet&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;[{"stateCode":"AL","type":"win","party":"Rep","electoralVotes":[{"type":"win","party":"Rep","electoralVotes":9}]},&lt;BR /&gt;{"stateCode":"AK","type":"win","party":"Rep","electoralVotes":[{"type":"win","party":"Rep","electoralVotes":3}]},&lt;BR /&gt;{"stateCode":"AZ","type":"lean","party":"Dem","battleground":true,"electoralVotes":[{"type":"lean","party":"Dem","electoralVotes":11}]},&lt;BR /&gt;...&lt;/PRE&gt;</description>
    <pubDate>Sun, 01 Nov 2020 01:47:11 GMT</pubDate>
    <dc:creator>tc</dc:creator>
    <dc:date>2020-11-01T01:47:11Z</dc:date>
    <item>
      <title>Fun With SAS ODS Graphics - Halloween 2020 Electoral Map</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/695704#M20656</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Halloween 2020 Electoral Map" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51279i25319FB1B4F7CE26/image-size/large?v=v2&amp;amp;px=999" role="button" title="PresidentialElection.png" alt="Halloween 2020 Electoral Map" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Halloween 2020 Electoral Map&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To mark Halloween 2020, here are the 10-31&amp;nbsp;&lt;A href="https://www.cnn.com/election/2020/electoral-college-interactive-maps#2020-CNN-ratings" target="_self"&gt;predicted electoral votes from CNN&lt;/A&gt; presented in a cartogram scatter plot of jittered pumpkin markers from &lt;A href="https://thenounproject.com/term/halloween-pumpkin/3435605/" target="_self"&gt;the Noun Project&lt;/A&gt;&amp;nbsp;together with a bar chart summary. Happy Halloween!&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SAS CODE&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Fun With SAS ODS Graphics - Halloween 2020 Electoral Map
  Data courtesy of CNN - cnn.com/election/2020/electoral-college-interactive-maps 
  Pumpkin icon courtesy of Saepul Nahwan - thenounproject.com/term/halloween-pumpkin/3435605/;

*==&amp;gt; Get CNN.com state-level election prediction data (JSON format);
 
filename cnn '/folders/myfolders/CNN2020JSON20201031.txt';
libname j json fileref=cnn;
proc sql;
create table CNNpredictions as 
select statecode, v.electoralVotes,
case when v.party='Dem' &amp;amp; v.type='win' then 'WIN (D)' when v.party='Dem' &amp;amp; v.type='lean' then 'LEAN (D)' 
     when v.party='Rep' &amp;amp; v.type='win' then 'WIN (R)' when v.party='Rep' &amp;amp; v.type='lean' then 'LEAN (R)' 
     when v.party='Tossup' then 'TOSSUP' else trim(v.party)||' '||v.type 
end as Prediction
from j.root j join j.electoralvotes v on j.ordinal_root=v.ordinal_root 
order by statecode, prediction;

*==&amp;gt; Generate USA cartogram map x/y coordinates from inline layout of state codes;

data states(keep=statecode x y y2);
input states $char80.;
y+1;
y2=y-.47;                            * y-axis location of state name;
x=mod(y-1,2)*.5;                     * Offset alternate rows of states by .5;
do c=1+mod(y-1,2)*2 to 80 by 4;
  statecode=substr(states,c,2);
  x+1; 
  if statecode^='' then output; 
end;
datalines;
AK                                          ME
                                      VT, NH
    WA, MT, ND, MN, WI,     MI,     NY, MA, RI
      ID, WY, SD, IA, IL, IN, OH, PA, NJ, CT
    OR, NV, CO, NE, MO, KY, WV, MD, DE
      CA, AZ, UT, KS, AR, TN, VA, NC,         DC       
            NM, OK, LA, MS, AL, SC
              TX              GA
HI                              FL    
;
proc sort data=states; by statecode;

*==&amp;gt; Create one observation per electoral vote, add sort seq;

data statesvotes(drop=v);
merge states cnnpredictions;
by statecode;                       * Assign sort sequence for bar chart;
sortseq=index('WIN (D), LEAN (D), TOSSUP, LEAN (R), WIN (R)', trim(prediction));
dummyvar='D';
do v=1 to electoralvotes;
  output;
end;

proc sort data=statesvotes; by sortseq; * Order for bar chart;

*==&amp;gt; Map and bar charts of predicted voting results (Scatter+Ellipse+Text+Bar plots);

ods listing image_dpi=300 gpath='/folders/myfolders';
ods graphics on / reset antialias width=11in height=8.5in imagename="PresidentialElection";

proc template;
define statgraph ustemplate;
  begingraph / border=true;         * Define Dem/Rep color and image attributes;
  symbolimage name=symPLD image="/folders/myfolders/PumpkinPaleBlue.png"; 
  symbolimage name=symPWD image="/folders/myfolders/PumpkinBlue.png";
  symbolimage name=symPLR image="/folders/myfolders/PumpkinLightRed.png";
  symbolimage name=symPWR image="/folders/myfolders/PumpkinRed.png";
  symbolimage name=symPTU image="/folders/myfolders/PumpkinOrange.png";
  discreteattrmap name="symbols" / ignorecase=true trimleading=true; 
    value "LEAN (D)" / markerattrs=(symbol=symPLD size=16pt) fillattrs=(color=cxA3A3FF);
    value "WIN (D)" / markerattrs=(symbol=symPWD size=16pt) fillattrs=(color=cx0000ff);
    value "LEAN (R)" / markerattrs=(symbol=symPLR size=16pt) fillattrs=(color=cxf1959b);
    value "WIN (R)" / markerattrs=(symbol=symPWR size=16pt) fillattrs=(color=cxff0000);
    value "TOSSUP" / markerattrs=(symbol=symPTU size=16pt) fillattrs=(color=cxFF9900);
  enddiscreteattrmap;              * Define custom legend for markers;
  discreteattrvar attrvar=groupmarkers var=prediction attrmap="symbols";
  legendItem type=marker name="pwd" / markerattrs=(symbol=symPWD size=28pt) label="WIN (D)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="pld" / markerattrs=(symbol=symPLD size=28pt) label="LEAN (D)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="ptu" / markerattrs=(symbol=symPTU size=28pt) label="TOSSUP" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="plr" / markerattrs=(symbol=symPLR size=28pt) label="LEAN (R)" labelattrs=(size=9pt weight=bold);
  legendItem type=marker name="pwr" / markerattrs=(symbol=symPWR size=28pt) label="WIN (R)" labelattrs=(size=9pt weight=bold);

  layout lattice / border=false rows=2 rowweights=(0.95 0.05);   * Cartogram map + scatterplot of all electoral votes;

  layout overlayequated / border=false xaxisopts=(display=none) yaxisopts=(display=none reverse=true) walldisplay=none;
    entry "Halloween 2020 Electoral Map (CNN 10/31 Data)" / valign=top textattrs=(size=16pt weight=bold);
    scatterplot x=x y=y /  jitter=auto jitteropts=(width=1) group=groupmarkers;
    ellipseparm semiminor=.48 semimajor=.48 xorigin=x yorigin=y slope=0 / outlineattrs=(color=black);
    textplot x=x y=y2 text=statecode /  position=top textattrs=(color=black size=9pt);
    discretelegend "pwd" "pld" "ptu" "plr" "pwr" / location=inside border=false valign=bottom;
  endlayout;
                                                                 * Bar chart summary of electoral votes;
  layout overlay / border=false xaxisopts=(display=none) yaxisopts=(display=none reverse=true) walldisplay=none;
    barchart category=dummyvar / stat=freq display=(fill) displaybaseline=off group=groupmarkers groupdisplay=stack orient=horizontal segmentlabel=true segmentlabelattrs=(size=10pt color=white weight=bold);
    referenceline x=270 / lineattrs=(color=white);               * Takes 270 votes to win!;
  endlayout;

  endlayout;
  endgraph;
end;
run;

proc sgrender data=statesvotes template=ustemplate;              * Generate the charts!;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;STATE DETAIL&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Oregon Detail" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51282i16F8760A788A07ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="OR.png" alt="Oregon Detail" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Oregon Detail&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CNN JSON Snippet&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;[{"stateCode":"AL","type":"win","party":"Rep","electoralVotes":[{"type":"win","party":"Rep","electoralVotes":9}]},&lt;BR /&gt;{"stateCode":"AK","type":"win","party":"Rep","electoralVotes":[{"type":"win","party":"Rep","electoralVotes":3}]},&lt;BR /&gt;{"stateCode":"AZ","type":"lean","party":"Dem","battleground":true,"electoralVotes":[{"type":"lean","party":"Dem","electoralVotes":11}]},&lt;BR /&gt;...&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 01:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/695704#M20656</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2020-11-01T01:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics - Halloween 2020 Electoral Map</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/696556#M20691</link>
      <description>&lt;P&gt;Thanks a bunch for sharing the ebullient graph!&lt;/P&gt;&lt;P&gt;I tried to play your SAS code but have a difficulty to get input file named "CNN2020JSON20201031.txt". Could you shed some lights on it?&lt;/P&gt;&lt;P&gt;Thx.&lt;/P&gt;&lt;P&gt;Ethan&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2020 15:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/696556#M20691</guid>
      <dc:creator>t75wez1</dc:creator>
      <dc:date>2020-11-04T15:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics - Halloween 2020 Electoral Map</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/697044#M20704</link>
      <description>&lt;P&gt;Sure, it was just the 2020 state prediction data from CNN. To obtain the data, I did a "View Source" on the web page at&amp;nbsp;&lt;A href="https://www.cnn.com/election/2020/electoral-college-interactive-maps" target="_blank"&gt;https://www.cnn.com/election/2020/electoral-college-interactive-maps&lt;/A&gt;&amp;nbsp;on 10/31, located the JSON data for the map, and simply copied-and-pasted the state entries into a text file, which I've uploaded (see attached file). Good luck!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2020 04:39:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Halloween-2020-Electoral-Map/m-p/697044#M20704</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2020-11-06T04:39:22Z</dc:date>
    </item>
  </channel>
</rss>

