2004 Presidential-election state polling data at a glance

By Tom Moertel
Posted on
Tags:

The Presidential-race polling data on www.electoral-vote.com are great, and the site’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:

2004 Presidential-race polling data by state
2004 Presidential-race polling data by state

Some things to observe:

See anything else interesting?

If you want to see the code I used to generate the plot, read on.

The code

In case you’re curious, here’s the shell script and R code that I used to generate the above plot:

#!/bin/bash

# Generate all-states polling chart from electoral-vote.com data
# Tom Moertel <tom@moertel.com>
# 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
) > $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  <<EOF

    polls <- read.csv("$CSV")
    require("lattice")
    trellis.device(x11, theme=col.whitebg())
    p <- 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!
comments powered by Disqus