Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Divided or not? How the counties voted at the 2012 US election
<?php
/**
* Did some manual data munging of the following dataset:
*
* http://www.guardian.co.uk/news/datablog/2012/nov/07/us-2012-election-county-results-download#data
*
* in libreoffice calc to calculate the percentage of democratic vote (i.e. democratic / ( democratic + republican )), concatenated the county
* and state together so it looked like the "inkscape:label='...'" value in the SVG below and then
* assigned this to a class from colourbrewer (11 diverging classes, using the red and blue variety - note, this is NOT printer friendly):
*
* http://colorbrewer2.org/
*
* County SVG map from Wikipedia:
*
* http://commons.wikimedia.org/wiki/File:USA_Counties_with_FIPS_and_names.svg
*
* I did a simple search and replace in vim to remove the fill styles (don't know if it was neccesary or not)
*
* And after all of this, I figured out essentially the same thing was done here :)
*
* http://flowingdata.com/2009/11/12/how-to-make-a-us-county-thematic-map-using-free-tools/
*/
$colours = array();
foreach( file( 'colours.csv' ) as $line )
{
$parts = explode( ';', $line );
$county = trim( $parts[ 0 ] );
$colour = trim( $parts[ 1 ] );
$colours[ $county ] = $colour;
}
$svg = file_get_contents( 'counties.svg' );
foreach( $colours as $county => $colour )
{
$svg = str_replace(
"inkscape:label=\"{$county}\"",
"fill=\"#{$colour}\" inkscape:label=\"{$county}\"",
$svg
);
}
echo $svg;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pserwylo
Owner

Hmm, dunno how to delete counties.coloured.svg, but divided.or.not.svg has a key and a title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.