Skip to content

Instantly share code, notes, and snippets.

@Clevejones
Created April 26, 2015 17:16
Show Gist options
  • Select an option

  • Save Clevejones/d4081ed6ee9b9b80913d to your computer and use it in GitHub Desktop.

Select an option

Save Clevejones/d4081ed6ee9b9b80913d to your computer and use it in GitHub Desktop.
Birth rate by country
country 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Argentina 18.757 18.445 18.177 17.958 17.788 17.66 17.564 17.481 17.396 17.301 17.188 17.057 16.911 16.756
Australia 13 12.7 12.8 12.6 12.3 12.8 12.9 14.1 14 13.9 13.7 13.6 13.7 13.2
Austria 9.8 9.4 9.7 9.5 9.7 9.5 9.4 9.2 9.3 9.2 9.4 9.3 9.4 9.4
Bangladesh 27.028 26.307 25.581 24.855 24.135 23.442 22.8 22.225 21.725 21.298 20.934 20.614 20.311 20.005
Barbados 13.463 13.349 13.262 13.194 13.137 13.089 13.043 13 12.957 12.91 12.859 12.802 12.74 12.676
Belgium 11.4 11.2 10.9 11 11.3 11.4 11.6 11.7 11.9 11.8 11.9 11.6 11.5 11.2
Bermuda 13.5 13.3 13.2 13.2 13.2 13.1 12.5 13.4 12.8 12.5 11.8 10.4 10 10.4
Bosnia and Herzegovina 10.199 9.64 9.14 8.748 8.48 8.338 8.307 8.345 8.418 8.509 8.609 8.716 8.833 8.962
Botswana 27.402 26.88 26.417 26.022 25.692 25.417 25.182 24.966 24.753 24.532 24.299 24.055 23.807 23.562
Brazil 20.867 20.459 19.954 19.372 18.729 18.061 17.411 16.818 16.309 15.896 15.576 15.332 15.129 14.938
Bulgaria 9 8.5 8.5 8.6 9 9.2 9.6 10 10.4 10.9 10.2 9.6 9.5 9.2
Cambodia 27.545 26.99 26.612 26.372 26.238 26.183 26.182 26.215 26.253 26.266 26.225 26.109 25.917 25.652
Canada 10.9 10.6 10.7 10.5 10.65 10.65 10.9 11.2 11.3 11.3 11.1 11 11 10.9
Chad 50.999 50.812 50.59 50.332 50.039 49.707 49.333 48.919 48.469 47.987 47.481 46.957 46.423 45.886
Channel Islands 10.992 10.851 10.714 10.576 10.437 10.298 10.164 10.038 9.925 9.824 9.735 9.657 9.587 9.522
Chile 16.683 16.25 15.89 15.586 15.328 15.11 14.922 14.758 14.609 14.468 14.329 14.191 14.055 13.922
China 14.03 13.38 12.86 12.41 12.29 12.4 12.09 12.1 12.14 12.13 11.9 11.93 12.1 12.1
Colombia 22.897 22.518 22.17 21.849 21.55 21.266 20.986 20.702 20.408 20.1 19.779 19.448 19.113 18.783
Cuba 13.044 12.785 12.496 12.171 11.813 11.435 11.056 10.7 10.385 10.12 9.908 9.744 9.614 9.505
Cyprus 13.244 12.828 12.501 12.25 12.068 11.944 11.862 11.807 11.762 11.717 11.665 11.605 11.542 11.482
Czech Republic 8.9 8.9 9.1 9.2 9.6 10 10.3 11.1 11.5 11.3 11.2 10.4 10.3 10.2
Denmark 12.6 12.2 11.9 12 12 11.9 12 11.7 11.8 11.4 11.4 10.6 10.4 10
Dominican Republic 24.982 24.7 24.436 24.172 23.895 23.6 23.284 22.952 22.612 22.263 21.907 21.547 21.186 20.826
Egypt 24.764 24.563 24.435 24.378 24.374 24.401 24.427 24.427 24.377 24.263 24.078 23.823 23.517 23.177
Finland 11 10.8 10.7 10.9 11 11 11.2 11.1 11.2 11.3 11.4 11.1 11 10.7
France 13.3 13.1 12.9 12.8 12.8 12.8 13.1 12.8 12.9 12.8 12.9 12.7 12.6 12.3
Georgia 11.964 11.898 11.975 12.176 12.473 12.822 13.173 13.478 13.699 13.814 13.815 13.713 13.542 13.332
Germany 9.3 8.9 8.7 8.6 8.6 8.3 8.2 8.3 8.3 8.1 8.3 8.1 8.4 8.5
Greece 9.5 9.3 9.4 9.5 9.6 9.7 10.1 10 10.6 10.5 10.3 9.6 9 8.5
Greenland 15.6 16.7 16.9 15.5 15.7 15.6 14.8 15.1 14.8 15.9 15.3 14.4 13.9 14.5
Haiti 31.127 30.563 30.028 29.529 29.068 28.642 28.244 27.864 27.492 27.121 26.75 26.376 26.003 25.633
Honduras 31.628 30.967 30.349 29.774 29.243 28.758 28.317 27.913 27.536 27.178 26.831 26.487 26.14 25.786
Hong Kong 8.1 7.2 7.1 6.9 7.2 8.4 9.6 10.2 11.3 11.8 12.6 13.5 12.8 7.9
Hungary 9.6 9.5 9.5 9.3 9.4 9.7 9.9 9.7 9.9 9.6 9 8.8 9.1 9.2
Iceland 15.3 14.4 14.1 14.3 14.5 14.4 14.5 14.6 15.2 15.8 15.4 14.1 14.1 13.4
India 25.622 25.186 24.73 24.253 23.757 23.259 22.779 22.335 21.937 21.589 21.281 20.999 20.723 20.438
Indonesia 21.521 21.477 21.468 21.465 21.444 21.376 21.241 21.037 20.767 20.435 20.051 19.633 19.204 18.787
Iran 18.672 18.228 17.976 17.89 17.937 18.084 18.3 18.551 18.795 18.995 19.11 19.112 19.003 18.79
Iraq 35.882 35.614 35.318 35.001 34.663 34.307 33.933 33.543 33.141 32.731 32.317 31.905 31.496 31.093
Ireland 14.4 15 15.4 15.4 15.2 14.8 15.3 16.2 16.7 16.7 16.5 16.2 15.7 15
Israel 21.7 21.2 21.2 21.7 21.3 20.8 21 21.1 21.5 21.5 21.8 21.4 21.6 21.3
Italy 9.5 9.4 9.4 9.5 9.8 9.6 9.6 9.7 9.8 9.6 9.5 9.2 9 8.5
Jamaica 20.7 18.92 18.15 17.36 17.02 17.54 16.3 16.29 16.14 15.96 15.05 14.69 14.53 13.54
Japan 9.4 9.3 9.3 9.2 8.9 8.5 8.65 8.63 8.7 8.5 8.5 8.3 8.2 8.2
Jordan 31.174 30.821 30.546 30.353 30.229 30.143 30.05 29.902 29.665 29.322 28.869 28.317 27.699 27.046
Kazakhstan 14.7 14.9 14.6 16.63 18.19 18.42 19.71 20.79 22.75 22.15 22.54 22.52 22.69 22.73
Kenya 38.532 38.668 38.773 38.825 38.8 38.676 38.442 38.11 37.699 37.218 36.681 36.104 35.505 34.904
Korea, Dem. Rep. 17.904 17.414 16.92 16.431 15.961 15.532 15.165 14.869 14.646 14.495 14.408 14.372 14.364 14.366
Korea, Rep. 13.3 11.6 10.2 10.2 9.8 8.9 9.2 10 9.4 9 9.4 9.4 9.6 8.6
Kosovo 22.8 21.7 20.8 18.3 20 20.9 19.1 19.3 19 18.8 18.5 18.3 18 17.7
Kuwait 23.854 23.147 22.383 21.777 21.401 21.263 21.31 21.41 21.463 21.435 21.314 21.107 20.852 20.575
Liechtenstein 12.9 12.1 11.7 10.2 10.8 11 10.3 10 9.9 11.4 9.1 10.9 9.7 9.2
Lithuania 9.8 9 8.6 8.8 8.8 8.9 9.1 9.3 9.9 10.2 9.9 10 10.2 10.1
Luxembourg 13.1 12.4 12 11.7 11.9 11.5 11.7 11.4 11.5 11.3 11.6 10.9 11.3 11.3
Macao 9.073 8.476 8.051 7.817 7.769 7.888 8.141 8.472 8.826 9.171 9.481 9.747 9.978 10.179
Macedonia 12.622 12.277 11.994 11.76 11.571 11.418 11.295 11.194 11.105 11.023 10.942 10.859 10.776 10.694
Madagascar 41.123 40.375 39.626 38.901 38.221 37.602 37.057 36.576 36.153 35.783 35.46 35.175 34.916 34.67
Malaysia 22.7 21.68 20.709 19.84 19.107 18.536 18.134 17.871 17.71 17.631 17.609 17.62 17.642 17.653
Maldives 24.528 23.382 22.47 21.816 21.417 21.262 21.322 21.523 21.785 22.047 22.236 22.292 22.201 21.957
Malta 11.5 10.1 9.9 10.2 9.7 9.6 9.3 9.3 9.8 9.8 9.4 10 9.8 9.5
Mexico 24.148 23.627 23.112 22.611 22.128 21.666 21.221 20.789 20.365 19.949 19.544 19.15 18.771 18.407
Montenegro 13.337 13.231 13.114 12.996 12.878 12.758 12.634 12.501 12.359 12.209 12.056 11.902 11.754 11.616
Morocco 22.022 21.464 20.978 20.57 20.253 20.076 20.095 20.32 20.726 21.263 21.833 22.322 22.636 22.712
Mozambique 44.284 44.502 44.6 44.54 44.307 43.904 43.353 42.709 42.026 41.336 40.666 40.034 39.441 38.888
Myanmar 20.924 20.424 19.969 19.571 19.234 18.952 18.716 18.506 18.306 18.105 17.896 17.674 17.443 17.204
Namibia 31.655 31.116 30.607 30.116 29.64 29.177 28.727 28.291 27.87 27.465 27.077 26.708 26.359 26.028
Nepal 32.849 32.088 31.239 30.317 29.336 28.307 27.241 26.159 25.088 24.058 23.108 22.27 21.558 20.973
Netherlands 13 12.6 12.5 12.3 11.9 11.5 11.3 11.1 11.2 11.2 11.1 10.8 10.5 10.2
New Caledonia 21.4 19.9 18.9 18.2 17.3 17.2 17.7 16.9 16.2 16.3 16.7 16.2 17 17
New Zealand 14.66 14.36 13.67 13.94 14.2 13.96 14.14 15.14 15.06 14.48 14.62 13.93 13.79 13.12
Nicaragua 27.904 27.19 26.598 26.111 25.719 25.398 25.122 24.859 24.582 24.274 23.929 23.546 23.142 22.73
Niger 53.141 52.743 52.331 51.92 51.526 51.165 50.851 50.587 50.369 50.194 50.052 49.929 49.808 49.674
Nigeria 42.909 42.833 42.751 42.662 42.565 42.464 42.364 42.267 42.169 42.063 41.933 41.76 41.532 41.245
Norway 13.2 12.6 12.2 12.4 12.4 12.3 12.6 12.4 12.7 12.8 12.6 12.2 12 11.6
Oman 23.913 23.144 22.524 22.059 21.741 21.564 21.522 21.576 21.675 21.77 21.787 21.655 21.345 20.851
Pakistan 31.192 30.205 29.375 28.735 28.287 27.994 27.794 27.605 27.364 27.045 26.644 26.182 25.704 25.246
Peru 24.114 23.658 23.234 22.835 22.46 22.105 21.768 21.447 21.139 20.84 20.547 20.26 19.977 19.699
Philippines 29.587 29.261 28.864 28.392 27.855 27.28 26.705 26.172 25.708 25.326 25.025 24.79 24.59 24.398
Poland 9.9 9.6 9.3 9.2 9.3 9.5 9.8 10.2 10.9 10.9 10.8 10.1 10 9.6
Portugal 11.7 10.9 11 10.8 10.4 10.4 10 9.7 9.9 9.4 9.6 9.2 8.5 7.9
Qatar 19.421 18.876 18.166 17.311 16.341 15.319 14.323 13.425 12.675 12.093 11.676 11.393 11.179 10.98
Romania 10.4 10 9.7 9.8 10.1 10.4 10.4 10.3 10.8 10.9 10.5 9.7 10 8.8
Russian Federation 8.7 9.1 9.8 10.2 10.4 10.2 10.4 11.3 12 12.3 12.5 12.6 13.3 13.2
Saudi Arabia 27.112 26.174 25.29 24.487 23.776 23.158 22.62 22.135 21.675 21.224 20.774 20.321 19.864 19.402
Senegal 39.961 39.794 39.64 39.489 39.335 39.182 39.042 38.918 38.808 38.697 38.561 38.369 38.1 37.746
Serbia 9.8 10.5 10.4 10.6 10.5 9.7 9.58 9.22 9.4 9.6 9.4 9.1 9.3 9.2
Singapore 11.8 11.8 11.4 10.5 10.3 10.2 10.3 10 10.2 9.9 9.3 9.5 10.1 9.3
South Africa 24.059 23.835 23.613 23.375 23.115 22.835 22.546 22.265 22.004 21.763 21.536 21.316 21.09 20.85
Spain 9.9 9.9 10.1 10.4 10.6 10.6 10.8 10.9 11.3 10.6 10.4 10.1 9.7 9.1
Swaziland 32.526 32.131 31.863 31.689 31.581 31.506 31.435 31.346 31.222 31.048 30.816 30.531 30.211 29.871
Sweden 10.2 10.3 10.7 11.1 11.2 11.2 11.7 11.7 11.9 12 12.3 11.8 11.9 11.8
Switzerland 10.9 10 9.9 9.8 9.9 9.8 9.8 9.9 10 10.1 10.3 10.2 10.3 10.2
Thailand 14.568 14.152 13.751 13.364 12.99 12.63 12.283 11.949 11.627 11.319 11.026 10.748 10.486 10.242
Tunisia 17.1 16.9 16.7 17.1 16.8 17.1 17.1 17.4 17.7 17.7 18.6 18.8 19.3 19.8
Turkey 21.456 21.021 20.591 20.176 19.779 19.403 19.047 18.706 18.377 18.058 17.746 17.439 17.135 16.836
Ukraine 7.8 7.7 8.7 8.5 9 9 9.8 10.2 11 11.1 10.8 11 11.4 11.1
United Arab Emirates 16.738 16.438 16.274 16.218 16.24 16.302 16.365 16.395 16.357 16.224 15.969 15.578 15.069 14.466
United Kingdom 11.5 11.3 11.3 11.7 11.9 12 12.3 12.6 12.9 12.7 12.9 12.8 12.8 12.2
United States 14.4 14.1 14 14.1 14 14 14.3 14.3 14 13.5 13 12.7 12.6 12.5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Module6</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
body {
background-color: #fff1e0;
font-family: Arial, sans-serif;
}
h1 {
color: #3B3D3B;
line-height: 50%;
}
p {
color: #3B3D3B;
line-height: 40%;
font-size: 16px;
}
path:hover {
stroke: #723545;
opacity: 1;
}
path {
stroke: #D6CFC4;
stroke-width: 2;
}
path {
cursor: pointer;
}
.tooltip {
position: absolute;
font-family: Arial, sans-serif;
background: #eee;
box-shadow: 0 0 5px #999999;
color: #333;
font-size: 14px;
line-height: 19px;
left: 130px;
width: 210px;
border-style: solid;
border-color: #76787A;
border-width: 0px;
border-radius: 7px;
z-index: 1000;
}
g.highlight path {
stroke: steelblue;
stroke-width: 2;
}
.axis path,
.axis line {
stroke: #cec5b8;
stroke-dasharray:2,2;
fill: none;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
fill: #3B3D3B;
}
</style>
</head>
<body>
<h1>Birth rate by country</h1>
<p>Birth rate, crude (per 1,000 people) top 100 countries
Source: <a href="http://databank.worldbank.org/Data/Views/reports/chart.aspx">World Bank</p>
<script type="text/javascript">
//Dimensions and padding
var w = 700;
var h = 600;
var padding = [ 20, 10, 30, 20 ]; //Top, right, bottom, left
//Set up date formatting and years
var dateFormat = d3.time.format("%Y");
//Set up scales
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(15)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
//Configure line generator
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(+d.amount);
});
//Create the empty SVG image
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load USA data
d3.csv("BirthRate.csv", function(data) {
var years = ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011","2012", "2013"];
//Create a new, empty array to hold our restructured dataset
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this country's name and empty array
dataset[i] = {
country: data[i].countryName,
birthRate: []
};
//Loop through all the years
for (var j = 0; j < years.length; j++) {
// If value is not empty
if (data[i][years[j]]) {
//Add a new object to the emissions data array
//for this country
dataset[i].birthRate.push({
year: years[j],
amount: data[i][years[j]]
});
}
}
}
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
yScale.domain([
d3.max(dataset, function(d) {
return d3.max(d.birthRate, function(d) {
return +d.amount;
});
}),
5
]);
//Make a group for each country
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.classed("highlight", function(d) {
if (d.country == "United States" ||
d.country == "United Kingdom") {
return true;
} else {
return false;
}
});
//Append a title with the country name (so we get easy tooltips)
groups.append("title")
.text(function(d) {
return d.country;
});
//Within each group, create a new line/path,
//binding just the emissions data to each one
groups.selectAll("path")
.data(function(d) {
return [ d.birthRate ];
})
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("fill", "none")
.attr("stroke-width", 2);
//Axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
});
//End USA data load function
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment