Skip to content

Instantly share code, notes, and snippets.

@andrewwitherspoon
Last active August 22, 2017 01:17
Show Gist options
  • Save andrewwitherspoon/9b2d5684aad0cd5c5e9d731a4865e0f3 to your computer and use it in GitHub Desktop.
Save andrewwitherspoon/9b2d5684aad0cd5c5e9d731a4865e0f3 to your computer and use it in GitHub Desktop.
Scatterplot
license: gpl-3.0
company twitter facebook
Arizona Republic -0.5786799802 -0.4888028337
Breitbart 0.953431373 0.8517288919
BuzzFeed -1.093135478 -0.812305703
Dallas Morning News -0.5600793801 -1.099702783
Florida Times-Union -1.052635043 -1.167190332
Huffington Post -0.9905037139 -1.425391949
Las Vegas Review-Journal -0.46704582 -0.6107511039
National Review 0.9016068743 0.7905246693
New Yorker -1.071515543 -0.6251366742
New York Daily News -1.162581853 -1.046564165
New York Post -0.7313084222 -0.1253380153
New York Times -1.124177018 -0.03812370545
Orange County Register -0.5824060758 -0.9702320932
Oklahoman -0.6925524696 -1.131569906
Politico -0.7916375777 -0.4942751173
RealClearPolitics 0.5863952782 0.2497833539
Tennessean -0.2879061304 -0.5048091056
The Hill -0.6281565585 -0.3461164074
USA TODAY -0.8062933547 -0.551398019
Vox -1.257181802 -0.3642899745
Washington Post -0.9672006281 -0.003520663582
Washington Times 1.034561349 1.191045644
Weekly Standard 1.110585958 1.003366897
Wall Street Journal -0.8132031625 0.4199379942
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 40, right: 40, bottom: 40, left: 40},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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.tsv("data.tsv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.twitter = +d.twitter;
d.facebook = +d.facebook;
});
x.domain(d3.extent(data, function(d) { return d.twitter; })).nice();
y.domain(d3.extent(data, function(d) { return d.facebook; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Twitter");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Facebook")
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.twitter); })
.attr("cy", function(d) { return y(d.facebook); })
.style("fill", function(d) { return color(d.correlation); });
svg.selectAll("stuff")
.data(data)
.enter().append("text")
.attr("class", "text")
.attr("x", function(d) { return x(d.twitter); })
.attr("y", function(d) { return y(d.facebook); })
.text( function(d) { return (d.company); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment