<?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 Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden? in Graphics Programming</title>
    <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699471#M20734</link>
    <description>&lt;P&gt;Instead of a stacked bar graph inside a glass, could you do side-by-side bars inside beer bottles, and audio annotate with&amp;nbsp;&lt;EM&gt;100 bottles of beer on the wall....&lt;/EM&gt; for the bumpy bus ride we are on.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Nov 2020 12:54:08 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-11-17T12:54:08Z</dc:date>
    <item>
      <title>Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/698932#M20725</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="A &amp;quot;Layered-Cocktail&amp;quot; Look at State Vote Totals" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51688i290D0A6572A7EC7F/image-size/large?v=v2&amp;amp;px=999" role="button" title="PresidentialElection.png" alt="A &amp;quot;Layered-Cocktail&amp;quot; Look at State Vote Totals" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;A "Layered-Cocktail" Look at State Vote Totals&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Yet another election map, this one a cartogram of state "glasses" showing the percentage of combined Trump and Biden votes that went to each candidate. Reference lines provided to help show when a candidate exceeds the "half-full" mark. Voting data as of 11-14 &lt;A href="https://www.nytimes.com/interactive/2020/11/03/us/elections/results-president.html" target="_self"&gt;from the New York Times&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE: &lt;/STRONG&gt;Modified 11-14 at 10:04 CT to fix Trump/Biden color assignment code. Output was unchanged though - managed to make two mistakes that canceled each other out. &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Fun With SAS ODS Graphics - Is the Glass Half-Trump or Half-Biden? (Yet Another Election Map)
  Data courtesy of NY Times  - nytimes.com/interactive/2020/11/03/us/elections/results-president.html;

*==&amp;gt; Get NY Times state-level Presidential election data (JSON format);
 
filename nyt '/folders/myfolders/NYTjsonResultsAsOf20201114.txt';
libname j json fileref=nyt;
                                      * Calculate % of votes for Biden &amp;amp; Trump; 
proc sql;                             * Dnominator is total Biden + Trump votes (others excluded); 
create table StateVotes as              
select r.state_id, 
sum(case when c.name_display='Joseph R. Biden Jr.' then c.votes end) as BidenVotes,
sum(case when c.name_display='Donald J. Trump' then c.votes end) as TrumpVotes,
calculated BidenVotes/(calculated BidenVotes+calculated TrumpVotes) as PctBiden,
calculated TrumpVotes/(calculated BidenVotes+calculated TrumpVotes) as PctTrump
from j.races r 
left join j.races_candidates c on r.ordinal_races=c.ordinal_races
group by state_id order by r.state_id;

*==&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;                                * 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; Merge voting data and map points,
     Generate points for polygons (rectangles) to make "glasses" showing each candidate's share,
     Generate points for ellipses for top (Trump) and bottom (Biden) of "glasses"; 

data statevotespolygons;
merge states statevotes(rename=(state_id=statecode));
by statecode;                        * Assign sort sequence for bar chart;
eyR=y-.45; output; eyR=.;            * Y-axis points for ellipses at top and bottom of "glasses"; 
eyD=y+.45; output; eyD=.;            * Glasses" occupy .96 of height (2*.45 for rectangles + 2*.03 for ellipses);         
candidate="Biden";                    
p+1;                                 * Generate polygon points for Biden;
px=x-.48; py=y+.45; output; px=x+.48; output;     * Top left/right;
py=y+.45-pctBiden*.9; output; px=x-.48; output;   * Bottom left/right;
candidate="Trump";                   * Generate polygon points for Trump;
p+1;
px=x-.48; py=y-.45; output; px=x+.48; output;     * Top left/right;
py=y-.45+pctTrump*.9; output; px=x-.48; output;   * Bottom left/right;

*==&amp;gt; Map and bar charts of vote shares - polygon + ellipse + text 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 / subpixel=on;          * Define Dem/Rep color attributes;
  discreteattrmap name="candidate"; 
    value "Trump" / fillattrs=(color=red); 
    value "Biden" / fillattrs=(color=blue); 
  enddiscreteattrmap;
  discreteattrvar attrvar=candidatecolor var=candidate attrmap="candidate";
  layout overlayequated / xaxisopts=(display=none thresholdmin=0 thresholdmax=0) yaxisopts=(display=none reverse=true);
    layout gridded / columns=1 valign=top;  * Titles (Insets);
      entry textattrs=(size=24pt weight=bold color=black) "ELECTION 2020";
      entry textattrs=(size=8pt) " ";
      entry textattrs=(size=16pt weight=bold color=black) "IS THE GLASS " 
        textattrs=(size=16pt weight=bold color=red) "HALF-TRUMP" 
        textattrs=(size=16pt weight=bold color=black) " OR "
        textattrs=(size=16pt weight=bold color=blue) "HALF-BIDEN" 
        textattrs=(size=16pt weight=bold color=black) "?";
    endlayout;                         * Polts to show candidates' vote shares;
    polygonplot x=px y=py id=p / display=(fill) group=candidatecolor includemissinggroup=false;  * Rectangles showing candidates' vote shares;
     ellipseparm semiminor=.03 semimajor=.48 xorigin=x yorigin=eyR slope=0 / 
       display=(fill outline) outlineattrs=(color=white pattern=solid) fillattrs=(color=red) group=eyr includemissinggroup=false;  * Top of "glass" (Trump);
     ellipseparm semiminor=.03 semimajor=.48 xorigin=x yorigin=eyD slope=0 /
       display=(fill) fillattrs=(color=blue) group=eyd includemissinggroup=false;  * Bottom of "glass" (Biden);
    referenceline y=y / lineattrs=(pattern=dot color=white);  * Show 50%-of-vote reference lines;
    textplot x=x y=y text=statecode / textattrs=(color=white weight=bold size=12pt);  * Two-digit state codes;
    entry "  NOTES: 1. PERCENTAGES BASED ONLY ON VOTES CAST FOR TRUMP AND BIDEN. 2. VOTING DATA FROM NYTIMES.COM (AS OF 11-14)  " / valign=bottom;
  endlayout;
  endgraph;
end;
run;

proc sgrender data=statevotespolygons template=ustemplate;  * Generate the chart!;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Nov 2020 04:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/698932#M20725</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2020-11-15T04:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699311#M20732</link>
      <description>&lt;P&gt;So two wrongs makes no difference&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 21:32:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699311#M20732</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-16T21:32:36Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699460#M20733</link>
      <description>&lt;P&gt;After I clicked on the image, I still had to increase my window size to increase the map size big enough to see the 50/50 reference lines. Perhaps making them a solid line, rather than dashed, would make them show up better when the map is viewed at less than 'native' resolution?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 12:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699460#M20733</guid>
      <dc:creator>GraphGuy</dc:creator>
      <dc:date>2020-11-17T12:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699471#M20734</link>
      <description>&lt;P&gt;Instead of a stacked bar graph inside a glass, could you do side-by-side bars inside beer bottles, and audio annotate with&amp;nbsp;&lt;EM&gt;100 bottles of beer on the wall....&lt;/EM&gt; for the bumpy bus ride we are on.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 12:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699471#M20734</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-17T12:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699655#M20739</link>
      <description>&lt;P&gt;Here is the beer bottle version with side-by-side bars.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PresidentialElection.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51777i85C91B4D7BBDD29B/image-size/large?v=v2&amp;amp;px=999" role="button" title="PresidentialElection.png" alt="PresidentialElection.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* Modification of 
 * Fun With SAS ODS Graphics - Is the Glass Half-Trump or Half-Biden? (Yet Another Election Map)
 * to show each opponents support in beer bottles
 * 
 * Data randomly generated
 */ 

*==&amp;gt; Generate USA cartogram map x/y coordinates from inline layout of state codes;

data state_cartogram_points(keep=statecode x y);
  length statecode $2;
  format x y 3.;
  input;
  line = _infile_;
  if line ne: '/*';
  y + 1;                                * y-axis location of state name;
  do _n_ = 1 to countw(line,', ');
    statecode = scan(line, _n_, ', ');
    x = index(line, statecode);
    output;
  end;
datalines;
/* -|----10---|----20---|----30---|----40---|----50---| */
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; by statecode; run;

data state_votes;
  set state_cartogram_points;
  by statecode;
  if first.statecode;

  call streaminit (123);

  name = 'Jrrgl Bmurgrgly         ';
  pct = round(47.5 + rand('uniform', 0, 5), 0.1); 
  _tot = pct;
  output;

  name = 'Dorggles Jrgllulrm';
  do _n_ = 1 to 1000 until (_tot + pct &amp;lt;= 100);
    pct = round(47.5 + rand('uniform', 0, 5), 0.1);
  end;
  _tot + pct;
  output;

  name = 'Erggglglia Gluggglr';
  pct = 100 - _tot;
  output;

  keep statecode name pct;
  format pct 6.2;
run;

*==&amp;gt; Merge voting data and map points,
     Generate points for polygons (rectangles) to make "glasses" showing each candidate's share,
     Generate points for ellipses for top (Trump) and bottom (Biden) of "glasses"; 

%macro rect(xvar=px, yvar=py, x=, y=, w=0.90, h=pct, ymult=3.5);
  &amp;amp;xvar = &amp;amp;x;      &amp;amp;yvar = (&amp;amp;y + 1) * &amp;amp;ymult;           output;
                   &amp;amp;yvar = &amp;amp;yvar - &amp;amp;h * &amp;amp;ymult / 100;   output;
  &amp;amp;xvar = &amp;amp;x + &amp;amp;w;                                      output;
                   &amp;amp;yvar = (&amp;amp;y + 1) * &amp;amp;ymult;           output;
%mend;

%macro bottles(xvar=pxo, yvar=pyo, x=x, y=y, w=0.91, h=96, ymult=3.5);
  /* x1,y1 is lower left  x2,y2 is upper right */

  h  = 0.96 * &amp;amp;ymult;
  h1 = 0.60 * &amp;amp;ymult;
  h2 = 0.76 * &amp;amp;ymult;
 
  x1 = &amp;amp;x;         y1 = (&amp;amp;y + 1) * &amp;amp;ymult;
  x2 = x1 + 0.91; y2 = y1 - h;

  polygon_id+1;
  &amp;amp;xvar = x1; &amp;amp;yvar = y1;  output;
  &amp;amp;yvar = y1-h1;  output;
  &amp;amp;xvar = x1+.15; &amp;amp;yvar = y1-h2;  output;
  &amp;amp;yvar = y2; output;
  &amp;amp;xvar = x2-.15; output;
  &amp;amp;yvar = y1-h2; output;
  &amp;amp;xvar = x2; &amp;amp;yvar = y1-h1; output;
  &amp;amp;yvar = y1; output;

  x1 + 1;
  x2 + 1;
  x3 + 1;
  x4 + 1;

  polygon_id+1;
  &amp;amp;xvar = x1; &amp;amp;yvar = y1;  output;
  &amp;amp;yvar = y1-h1;  output;
  &amp;amp;xvar = x1+.15; &amp;amp;yvar = y1-h2;  output;
  &amp;amp;yvar = y2; output;
  &amp;amp;xvar = x2-.15; output;
  &amp;amp;yvar = y1-h2; output;
  &amp;amp;xvar = x2; &amp;amp;yvar = y1-h1; output;
  &amp;amp;yvar = y1; output;
%mend;

options mprint;

