Skip to content

Instantly share code, notes, and snippets.

@edooley7
Created August 13, 2015 18:48
Show Gist options
  • Save edooley7/3515720a49420aded961 to your computer and use it in GitHub Desktop.
Save edooley7/3515720a49420aded961 to your computer and use it in GitHub Desktop.
Different error types
Model Innocent - presumed guilty Guilty - presumed innocent
Severity of offense (1994) 0.391673570837 0.000310687655344
Sentence Length (1994) 0.376346313173 0.0158450704225
Time served (1994) 0.344552609776 0.0396644573322
Age at release (1994) 0.310141020324 0.0484238905019
Consequence (first arrest) 0.304681027341 0.071043910522
Age at first offense 0.292648186699 0.0672729264819
Total prior arrests (1994) 0.274751449876 0.0631731565866
Average arrest rate 0.254145478665 0.0895423391554
Full model 0.212283044059 0.0897863818425
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Error classification</title>
</head>
<style>
body {
font: 12px sans-serif;
fill: #C5C3C6;
background: black;
}
.axis path,
.axis line {
fill: None;
stroke: #C5C3C6;
shape-rendering: crispEdges;
}
.bar {
fill: white;
}
.x.axis path {
display: #C5C3C6;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 80, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["#9BC53D", "#FA7921", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(",.0%"));
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("error_data.csv", function(error, data) {
if (error) throw error;
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "Model"; });
data.forEach(function(d) {
d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
});
x0.domain(data.map(function(d) { return d.Model; }));
x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.call(wrap, x1.rangeBand())
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".4em")
.style("text-anchor", "end")
.text("Percent of prisoners")
var model = svg.selectAll(".model")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.Model) + ",0)"; });
model.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); })
.style("opacity", .65);
var legend = svg.selectAll(".legend")
.data(ageNames.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.style("opacity",.65);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
var barWidth = width / data.length;
function mouseover() {
d3.select(this).style("opacity",1 );
}
function mouseout() {
d3.select(this).style("opacity",.65);
}
d3.selectAll("rect").on("mouseover",mouseover).on("mouseout",mouseout);
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment