Skip to content

Instantly share code, notes, and snippets.

@pserwylo
Created November 7, 2012 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pserwylo/4034954 to your computer and use it in GitHub Desktop.
Save pserwylo/4034954 to your computer and use it in GitHub Desktop.
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;
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pserwylo
Copy link
Author

pserwylo commented Nov 7, 2012

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