Skip to content

Instantly share code, notes, and snippets.

@anaeliaovalle
Created February 22, 2017 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anaeliaovalle/b76e2b3f91247351308da0ec88b1e4d3 to your computer and use it in GitHub Desktop.
Save anaeliaovalle/b76e2b3f91247351308da0ec88b1e4d3 to your computer and use it in GitHub Desktop.
Scatterplot Matrix
license: gpl-3.0
border: no
height: 960

The scatterplot matrix visualizations pairwise correlations for multi-dimensional data; each cell in the matrix is a scatterplot. This example uses Anderson's data of iris flowers on the Gaspé Peninsula. Scatterplot matrix design invented by J. A. Hartigan; see also R and GGobi. Data on Iris flowers collected by Edgar Anderson and published by Ronald Fisher.

See also this version with brushing.

forked from mbostock's block: Scatterplot Matrix

forked from anaeliaovalle's block: Scatterplot Matrix

<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
padding: 10px;
}
.axis,
.frame {
shape-rendering: crispEdges;
}
.axis line {
stroke: #ddd;
}
.axis path {
display: none;
}
.cell text {
font-weight: bold;
text-transform: capitalize;
}
.frame {
fill: none;
stroke: #aaa;
}
circle {
fill-opacity: .7;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
size = 230,
padding = 20;
var x = d3.scale.linear()
.range([padding / 2, size - padding / 2]);
var y = d3.scale.linear()
.range([size - padding / 2, padding / 2]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
var color = d3.scale.category10();
function toNumber(text) {
text = text.trim();
text = text.replace(/,/, "");
return +text;
}
d3.csv("scatter.csv", function(error, data) {
if (error) throw error;
/*
* converts strings (with extra spaces and comma thousands
* separators) to a number
*/
if (error) throw error;
var domainByTrait = {},
traits = d3.keys(data[0]).filter(function(d) { return d !== "tiername"; }),
n = traits.length;
traits.forEach(function(trait) {
domainByTrait[trait] = d3.extent(data, function(d) { return d[trait]; });
});
xAxis.tickSize(size * n);
yAxis.tickSize(-size * n);
var svg = d3.select("body").append("svg")
.attr("width", size * n + padding)
.attr("height", size * n + padding)
.append("g")
.attr("transform", "translate(" + padding + "," + padding / 2 + ")");
svg.selectAll(".x.axis")
.data(traits)
.enter().append("g")
.attr("class", "x axis")
.attr("transform", function(d, i) { return "translate(" + (n - i - 1) * size + ",0)"; })
.each(function(d) { x.domain(domainByTrait[d]); d3.select(this).call(xAxis); });
svg.selectAll(".y.axis")
.data(traits)
.enter().append("g")
.attr("class", "y axis")
.attr("transform", function(d, i) { return "translate(0," + i * size + ")"; })
.each(function(d) { y.domain(domainByTrait[d]); d3.select(this).call(yAxis); });
var cell = svg.selectAll(".cell")
.data(cross(traits, traits))
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")"; })
.each(plot);
// Titles for the diagonal.
cell.filter(function(d) { return d.i === d.j; }).append("text")
.attr("x", padding)
.attr("y", padding)
.attr("dy", ".71em")
.text(function(d) { return d.x; });
function plot(p) {
var cell = d3.select(this);
x.domain(domainByTrait[p.x]);
y.domain(domainByTrait[p.y]);
cell.append("rect")
.attr("class", "frame")
.attr("x", padding / 2)
.attr("y", padding / 2)
.attr("width", size - padding)
.attr("height", size - padding);
cell.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d) { return x(d[p.x]); })
.attr("cy", function(d) { return y(d[p.y]); })
.attr("r", 2)
.style("fill", function(d) { return color(d.tiername); });
}
});
//what is this doing?
function cross(a, b) {
var c = [], n = a.length, m = b.length, i, j;
for (i = -1; ++i < n;)
for (j = -1; ++j < m;)
c.push({x: a[i], i: i, y: b[j], j: j});
return c;
}
</script>
k_median k_rank_cond_parq1 k_rank_cond_parq2 k_rank_cond_parq3 k_rank_cond_parq4 k_rank_cond_parq5 tiername
83000 0.726 0.739 0.73 0.79 0.786 Highly selective
47900 0.588 0.651 0.6 0.664 0.652 Highly selective
69900 0.791 0.668 0.696 0.765 0.753 Highly selective
82400 0.867 0.782 0.8 0.7 0.808 Highly selective
49000 0.649 0.621 0.67 0.679 0.641 Highly selective
55800 0.653 0.682 0.696 0.664 0.671 Highly selective
43500 0.582 0.671 0.594 0.676 0.596 Highly selective
62000 0.628 0.696 0.669 0.704 0.738 Highly selective
72500 0.74 0.699 0.731 0.754 0.758 Highly selective
46400 0.656 0.559 0.565 0.668 0.611 Highly selective
67900 0.699 0.717 0.726 0.732 0.749 Highly selective
60400 0.707 0.675 0.71 0.699 0.717 Highly selective
65800 0.694 0.71 0.712 0.713 0.741 Highly selective
65300 0.701 0.713 0.716 0.722 0.744 Highly selective
58800 0.67 0.671 0.672 0.71 0.715 Highly selective
61200 0.666 0.664 0.702 0.694 0.694 Highly selective
63700 0.687 0.694 0.691 0.723 0.719 Highly selective
47600 0.577 0.522 0.606 0.63 0.643 Highly selective
42800 0.487 0.491 0.584 0.454 0.63 Nonselective
29700 0.4 0.557 0.521 0.521 0.514 Nonselective
25300 0.397 0.527 0.466 0.499 0.546 Nonselective
47300 0.585 0.505 0.667 0.661 0.624 Nonselective
31300 0.566 0.512 0.441 0.483 0.506 Nonselective
47600 0.582 0.677 0.587 0.582 0.575 Nonselective
42100 0.572 0.608 0.597 0.595 0.601 Selective
35700 0.503 0.615 0.566 0.544 0.562 Selective
36200 0.499 0.533 0.566 0.592 0.545 Selective
50900 0.655 0.621 0.679 0.683 0.664 Selective
85800 0.916 0.735 0.718 0.783 0.81 Selective
65500 0.691 0.716 0.735 0.718 0.741 Selective
55100 0.674 0.681 0.672 0.692 0.702 Selective
47900 0.624 0.625 0.656 0.657 0.659 Selective
46100 0.64 0.618 0.607 0.632 0.633 Selective
48700 0.605 0.597 0.636 0.636 0.685 Selective
40300 0.565 0.609 0.62 0.607 0.618 Selective
51300 0.638 0.646 0.62 0.679 0.694 Selective
44400 0.587 0.605 0.616 0.638 0.661 Selective
47800 0.631 0.634 0.632 0.644 0.656 Selective
48800 0.643 0.638 0.642 0.659 0.661 Selective
43000 0.606 0.617 0.604 0.635 0.645 Selective
41100 0.629 0.576 0.579 0.604 0.618 Selective
44100 0.607 0.614 0.617 0.641 0.635 Selective
43500 0.595 0.576 0.614 0.627 0.649 Selective
44400 0.568 0.629 0.633 0.64 0.631 Selective
44800 0.608 0.608 0.614 0.626 0.627 Selective
39600 0.574 0.592 0.594 0.566 0.587 Selective
45200 0.645 0.602 0.621 0.615 0.67 Selective
37900 0.637 0.592 0.563 0.566 0.603 Selective
35500 0.499 0.508 0.552 0.564 0.581 Selective
43200 0.606 0.66 0.594 0.579 0.644 Selective
56200 0.658 0.699 0.686 0.703 0.684 Selective
30700 0.51 0.55 0.505 0.518 0.54 Selective
40200 0.637 0.704 0.569 0.578 0.571 Selective
44900 0.615 0.635 0.676 0.623 0.65 Selective
50700 0.609 0.687 0.655 0.629 0.64 Selective
45900 0.654 0.575 0.616 0.625 0.623 Selective
55200 0.648 0.688 0.681 0.707 0.694 Selective
51000 0.641 0.641 0.66 0.668 0.68 Selective
45800 0.617 0.614 0.626 0.642 0.636 Selective
56500 0.676 0.684 0.69 0.719 0.703 Selective
27800 0.42 0.448 0.569 0.512 0.532 Selective
46400 0.63 0.589 0.62 0.623 0.654 Selective
61600 0.699 0.699 0.72 0.718 0.732 Selective
52800 0.64 0.673 0.67 0.677 0.69 Selective
46100 0.611 0.632 0.63 0.641 0.636 Selective
48600 0.568 0.617 0.683 0.679 0.663 Selective
47700 0.646 0.643 0.597 0.658 0.653 Selective
56900 0.653 0.671 0.698 0.709 0.685 Selective
59000 0.692 0.686 0.716 0.715 0.707 Selective
32900 0.508 0.467 0.555 0.543 0.565 Selective
47900 0.605 0.548 0.637 0.646 0.646 Selective
44300 0.58 0.613 0.612 0.663 0.624 Selective
30800 0.481 0.492 0.503 0.539 0.551 Two-year
25700 0.411 0.479 0.493 0.523 0.529 Two-year
28700 0.484 0.51 0.497 0.529 0.548 Two-year
27300 0.427 0.467 0.495 0.53 0.551 Two-year
26500 0.439 0.487 0.485 0.521 0.515 Two-year
30200 0.483 0.517 0.526 0.532 0.546 Two-year
35200 0.476 0.527 0.539 0.57 0.589 Two-year
27700 0.443 0.494 0.5 0.508 0.525 Two-year
28100 0.471 0.492 0.506 0.506 0.535 Two-year
30600 0.495 0.505 0.513 0.531 0.54 Two-year
29700 0.448 0.488 0.529 0.483 0.539 Two-year
32500 0.477 0.502 0.537 0.541 0.578 Two-year
28400 0.482 0.497 0.505 0.514 0.537 Two-year
20300 0.379 0.423 0.448 0.477 0.475 Two-year
27500 0.456 0.491 0.511 0.532 0.547 Two-year
27900 0.399 0.471 0.546 0.526 0.547 Two-year
32900 0.473 0.503 0.529 0.546 0.569 Two-year
27100 0.467 0.497 0.525 0.51 0.52 Two-year
31000 0.485 0.49 0.512 0.545 0.562 Two-year
28300 0.474 0.501 0.509 0.517 0.523 Two-year
23400 0.418 0.468 0.485 0.478 0.478 Two-year
33900 0.486 0.523 0.552 0.565 0.576 Two-year
29800 0.481 0.496 0.494 0.528 0.559 Two-year
30500 0.518 0.527 0.539 0.533 0.522 Two-year
31700 0.48 0.506 0.521 0.548 0.564 Two-year
30500 0.491 0.518 0.523 0.534 0.549 Two-year
25800 0.455 0.496 0.534 0.515 0.549 Two-year
30900 0.466 0.521 0.516 0.534 0.539 Two-year
30000 0.486 0.513 0.525 0.532 0.557 Two-year
22100 0.427 0.405 0.479 0.481 0.486 Two-year
37300 0.521 0.519 0.567 0.54 0.596 Two-year
23300 0.378 0.447 0.473 0.506 0.527 Two-year
28100 0.461 0.484 0.522 0.522 0.552 Two-year
28400 0.486 0.506 0.514 0.522 0.523 Two-year
31200 0.485 0.503 0.521 0.532 0.552 Two-year
21400 0.384 0.42 0.444 0.469 0.53 Two-year
25400 0.441 0.471 0.493 0.502 0.523 Two-year
26500 0.44 0.472 0.504 0.506 0.52 Two-year
27200 0.437 0.487 0.493 0.532 0.533 Two-year
30800 0.494 0.508 0.515 0.549 0.558 Two-year
25100 0.446 0.467 0.476 0.49 0.508 Two-year
33700 0.496 0.509 0.547 0.541 0.568 Two-year
38500 0.569 0.54 0.559 0.599 0.607 Two-year
31500 0.497 0.511 0.524 0.548 0.543 Two-year
29500 0.503 0.501 0.518 0.523 0.526 Two-year
33700 0.515 0.522 0.551 0.56 0.571 Two-year
32400 0.516 0.528 0.551 0.551 0.552 Two-year
28800 0.47 0.499 0.5 0.525 0.53 Two-year
31100 0.467 0.504 0.513 0.528 0.543 Two-year
25900 0.449 0.462 0.496 0.525 0.537 Two-year
30900 0.479 0.503 0.531 0.539 0.559 Two-year
33000 0.509 0.544 0.533 0.552 0.555 Two-year
30700 0.49 0.495 0.519 0.537 0.559 Two-year
31900 0.518 0.501 0.545 0.565 0.548 Two-year
37800 0.531 0.539 0.565 0.595 0.611 Two-year
31400 0.486 0.511 0.534 0.54 0.545 Two-year
28800 0.506 0.504 0.518 0.526 0.516 Two-year
31600 0.489 0.51 0.534 0.539 0.557 Two-year
30900 0.458 0.499 0.514 0.542 0.556 Two-year
34300 0.51 0.526 0.552 0.56 0.565 Two-year
28200 0.479 0.486 0.511 0.52 0.543 Two-year
25200 0.436 0.468 0.493 0.511 0.536 Two-year
32300 0.459 0.529 0.498 0.517 0.6 Two-year
30600 0.479 0.5 0.519 0.528 0.559 Two-year
25200 0.425 0.473 0.489 0.514 0.554 Two-year
25600 0.47 0.483 0.492 0.511 0.515 Two-year
36700 0.567 0.549 0.551 0.567 0.589 Two-year
28900 0.48 0.502 0.505 0.533 0.527 Two-year
25400 0.44 0.472 0.469 0.52 0.522 Two-year
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment