Skip to content

Instantly share code, notes, and snippets.

@emilyinamillion
Last active May 20, 2016 21:08
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 emilyinamillion/d18b57b553d84a6419856be7090a5012 to your computer and use it in GitHub Desktop.
Save emilyinamillion/d18b57b553d84a6419856be7090a5012 to your computer and use it in GitHub Desktop.
Stacked Bar Chart
license: gpl-3.0

This is my first visualization with D3, using data from a project I recently did with Metis. I took the yelp corpus from 1/2 star ratings and took the twitter vocabulary for tweets my model predicted were similar to the 1/2 star ratings and plotted the top 100 words for each (excluding stopwords).

Some things I think are interesting about this are how much the words overlap/how much they don't, and how accurate the words themselves are, despite possibly not occurring in the yelp reviews at all.

  • Further iterations will possibly include:
    • scroll bar side to side

    • yelp/twitter button to show only one or the other

    • hover over examples

    • animations just because animations

forked from mbostock's block: Stacked Bar Chart

State Under 5 Years 5 to 13 Years 14 to 17 Years 18 to 24 Years 25 to 44 Years 45 to 64 Years 65 Years and Over
AL 310504 552339 259034 450818 1231572 1215966 641667
AK 52083 85640 42153 74257 198724 183159 50277
AZ 515910 828669 362642 601943 1804762 1523681 862573
AR 202070 343207 157204 264160 754420 727124 407205
CA 2704659 4499890 2159981 3853788 10604510 8819342 4114496
CO 358280 587154 261701 466194 1464939 1290094 511094
CT 211637 403658 196918 325110 916955 968967 478007
DE 59319 99496 47414 84464 230183 230528 121688
DC 36352 50439 25225 75569 193557 140043 70648
FL 1140516 1938695 925060 1607297 4782119 4746856 3187797
GA 740521 1250460 557860 919876 2846985 2389018 981024
HI 87207 134025 64011 124834 356237 331817 190067
ID 121746 201192 89702 147606 406247 375173 182150
IL 894368 1558919 725973 1311479 3596343 3239173 1575308
IN 443089 780199 361393 605863 1724528 1647881 813839
IA 201321 345409 165883 306398 750505 788485 444554
KS 202529 342134 155822 293114 728166 713663 366706
KY 284601 493536 229927 381394 1179637 1134283 565867
LA 310716 542341 254916 471275 1162463 1128771 540314
ME 71459 133656 69752 112682 331809 397911 199187
MD 371787 651923 316873 543470 1556225 1513754 679565
MA 383568 701752 341713 665879 1782449 1751508 871098
MI 625526 1179503 585169 974480 2628322 2706100 1304322
MN 358471 606802 289371 507289 1416063 1391878 650519
MS 220813 371502 174405 305964 764203 730133 371598
MO 399450 690476 331543 560463 1569626 1554812 805235
MT 61114 106088 53156 95232 236297 278241 137312
NE 132092 215265 99638 186657 457177 451756 240847
NV 199175 325650 142976 212379 769913 653357 296717
NH 75297 144235 73826 119114 345109 388250 169978
NJ 557421 1011656 478505 769321 2379649 2335168 1150941
NM 148323 241326 112801 203097 517154 501604 260051
NY 1208495 2141490 1058031 1999120 5355235 5120254 2607672
NC 652823 1097890 492964 883397 2575603 2380685 1139052
ND 41896 67358 33794 82629 154913 166615 94276
OH 743750 1340492 646135 1081734 3019147 3083815 1570837
OK 266547 438926 200562 369916 957085 918688 490637
OR 243483 424167 199925 338162 1044056 1036269 503998
PA 737462 1345341 679201 1203944 3157759 3414001 1910571
RI 60934 111408 56198 114502 277779 282321 147646
SC 303024 517803 245400 438147 1193112 1186019 596295
SD 58566 94438 45305 82869 196738 210178 116100
TN 416334 725948 336312 550612 1719433 1646623 819626
TX 2027307 3277946 1420518 2454721 7017731 5656528 2472223
UT 268916 413034 167685 329585 772024 538978 246202
VT 32635 62538 33757 61679 155419 188593 86649
VA 522672 887525 413004 768475 2203286 2033550 940577
WA 433119 750274 357782 610378 1850983 1762811 783877
WV 105435 189649 91074 157989 470749 514505 285067
WI 362277 640286 311849 553914 1487457 1522038 750146
WY 38253 60890 29314 53980 137338 147279 65614
word Yelp Frequency Twitter Frequency Yelp Weight Twitter Weight TotalWeight
after 79199 5 0.397979625 0.241429261 0.639408886
again 54290 0 0.272810437 0 0.272810437
always 0 4 0 0.193143409 0.193143409
another 40932 0 0.205685703 0 0.205685703
any 40386 0 0.202942021 0 0.202942021
asked 56846 0 0.285654488 0 0.285654488
back 106999 4 0.537676257 0.193143409 0.730819666
bad 55867 12 0.280734955 0.579430227 0.860165182
bar 40735 8 0.204695767 0.386286818 0.590982585
before 42838 0 0.215263465 0 0.215263465
better 57657 0 0.28972981 0 0.28972981
came 75847 0 0.381135628 0 0.381135628
charge 0 5 0 0.241429261 0.241429261
cheese 38237 0 0.19214317 0 0.19214317
chicken 59589 10 0.299438224 0.482858522 0.782296746
come 42728 6 0.214710709 0.289715113 0.504425822
could 55702 0 0.27990582 0 0.27990582
day 0 4 0 0.193143409 0.193143409
did 57043 0 0.286644424 0 0.286644424
dinner 0 5 0 0.241429261 0.241429261
disappointed 0 4 0 0.193143409 0.193143409
don't 0 7 0 0.338000966 0.338000966
down 41564 5 0.20886154 0.241429261 0.450290801
drink 0 4 0.16174659 0.579343409 0.741089999
eat 48556 5 0.243996751 0.579429261 0.823426012
even 81644 4 0.410265893 0.193143409 0.603409302
ever 36699 0 0.18441463 0 0.18441463
experience 40288 0 0.202449565 0 0.202449565
find 0 3 0 0.144857557 0.144857557
first 51500 0 0.258790524 0 0.258790524
food 267148 10 1.342434385 0.482858522 1.825292907
found 0 4 0 0.193143409 0.193143409
friends 0 4 0 0.193143409 0.193143409
fuck 0 5 0 0.241429261 0.241429261
game 0 5 0 0.241429261 0.241429261
get 108541 18 0.545424898 0.86914534 1.414570238
gift 0 4 0 0.193143409 0.193143409
give 38030 0 0.191102983 0 0.191102983
going 43905 4 0.220625203 0.193143409 0.413768612
good 145964 5 0.73347767 0.241429261 0.974906931
great 53352 0 0.268096932 0 0.268096932
grill 0 5 0 0.241429261 0.241429261
guys 0 4 0 0.193143409 0.193143409
he 95199 0 0.478380564 0 0.478380564
here 107385 5 0.53961593 0.241429261 0.781045191
hey 0 4 0 0.193143409 0.193143409
how 46575 4 0.234042109 0.193143409 0.427185518
know 46054 4 0.231424054 0.193143409 0.424567463
last 0 5 0 0.241429261 0.241429261
like 139239 14 0.69968415 0.676001931 1.375686081
little 34169 0 0.171701231 0 0.171701231
location 0 4 0 0.4 0.4
made 35683 0 0.36930917 0.241429261 0.610738431
manager 35431 0 0.178042855 0 0.178042855
me 131509 26 0.660840446 1.255432158 1.916272604
meal 39383 0 0.197901887 0 0.197901887
menu 47144 0 0.236901368 0 0.236901368
minutes 68272 5 0.343070808 0.241429261 0.584500069
more 65404 0 0.32865894 0 0.32865894
much 51249 0 0.257529234 0 0.257529234
need 0 7 0 0.338000966 0.338000966
never 66502 4 0.334176454 0.193143409 0.527319863
nice 36070 0 0.181253868 0 0.181253868
night 34428 0 0.173002721 0 0.173002721
not 0 21 0 1.014002897 1.014002897
nothing 35386 5 0.177816728 0.241429261 0.419245989
off 47651 0 0.239449073 0 0.239449073
ok 35579 0 0.178786564 0 0.178786564
order 95091 7 0.973837858 0.578000966 1.551838824
other 62892 0 0.316035992 0 0.316035992
out 138878 7 0.697870104 0.338000966 1.03587107
over 64426 4 0.323744433 0.193143409 0.516887842
people 49064 0 0.246549481 0 0.246549481
pizza 46152 0 0.23191651 0 0.23191651
place 162301 0 0.815572054 0 0.815572054
pretty 37797 0 0.189932144 0 0.189932144
put 0 4 0 0.193143409 0.193143409
really 78023 4 0.392070156 0.193143409 0.585213565
restaurant 77333 14 0.388602865 0.676001931 1.064604796
right 31848 0 0.16003807 0 0.16003807
said 60668 4 0.304860262 0.193143409 0.498003671
salad 35732 4 0.179555398 0.193143409 0.372698807
sauce 39001 0 0.195982315 0 0.195982315
say 38858 0 0.195263731 0 0.195263731
send 0 4 0 0.193143409 0.193143409
server 44207 0 0.22214277 0 0.22214277
service 131320 13 0.65989071 0.847716079 1.507606789
she 104567 0 0.525455314 0 0.525455314
shit 0 5 0 0.241429261 0.241429261
should 35980 5 0.180801612 0.241429261 0.422230873
something 31864 0 0.160118471 0 0.160118471
staff 31600 0 0.158791855 0 0.158791855
steakhouse 0 6 0 0.289715113 0.289715113
still 35778 0 0.179786551 0 0.179786551
stop 0 6 0 0.289715113 0.289715113
table 60087 4 0.301940703 0.193143409 0.495084112
take 36101 6 0.181409645 0.289715113 0.471124758
taste 31844 0 0.16001797 0 0.16001797
then 63285 0 0.318010841 0 0.318010841
think 37208 0 0.186972385 0 0.186972385
time 106527 3 0.535304429 0.144857557 0.680161986
today 0 8 0 0.386286818 0.386286818
told 42157 5 0.2118414 0.241429261 0.453270661
took 46280 0 0.232559717 0 0.232559717
tried 37306 3 0.18746484 0.144857557 0.332322397
two 46339 0 0.232856196 0 0.232856196
very 104248 0 0.523852321 0 0.523852321
wait 41016 6 0.206107808 0.289715113 0.495822921
waitress 37341 0 0.187640717 0 0.187640717
want 36271 8 0.342263905 0.386286818 0.728550723
wasn't 42045 0 0.211278594 0 0.211278594
way 44772 4 0.224981929 0.193143409 0.418125338
well 45656 0 0.229424081 0 0.229424081
went 55909 3 0.280946008 0.144857557 0.425803565
what 0 4 0 0.193143409 0.193143409
when 0 7 0 0.338000966 0.338000966
while 32432 0 0.162972704 0 0.162972704
who 41699 0 0.209539923 0 0.209539923
why 0 6 0 0.289715113 0.289715113
wings 0 4 0 0.193143409 0.193143409
worst 0 5 0 0.241429261 0.241429261
word Yelp Twitter
again 0.272810437 0
another 0.205685703 0
any 0.202942021 0
asked 0.285654488 0
before 0.215263465 0
better 0.28972981 0
came 0.381135628 0
cheese 0.19214317 0
could 0.27990582 0
did 0.286644424 0
ever 0.18441463 0
experience 0.202449565 0
first 0.258790524 0
give 0.191102983 0
great 0.268096932 0
he 0.478380564 0
little 0.171701231 0
manager 0.178042855 0
meal 0.197901887 0
menu 0.236901368 0
more 0.32865894 0
much 0.257529234 0
nice 0.181253868 0
night 0.173002721 0
off 0.239449073 0
ok 0.178786564 0
other 0.316035992 0
people 0.246549481 0
pizza 0.23191651 0
place 0.815572054 0
pretty 0.189932144 0
right 0.16003807 0
sauce 0.195982315 0
say 0.195263731 0
server 0.22214277 0
she 0.525455314 0
something 0.160118471 0
staff 0.158791855 0
still 0.179786551 0
taste 0.16001797 0
then 0.318010841 0
think 0.186972385 0
took 0.232559717 0
two 0.232856196 0
very 0.523852321 0
waitress 0.187640717 0
wasn't 0.211278594 0
well 0.229424081 0
while 0.162972704 0
who 0.209539923 0
time 0.535304429 0.144857557
tried 0.18746484 0.144857557
went 0.280946008 0.144857557
back 0.537676257 0.193143409
disappointed 0 0.193143409
even 0.410265893 0.193143409
found 0 0.193143409
friends 0 0.193143409
gift 0 0.193143409
going 0.220625203 0.193143409
guys 0 0.193143409
hey 0 0.193143409
how 0.234042109 0.193143409
know 0.231424054 0.193143409
never 0.334176454 0.193143409
over 0.323744433 0.193143409
put 0 0.193143409
really 0.392070156 0.193143409
said 0.304860262 0.193143409
salad 0.179555398 0.193143409
send 0 0.193143409
table 0.301940703 0.193143409
way 0.224981929 0.193143409
wings 0 0.193143409
after 0.397979625 0.241429261
charge 0 0.241429261
dinner 0 0.241429261
down 0.20886154 0.241429261
fuck 0 0.241429261
game 0 0.241429261
good 0.73347767 0.241429261
grill 0 0.241429261
here 0.53961593 0.241429261
last 0 0.241429261
made 0.36930917 0.241429261
minutes 0.343070808 0.241429261
nothing 0.177816728 0.241429261
shit 0 0.241429261
should 0.180801612 0.241429261
told 0.2118414 0.241429261
worst 0 0.241429261
come 0.214710709 0.289715113
steakhouse 0 0.289715113
stop 0 0.289715113
take 0.181409645 0.289715113
wait 0.206107808 0.289715113
why 0 0.289715113
don't 0 0.338000966
need 0 0.338000966
out 0.697870104 0.338000966
when 0 0.338000966
bar 0.204695767 0.386286818
today 0 0.386286818
want 0.342263905 0.386286818
location 0 0.4
chicken 0.299438224 0.482858522
food 1.342434385 0.482858522
order 0.973837858 0.578000966
drink 0.16174659 0.579343409
eat 0.243996751 0.579429261
bad 0.280734955 0.579430227
like 0.69968415 0.676001931
restaurant 0.388602865 0.676001931
service 0.65989071 0.847716079
get 0.545424898 0.86914534
not 0 1.014002897
me 0.660840446 1.255432158
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 8px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: blue;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: -5, bottom: 50, left: 35},
width = 961 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.ordinal()
.range(["#D98880", "#A9CCE3", ]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
//.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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("fullshebangdata4d3.csv", function(error, data) {
if (error) throw error;
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "word"; }));
data.forEach(function(d) {
var y0 = 0;
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.ages[d.ages.length - 1].y1;
});
data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return d.word; }));
y.domain([0, d3.max(data, function(d) { return d.total; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.80em")
.attr("transform", "rotate(-90)")
;
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "1.43704em")
.attr("font-size", "20")
.style("text-anchor", "end")
.text("Word Frequency");
var state = svg.selectAll(".word")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.word) + ",0)"; });
state.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 59)
.attr("y", 9)
.attr("width", 27)
.attr("height", 16)
.style("fill", color);
legend.append("text")
.attr("x", width - 67)
.attr("y", 18)
.attr("font-size", 19)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment