Skip to content

Instantly share code, notes, and snippets.

@niphadkarneha
Last active February 13, 2019 13:01
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 niphadkarneha/acebec8004f971d1f47abf69d1993046 to your computer and use it in GitHub Desktop.
Save niphadkarneha/acebec8004f971d1f47abf69d1993046 to your computer and use it in GitHub Desktop.
ScatterPlot
license: mit

Built with blockbuilder.org

Q1. List the marks/channels used and the data/attributes they map to. Answer: The visual mark and the attributes used to represent the data in this scatter plot is Points and shape. These two quantitative data points are located on the x and y axis of the 2D plane to represent the data which maps the Total number of Faculty members on the y-axis and the Total number of students on the x-axis. The idea being their is a positive correlation between these two datasets, as one variable increases so does the other.

Q2. Write an explanation of what the chart shows point out 1-2 interesting insights (i.e., things you learned from the chart) include links to any examples or helpful tutorials that you used.
Answer: It is difficult to eastablish a realtionship with just the data from the European Nations so I added few more countries at random to see if a pattern emerges. The points go vertically up following the y-axis having very little positive correlation.

References:

  1. http://bl.ocks.org/jfreels/6816504
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> <!-- for color scales -->
<style>
body {font-family: calibri;}
.axis {font: 14px calibri;}
.label {font: 16px calibri;}
.tooltip {
position: absolute;
width: 1105px;
height: 73px;
background: #f2f2f2;
pointer-events: none;}
</style>
</head>
<body>
<h3>Scatterplot</h3>
<div><svg id="chart1" width="800" height="451"></svg></div>
<script>
// add the graph canvas to the body of the webpage
var svg = d3.select("#chart1"),
margin = {top: 20, right: 55, bottom: 50, left: 70},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// load data
d3.csv("ScatterPlot1.csv").then(function(data) {
/*
* value accessor - returns the value to encode for a given data object.
* scale - maps value to a visual display encoding, such as a pixel position.
* map function - maps from data value to display value
*/
// setup x - use +d to change string (from CSV) into number format
var xValue = function(d) { return +d["numStudents"];}, // data -> value
xScale = d3.scaleLinear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}; // data -> display
// setup y - use +d to change string into number format
var yValue = function(d) { return +d["Faculty"];}, // data -> value
yScale = d3.scaleLinear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}; // data -> display
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
yScale.domain([0, d3.max(data, yValue)+1]);
//create color scale
var cValue = function(d) { return d['country'];},
colors = d3.scaleOrdinal(d3.schemePaired);
var sValue = function(d) { return parseInt(d['country']); },
sizes = d3.scaleLinear()
.domain([1.5, d3.max(data, sValue)+4.86])
.range([1.5, 6.5]);
// x-axis
g.append("g")
.attr("class", "axis x-axis")
.attr("transform", "translate(0," + height + ")") // move axis to bottom of chart
.call(d3.axisBottom(xScale));
// x-axis label
g.append("text")
.attr("class", "label")
.attr("x", width/2)
.attr("y", height+(margin.bottom*0.75))
.style("text-anchor", "middle")
.text("Number of Students");
// y-axis
g.append("g")
.attr("class", "axis y-axis")
.call(d3.axisLeft(yScale));
// y-axis label
g.append("text")
.attr("class", "label")
.attr("x", 0-(height/2))
.attr("y", 0-(margin.left*0.75))
.attr("transform", "rotate(-90)") // rotate text -90 degrees from x, y
.style("text-anchor", "middle")
.text("Number of Facutly Members");
// draw dots
g.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d) { return colors(cValue(d)); })
// tooltip
.on("mouseover", function(d) {
tooltip.transition()
.duration(200) // ms delay before appearing
.style("opacity", .8); // tooltip appears on mouseover
tooltip.html(d["Number of Students"] + "<br/> " + d.numStudents + "<br/>(" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 10) + "px") // specify x location
.style("top", (d3.event.pageY - 28) + "px"); // specify y location
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0); // disappear on mouseout
});
// draw legend
var legend = svg.selectAll(".legend")
.data(colors.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 15 + ")"; });
// draw legend colored rectangles
legend.append("rect")
.attr("x", width )
.attr("width", 8)
.attr("height", 7)
.style("fill", colors);
// draw legend text
legend.append("text")
.attr("x", width - 10)
.attr("y", 9)
.attr("dy", ".15em")
.style("text-anchor", "end")
.text(function(d,i) { return d;})
});
</script>
</body>
country numStudents Faculty
Albania 800 50
Brazil 24170 1728
Bulgaria 1116 79
Canada 166134 8257
China 6900 830
England 436465 9593
France 46911 6145
India 1990 215
Iran 114428 553
Italy 53830 4210
Japan 51942 4116
Jordan 17849 4116
Lebanon 6342 653
Mexico 48249 5124
Netherlands 49939 6171
Philippines 10688 817
Puerto Rico 15949 1649
Romania 38140 838
Russia 339 30
South Korea 48013 5286
Sri Lanka 22649 971
UK 802219 4083
Country numStudents Endowment
Albania 800 3000000000
Bulgaria 1116 17400000000000
Denmark 33899 5270000000000000
Finland 34200 624000000000000
France 46911 166000000
Italy 53830 562000000000000
Netherlands 49939 382700660000000
Romania 38140 120000000000000
United Kingdom 802219 802995072000000
Country numStudents Faculty Endowment
Albania 800 50 3000000000
Bulgaria 1116 79 17400000000000
Denmark 33899 0 5270000000000000
Finland 34200 0 624000000000000
France 46911 6145 166000000
Italy 53830 4210 562000000000000
Netherlands 49939 6171 382700660000000
Romania 38140 838 120000000000000
United Kingdom 802219 4083 802995072000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment