Skip to content

Instantly share code, notes, and snippets.

@ohdebby
Last active January 31, 2017 23:24
Show Gist options
  • Save ohdebby/0e3ac9340a87209a78e96975bfc1a3f3 to your computer and use it in GitHub Desktop.
Save ohdebby/0e3ac9340a87209a78e96975bfc1a3f3 to your computer and use it in GitHub Desktop.
All-cause mortality, ages 50-54
license: mit
year US-White US-Hispanic Australia Canada France Germany Sweden UK
1990 428.9 455.3 548.8 556.3 395.2 496.3
1991 414.8 435.3 528.9 553.8 395 488.3
1992 410.6 430.6 522 534.4 394.8 471.2
1993 379.1 422.6 510.7 538.2 386 475.3
1994 376.7 415.8 512.3 530.2 379.6 449
1995 365.9 417.5 496.8 521.2 376.7 450.2
1996 360.2 400.2 499.3 515.7 376.9 435.2
1997 351.8 382.3 488.3 499.2 365.6 426.9
1998 335.8 373.3 482.3 471.6 346.4 419.9
1999 460 398.1 313.5 367 480.5 462.6 339.3 415
2000 467.4 394.3 313.6 366.1 472.8 454.4 339.1 406.6
2001 464.5 401.4 305.9 361.9 470.4 449.8 338.8 405.3
2002 473.3 397.5 305.9 352.6 476.6 451.5 320.3 396.2
2003 474.2 399.3 280.3 359.8 481.3 449.5 317.2 394.5
2004 471.5 384.2 289.5 348.8 458.8 433.3 314.2 385.1
2005 480.9 397 285.5 343.1 455.8 432.2 315.2 377.2
2006 480 383.8 282.6 342.3 457.7 422.2 310.1 378.7
2007 475.3 375.1 292.2 341.4 443.5 413.9 298.1 367.3
2008 478.3 353.3 284.8 342 437.4 408.3 296 365.3
2009 482.6 357.5 290.2 334.5 433.5 403.1 294.4 346
2010 471.9 338.9 277 320.6 424.8 398.2 268.7 342.9
2011 478.3 340.9 275.7 308 413.2 385.2 261 329.2
2012 475.7 335.9 399 371.7 263.7 316.1
2013 480.3 336.4 382.3 369.2 261.5 316.6
2014 480.8 331.7 364.1 246.7
2015 483.2 329.5
<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: sans-serif;
}
text {
font-size: 12px;
fill: #888;
}
path, line {
stroke: #888;
stroke-width: 2;
fill: none;
}
</style>
<p>All-cause mortality, ages 50-54</p>
<svg width="700" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 80, bottom: 30, left: 50},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseTime = d3.timeParse("%Y");
var x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
var line = d3.line()
.defined(function (d) { return d[1] !==NaN; })
// .curve(d3.curveBasis)
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.mortality); });
d3.csv("Fig1.csv", type, function(error, data) {
if (error) throw error;
var country = data.columns.slice(1).map(function(id) {
return {
id: id,
values: data.map(function(d) {
return {year: d.year, mortality: d[id]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.year; }));
y.domain([0,
d3.max(country, function(c) { return d3.max(c.values, function(d) { return d.mortality; }); })
]);
z.domain(country.map(function(c) { return c.id; }));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -35)
.attr("x", -150)
.text("deaths per 100,000");
var group = g.selectAll(".group")
.data(country)
.enter().append("g")
.attr("class", "group");
group.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return z(d.id); });
group.append("text")
.datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.year) + "," + y(d.value.mortality) + ")"; })
.attr("x", 3)
.attr("dy", "0.35em")
.style("font", "10px sans-serif")
.text(function(d) { return d.id; });
});
function type(d, _, columns) {
d.year = parseTime(d.year);
for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c];
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment