US State Emissions Choropleth
Last active
August 29, 2015 14:13
-
-
Save amrit/39bbf25bd3d4a563b1d5 to your computer and use it in GitHub Desktop.
US State Emissions Choropleth
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>US Emissions by State (2013)</title> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style type="text/css"> | |
/* No style rules here yet */ | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//Width and height | |
var w = 960; | |
var h = 600; | |
//Define map projection | |
var projection = d3.geo.albersUsa() | |
.translate([w/2, h/2]) | |
.scale([1280]); | |
//Define path generator | |
var path = d3.geo.path() | |
.projection(projection); | |
//Define quantize scale to sort data values into buckets of color | |
var color = d3.scale.quantize() | |
.range(["#fef0d9","#fdd49e","#fdbb84","#fc8d59","#ef6548","#d7301f","#990000"]); | |
//Colors taken from colorbrewer.js, included in the D3 download | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Load in agriculture data | |
d3.csv("State_Emissions.csv", function(data) { | |
//Set input domain for color scale | |
color.domain([ | |
d3.min(data, function(d) { return d.emissions; }), | |
d3.max(data, function(d) { return d.emissions; }) | |
]); | |
//Load in GeoJSON data | |
d3.json("us_states.json", function(json) { | |
//Merge the ag. data and GeoJSON | |
//Loop through once for each ag. data value | |
for (var i = 0; i < data.length; i++) { | |
//Grab state name | |
var dataState = data[i].state; | |
//Grab data value, and convert from string to float | |
var dataValue = parseFloat(data[i].emissions); | |
//Find the corresponding state inside the GeoJSON | |
for (var j = 0; j < json.features.length; j++) { | |
var jsonState = json.features[j].properties.name; | |
if (dataState == jsonState) { | |
//Copy the data value into the JSON | |
json.features[j].properties.value = dataValue; | |
//Stop looking through the JSON | |
break; | |
} | |
} | |
} | |
//Bind data and create one path per GeoJSON feature | |
svg.selectAll("path") | |
.data(json.features) | |
.enter() | |
.append("path") | |
.attr("d", path) | |
.style("fill", function(d) { | |
//Get data value | |
var value = d.properties.value; | |
if (value) { | |
//If value exists… | |
return color(value); | |
} else { | |
//If value is undefined… | |
return "#ccc"; | |
} | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
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
state | emissions | |
---|---|---|
Alabama | 123.05 | |
Alaska | 37.81 | |
Arizona | 91.23 | |
Arkansas | 66.20 | |
California | 364.20 | |
Colorado | 90.64 | |
Connecticut | 34.68 | |
Delaware | 14.07 | |
District of Columbia | 2.71 | |
Florida | 224.20 | |
Georgia | 136.88 | |
Hawaii | 18.77 | |
Idaho | 15.89 | |
Illinois | 218.09 | |
Indiana | 194.07 | |
Iowa | 82.02 | |
Kansas | 67.27 | |
Kentucky | 138.12 | |
Louisiana | 205.52 | |
Maine | 16.01 | |
Maryland | 61.44 | |
Massachusetts | 62.83 | |
Michigan | 154.03 | |
Minnesota | 87.63 | |
Mississippi | 61.87 | |
Missouri | 128.59 | |
Montana | 30.66 | |
Nebraska | 50.58 | |
Nevada | 34.61 | |
New Hampshire | 14.82 | |
New Jersey | 106.08 | |
New Mexico | 54.61 | |
New York | 163.53 | |
North Carolina | 120.60 | |
North Dakota | 52.65 | |
Ohio | 217.19 | |
Oklahoma | 105.44 | |
Oregon | 37.03 | |
Pennsylvania | 237.48 | |
Rhode Island | 10.58 | |
South Carolina | 74.31 | |
South Dakota | 15.13 | |
Tennessee | 99.91 | |
Texas | 676.94 | |
Utah | 61.25 | |
Vermont | 5.56 | |
Virginia | 98.53 | |
Washington | 71.05 | |
West Virginia | 90.75 | |
Wisconsin | 90.94 | |
Wyoming | 66.16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment