Skip to content

Instantly share code, notes, and snippets.

@niphadkarneha
Last active February 5, 2019 21:31
Show Gist options
  • Save niphadkarneha/b36cb026af1efe4d287af864d3fbf46f to your computer and use it in GitHub Desktop.
Save niphadkarneha/b36cb026af1efe4d287af864d3fbf46f to your computer and use it in GitHub Desktop.
S19-HW3-BarChart
license: gpl-3.0

CS 725 - Spring 2019 - Homework 3 - Bar Chart

  1. What is the type of mark used in the bar chart?
    Answer: The basic graphical element used in this bar chart is a line.

  2. List the channels, the attribute they are mapped to, and the data type of that attribute.
    Answer:
  • List of channels - Length and position
  • Attributes: Horizontal and Vertical, Vertical spatial Position for quantitative attribute and Horizontal spatial position for the categorical attribute.
  • Data type: Alphabet and Frequency.

3. After your changes, list the channels, the attribute they are mapped to, and the data type of that attribute.
    Answer: In the first chart, color hue channel was edited and different color were given to each Alphabet. From the examples given, colorScale/ Ordinal scale was used. I used the pastel color scheme, its user preference.
  • List of channels - Length, position, and color (hue).
  • Attributes: Horizontal and Vertical, Vertical spatial Position for quantitative attribute and Horizontal spatial position for the categorical attribute.
  • Data type: Alphabet and Frequency.
  1. After your changes, list the channels, the attribute they are mapped to, and the data type of that attribute. List all of the channels mapped to the frequency attribute.
    Answer:

  • List of channels - Length, position, and color (saturation).
  • Attributes: Horizontal and Vertical, Vertical spatial Position for quantitative attribute and Horizontal spatial position for the categorical attribute.
  • Data type: Alphabet and Frequency.
  • I tried putting in color range from Red-Green-Blue, color saturation is reflected for numbers/values in the same range.

References:
https://bl.ocks.org/TaraZhu/120d48c43be68e92da304cb08235d052
https://codepen.io/stevepepple/post/color-scales-for-charts-and-maps
http://www.workwithcolor.com/red-color-hue-range-01.htm

letter frequency
A .08167
B .01492
C .02782
D .04253
E .12702
F .02288
G .02015
H .06094
I .06966
J .00153
K .00772
L .04025
M .02406
N .06749
O .07507
P .01929
Q .00095
R .05987
S .06327
T .09056
U .02758
V .00978
W .02360
X .00150
Y .01974
Z .00074
<!DOCTYPE html>
<html>
<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;}
</style>
<body>
<p>Frequency of usage of letters in English</p>
<h2>Bar Chart 1</h2>
<div><svg id="chart1" width="800" height="400"></svg></div>
<h2>Bar Chart 2</h2>
<div><svg id="chart2" width="800" height="400"></svg></div>
<script>
// chart 1
var svg1 = d3.select("#chart1"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg1.attr("width") - margin.left - margin.right,
height = +svg1.attr("height") - margin.top - margin.bottom;
// See https://github.com/d3/d3-scale
var x1 = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y1 = d3.scaleLinear().rangeRound([height, 0]); // note that we've reversed the range
// creates new svg <g> space, sets new (0,0) at left, top margin
var g1 = svg1.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// load data
d3.tsv("data.tsv").then(function(data) {
// See https://www.dashingd3js.com/d3js-scales
// maps domain of x values (letters) to range of positions on x-axis
x1.domain(data.map(function(d) { return d.letter; }));
// maps domain of y values (frequencies 0, max freq) to range of positions on y-axis
y1.domain([0, d3.max(data, d => d.frequency)]); // shorthand
// x-axis
g1.append("g")
.attr("class", "axis x-axis")
.attr("transform", "translate(0," + height + ")") // move axis to bottom of chart
.call(d3.axisBottom(x1));
// y-axis
g1.append("g")
.attr("class", "axis y-axis")
.call(d3.axisLeft(y1).ticks(10, "#")); // number of ticks and type
// y-axis label
g1.append("text")
.attr("class", "label")
.attr("x", 0-margin.left) // set x position of label
.attr("y", 0-margin.top/2) // set y position of label
.style("text-anchor", "start") // left-justify
.text ("Frequency")
// bars
var categorical = [
{ "name" : "schemeAccent"},
{ "name" : "schemeDark2"},
{ "name" : "schemePastel2"},
{ "name" : "schemeSet2"},
{ "name" : "schemeSet1"},
{ "name" : "schemePastel1"},
{ "name" : "schemeCategory10"},
{ "name" : "schemeSet3"},
{ "name" : "schemePaired"},
{ "name" : "schemeCategory20"},
{ "name" : "schemeCategory20b"},
{ "name" : "schemeCategory20c"}
]
var colorScale = d3.scaleOrdinal(d3[categorical[7].name])
g1.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { console.log ("letter: " + d.letter + ", x: " + x1(d.letter)); return x1(d.letter); })
.attr("y", function(d) { console.log ("freq: " + d.frequency + ", y: " + y1(d.frequency)); return y1(d.frequency); })
.attr("width", x1.bandwidth()) // width of each band
.attr("height", function(d) { return height - y1(d.frequency); })
.style("fill", function(d, i) { return colorScale(i); }); // color of the bars
;
});
// chart 2
var svg2 = d3.select("#chart2"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg2.attr("width") - margin.left - margin.right,
height = +svg2.attr("height") - margin.top - margin.bottom;
var x2 = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y2 = d3.scaleLinear().rangeRound([height, 0]);
var g2 = svg2.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// load data
d3.tsv("data.tsv").then(function(data) {
x2.domain(data.map(function(d) { return d.letter; }));
y2.domain([0, d3.max(data, function(d) { return d.frequency; })]);
// x-axis
g2.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x2));
// y-axis
g2.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y2).ticks(10, "#"));
// y-axis label
g2.append("text")
.attr("class", "label")
.attr("x", 0-margin.left)
.attr("y", 0-margin.top/2)
.style("text-anchor", "begin")
.text ("Frequency")
// bars
var color = d3.scaleLinear() .domain([0.12702,0.09056,0.08167,0.07507,0.06966,0.06749,0.06327,0.06094,0.05987,0.04253,0.04025,0.02782,0.02758,0.02406,0.02360,0.02288,0.02015,0.01974,0.01929,0.01492,0.00978,0.00772,0.00153,0.00150,0.00095,0.00074])
.range(['#FFFAFA','#F4C2C2','#FF6961','#FF5C5C','#FF1C00','#FF0800','#FF0000','#CD5C5C','#E34234','#D73B3E','#CE1620','#CC0000','#B22222','#B31B1B','#A40000','#800000','#701C1C','#3C1414','#321414','#280f0f','#163b34','#318656','#53bf82','#53b6bf','#5380bf','#93d1d7','#d1dfef']);
g2.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x2(d.letter); })
.attr("y", function(d) { return y2(d.frequency); })
.attr("width", x2.bandwidth())
.attr("height", function(d) { return height - y2(d.frequency); })
.style("fill", function(d)
{return color(d.frequency);}) // color of the bars
;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment