Created
November 7, 2012 22:30
-
-
Save pserwylo/4034954 to your computer and use it in GitHub Desktop.
Divided or not? How the counties voted at the 2012 US election
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm, dunno how to delete counties.coloured.svg, but divided.or.not.svg has a key and a title.