<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: 2004 Presidential-election state polling data at a glance</title>
    <link>http://blog.moertel.com/articles/2004/11/02/2004-presidential-election-state-polling-data-at-a-glance</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>2004 Presidential-election state polling data at a glance</title>
      <description>&lt;p&gt;The Presidential-race polling data on &lt;a href="http://www.electoral-vote.com/"&gt;www.electoral-vote.com&lt;/a&gt; are great, and the site&amp;#8217;s plots do help visualize the country-wide race. But I want to see all the historical data for all of the states at a glance. To this end, I have taken the data from the site (as of the morning of November 2, 2004) and created this all-inclusive summary plot that shows the state-by-state polling results from September 1, 2004 through November 2, 2004:&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://community.moertel.com/ss/space/start/2004-11-02/1/polls-by-state.png" title="2004 Presidential-race polling data by state" alt="2004 Presidential-race polling data by state" /&gt;&lt;/p&gt;


	&lt;p&gt;Some things to observe:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Battleground states are polled frequently; others, hardly at all&lt;/li&gt;
		&lt;li&gt;Wisconsin&amp;#8217;s polling data suggest that Bush once had the state but lost it&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;See anything else interesting?&lt;/p&gt;


	&lt;p&gt;If you want to see the code I used to generate the plot, read on.&lt;/p&gt;&lt;h3&gt;The code&lt;/h3&gt;


	&lt;p&gt;In case you&amp;#8217;re curious, here&amp;#8217;s the shell script and &lt;a href="http://www.r-project.org/"&gt;R&lt;/a&gt; code that I used to generate the above plot:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;#!/bin/bash

# Generate all-states polling chart from electoral-vote.com data
# Tom Moertel &amp;lt;tom@moertel.com&amp;gt;
# 2004-11-02

# config

CSV=allpolls.csv
FILEBASE=polls-by-state
GS="gs -dSAFTER -dBATCH -dNOPAUSE" 

# set up filenames

EPS=$FILEBASE.eps
PNG=$FILEBASE.png
PDF=$FILEBASE.pdf

# get polling data and add header to it

echo "Making sure we have polling data (delete $CSV to force download) ..." 

[ -f $CSV ] ||
(    echo -n date,series,state,ev,kerry,bush,nader,released,
     echo    unk2,unk3,unk4,unk5,unk6,unk7,unk8,source
     GET http://www.electoral-vote.com/info/$CSV
) &amp;gt; $CSV

# use R to generate plot in Postscript format
# (uses X11 window so we can see the plot in passing)

echo Generating plots ...

R --no-save --no-init-file --no-restore-data  &amp;lt;&amp;lt;EOF

    polls &amp;lt;- read.csv("$CSV")
    require("lattice")
    trellis.device(x11, theme=col.whitebg())
    p &amp;lt;- xyplot(kerry + bush ~ date | state, data=polls,
                main="Polling Numbers by State (Nov 2)",
                auto.key=list(rectangles=F,x=.75,y=.875,corner=c(1,1)),
                xlab="Elapsed days (1 = Sep 1)",
                ylab="Percentage who would vote for candidate")
    p
    dev.copy2eps(file="$EPS",family="Helvetica-Narrow")

EOF

# create other graphics formats

echo Rasterizing into PNG format ...

$GS -sDEVICE=pngalpha -dBackgroundColor='16#ffeebb' -dEPSCrop \
    -sOutputFile=$PNG $EPS

echo Converting into PDF version ...

ps2pdf -dEPSCrop $SIZEPARMS $EPS

# done!

echo Done!
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Tue, 02 Nov 2004 12:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:58e0092df1c268818b386d022749bf0b</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2004/11/02/2004-presidential-election-state-polling-data-at-a-glance</link>
      <category>statistics</category>
      <category>interesting stuff</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/31</trackback:ping>
    </item>
  </channel>
</rss>