data map_polygons (rename=name=who);
  merge 
    state_cartogram_points 
    state_votes
  ;
  by statecode;

  if name in: ('Jrrgl', 'Dorggles');

  if name =: 'Dorggles' then dx_who = 0; else dx_who = 1;

  polygon_id+1;
  %rect (x=x + dx_who, y=y);
  call missing (px, py);

  if first.statecode then do;
    pxt =  x + 1;
    pyt = (y + 1) * 3.5 + .5;
    output;
    call missing(pxt, pyt);

    %bottles ();
    call missing (pxo, pyo);
  end;

  format px py pxt pyt pxo pyo 7.2 polygon_id dx_who 4.;
run;

options nomprint;

*==&amp;gt; Map and bar charts of vote shares - polygon + ellipse + text plots);

*ods listing image_dpi=300 gpath='/temp';
*ods graphics on / reset antialias width=11in height=8.5in imagename="PresidentialElection";

ods listing image_dpi=96 gpath='/temp';
ods graphics on / reset antialias width=2200px height=1700px imagename="PresidentialElection";

proc template;
  define statgraph ustemplate;
    begingraph / subpixel=on;          * Define Dem/Rep color attributes;

      discreteattrmap name="who_colors"; 
        value "Jrrgl Bmurgrgly"    / fillattrs=(color=red); 
        value "Dorggles Jrgllulrm" / fillattrs=(color=blue); 
      enddiscreteattrmap;

      discreteattrvar 
        attrvar=who_color 
        var=who
        attrmap="who_colors"
      ;

      layout overlayequated / 
        xaxisopts=(display=none thresholdmin=0 thresholdmax=0) 
        yaxisopts=(display=none reverse=true)
      ;

        layout gridded / columns=1 valign=top;  * Titles (Insets);
          entry textattrs=(size=24pt weight=bold color=black) "ELECTION 2020";
          entry textattrs=(size=8pt) " ";
          entry textattrs=(size=16pt weight=bold color=black) "WHO HAS MORE? " 
            textattrs=(size=16pt weight=bold color=red) "JRRGL" 
            textattrs=(size=16pt weight=bold color=black) " OR "
            textattrs=(size=16pt weight=bold color=blue) "DORGGLES" 
            textattrs=(size=16pt weight=bold color=black) "?";
        endlayout;                         

        polygonplot x=px y=py id=polygon_id / 
          display=(fill) 
          group=who_color 
          includemissinggroup=false;  * Rectangles showing candidates' vote shares;

        polygonplot x=pxo y=pyo id=polygon_id / 
          display=(outline)
          includemissinggroup=false;

        textplot x=pxt y=pyt text=statecode / textattrs=(color=gray66 weight=bold size=8pt);

        entry 
          "  NOTES: "
          "1. PERCENTAGES BASED ONLY ON VOTES CAST FOR Dorggles AND Jrrgl."
          "2. VOTING DATA FROM RAND('UNIFORM')  " 
          / valign=bottom;
        ;
        
      endlayout;
    endgraph;
  end;
run;

proc sgrender data=map_polygons template=ustemplate;  * Generate the chart!;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Nov 2020 02:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699655#M20739</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-18T02:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699694#M20740</link>
      <description>&lt;P&gt;Very clever! I vote for the %bottles macro to ship with Base SAS!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 08:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/699694#M20740</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2020-11-18T08:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Fun With SAS ODS Graphics: Is the Glass Half-Trump or Half-Biden?</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/700394#M20749</link>
      <description>&lt;P&gt;Yea, I'm getting spoiled by higher-resolution displays.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt; I did try solid lines, which could be seen better, but I didn't like the way they bisected the state codes (also tried backlighting letters, but felt that varied letter heights too much for this particular chart). In retrospect, I guess I probably should have tried varying the thickness of the dotted line though. Oh well, there's always next time!&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 04:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Fun-With-SAS-ODS-Graphics-Is-the-Glass-Half-Trump-or-Half-Biden/m-p/700394#M20749</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2020-11-20T04:18:09Z</dc:date>
    </item>
  </channel>
</rss>

