Skip to content

Instantly share code, notes, and snippets.

@tts
Last active October 2, 2015 07:53
Show Gist options
  • Save tts/828a7d3da6cfb1e86159 to your computer and use it in GitHub Desktop.
Save tts/828a7d3da6cfb1e86159 to your computer and use it in GitHub Desktop.
Scatterplot on languages in Espoo. Exercise in Data Visualization and Infographics with D3 by Knight Center for Journalism in the Americas
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Languages in Espoo in age groups under 6 and over 65</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<style type="text/css">
body {
font-family: Helvetica, Arial, sans-serif;
padding: 100px;
background-color: GhostWhite;
}
h1 {
font-size: 30px;
font-weight: bold;
}
a {
text-decoration: none;
}
a:link {
text-decoration: none;
}
#about {
font-size: 11px;
}
svg .cityname {
fill: SlateBlue;
font-weight: bold;
font-size: 30px;
}
svg .axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
svg .axis text {
font-family: sans-serif;
font-size: 11px;
}
svg circle:hover {
fill: orange;
}
svg .label {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<h1>Languages in Espoo in age groups under 6 and over 65</h1>
<div id="about">
<p>Source: <a href="http://www.hri.fi/fi/dataset/espoon-vaesto-aidinkielen-ja-ian-mukaan-tilastoalueittain-vuodenvaihteessa-2013-2014">Espoon väestö äidinkielen ja iän mukaan tilastoalueittain vuodenvaihteessa 2013/2014.</a></p>
<p>Data maintainer, Espoo City R&D. Data origin, Tilastokeskus.</p>
<p>Downloaded from <a href="http://www.hri.fi/">Helsinki Region Infoshare</a> by licence <a href="http://creativecommons.org/licenses/by/4.0/deed.fi">Creative Commons Nimeä 4.0 Kansainvälinen (CC BY 4.0)</a></p>
</div>
<script type="text/javascript">
var h = 500;
var w = 600;
var padding = [ 30, 10, 30, 70 ]; // Top, right, bottom, left
var xScale = d3.scale.linear()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(10);
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("lang.csv", function(data) {
xScale.domain([
d3.min(data, function(d) {
return +d.from0to6;
}),
d3.max(data, function(d) {
return +d.from0to6;
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.from65;
}),
d3.min(data, function(d) {
return +d.from65;
})
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", function(d) {
return xScale(d.from0to6);
})
.attr("cy", function(d) {
return yScale(d.from65);
})
.attr("r", 1)
.attr("fill", "lightblue")
.append("title")
.text(function(d) {
return d.Language + " is spoken by " + d.from0to6 + " children under 6 and " + d.from65 + " people over 65";
});
circles.transition()
.duration(2000)
.attr("r", 10);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2] + 10) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3] - 10) + ",0)")
.call(yAxis);
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", w )
.attr("y", h - padding[2])
.text("People under 6");
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", padding [1] + padding[3] + 60)
.attr("y", padding [0] )
.text("People over 65");
});
</script>
</body>
</html>
Language from0to6 from7to15 from16to29 from30to44 from45to64 from65
Albanian 218 211 389 316 177 16
Arabic 232 216 278 442 274 20
Bengali 61 35 162 201 31 0
English 275 164 432 833 392 79
Spanish 47 40 132 303 97 17
Hindi 74 34 76 248 23 5
Chinese 194 133 497 775 258 13
Kurdish 152 143 354 276 136 11
Other 550 429 1393 2214 849 199
Persian 67 72 258 194 126 36
French 43 36 70 170 73 10
Romanian 26 30 52 139 34 4
Swedish 2226 2642 3043 3440 4822 4102
Somali 421 442 615 337 213 29
Finnish 19150 22669 37250 44685 55350 29425
Tagalog 26 46 91 168 69 3
Tamil 75 20 86 151 13 1
Telugu 68 21 74 149 3 0
Thai 19 50 77 184 58 0
Turkish 54 39 109 174 102 21
Hungarian 53 48 59 171 46 11
Urdu 75 20 141 173 15 0
Russian 512 489 1054 1551 1086 178
Vietnamese 60 41 310 178 127 30
Estonian 459 429 1007 1632 1328 92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment