Skip to content

Instantly share code, notes, and snippets.

@Jverma
Created July 19, 2015 15:15
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 Jverma/d5c80d80e675908e2cb0 to your computer and use it in GitHub Desktop.
Save Jverma/d5c80d80e675908e2cb0 to your computer and use it in GitHub Desktop.
Sentiment Scores of Tweets

Scatterplot of sentiment scores of tweets extracted using search API.

The sentiment is calculated using AFINN scores. For more information see Twitter Sentiment Analysis

It can be seen that a lot of the tweets are neutral (zero sentiment score).

<!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: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - 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.csv("sentiment_by_tweet.csv", function(error, data) {
data.forEach(function(d) {
d.Index = +d.Index;
d.Score = +d.Score;
});
x.domain(d3.extent(data, function(d) { return d.Index; })).nice();
y.domain(d3.extent(data, function(d) { return d.Score; })).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("Tweet Index");
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("Sentiment Score")
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 2.5)
.attr("cx", function(d) { return x(d.Index); })
.attr("cy", function(d) { return y(d.Score); })
.style("fill", function(d) { return 'steelblue' });
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Index Score
0 8
1 0
2 0
3 4
4 0
5 0
6 0
7 2
8 0
9 0
10 1
11 0
12 1
13 0
14 0
15 -2
16 -1
17 0
18 0
19 0
20 0
21 0
22 -2
23 0
24 1
25 0
26 -4
27 -2
28 -2
29 0
30 0
31 0
32 1
33 0
34 0
35 1
36 0
37 4
38 0
39 0
40 0
41 0
42 0
43 1
44 2
45 -2
46 0
47 0
48 0
49 0
50 0
51 4
52 0
53 0
54 0
55 0
56 0
57 0
58 0
59 0
60 0
61 0
62 0
63 0
64 0
65 0
66 2
67 -1
68 6
69 0
70 0
71 4
72 0
73 0
74 1
75 0
76 0
77 0
78 0
79 0
80 0
81 2
82 0
83 0
84 6
85 2
86 0
87 2
88 0
89 3
90 0
91 0
92 0
93 0
94 0
95 0
96 -2
97 0
98 0
99 0
100 0
101 0
102 2
103 2
104 3
105 0
106 0
107 0
108 0
109 0
110 0
111 0
112 0
113 -2
114 0
115 0
116 0
117 0
118 0
119 0
120 0
121 0
122 0
123 1
124 1
125 0
126 0
127 0
128 4
129 0
130 1
131 -1
132 0
133 0
134 0
135 2
136 4
137 0
138 0
139 4
140 0
141 0
142 5
143 5
144 0
145 0
146 3
147 4
148 2
149 0
150 2
151 0
152 2
153 0
154 0
155 0
156 0
157 0
158 0
159 0
160 0
161 0
162 0
163 4
164 0
165 0
166 3
167 0
168 0
169 1
170 0
171 0
172 -2
173 0
174 0
175 0
176 4
177 0
178 0
179 0
180 3
181 0
182 0
183 0
184 0
185 0
186 2
187 0
188 0
189 2
190 1
191 0
192 0
193 0
194 0
195 0
196 0
197 1
198 6
199 0
200 1
201 0
202 0
203 0
204 0
205 0
206 0
207 2
208 0
209 0
210 1
211 3
212 0
213 0
214 0
215 0
216 0
217 4
218 0
219 2
220 0
221 0
222 0
223 0
224 1
225 0
226 2
227 0
228 2
229 0
230 0
231 2
232 1
233 0
234 0
235 0
236 0
237 0
238 0
239 0
240 0
241 0
242 0
243 2
244 0
245 1
246 0
247 0
248 0
249 3
250 0
251 0
252 0
253 0
254 0
255 0
256 0
257 4
258 0
259 0
260 0
261 4
262 0
263 0
264 0
265 0
266 0
267 0
268 0
269 0
270 4
271 0
272 0
273 0
274 0
275 0
276 0
277 -1
278 0
279 0
280 0
281 0
282 0
283 -2
284 0
285 0
286 0
287 0
288 0
289 0
290 3
291 0
292 0
293 2
294 0
295 0
296 0
297 0
298 0
299 -1
300 0
301 0
302 1
303 1
304 0
305 0
306 -2
307 0
308 0
309 0
310 0
311 4
312 2
313 0
314 0
315 -1
316 0
317 0
318 0
319 0
320 0
321 2
322 0
323 0
324 0
325 0
326 -1
327 2
328 0
329 0
330 2
331 0
332 3
333 5
334 2
335 0
336 0
337 0
338 0
339 0
340 0
341 4
342 4
343 -3
344 0
345 0
346 0
347 0
348 0
349 0
350 0
351 0
352 -2
353 0
354 4
355 0
356 1
357 0
358 1
359 1
360 4
361 0
362 0
363 4
364 0
365 0
366 0
367 2
368 0
369 0
370 0
371 0
372 0
373 0
374 0
375 -1
376 0
377 0
378 3
379 1
380 0
381 0
382 0
383 0
384 0
385 4
386 0
387 0
388 0
389 0
390 0
391 0
392 0
393 2
394 0
395 4
396 0
397 0
398 -1
399 0
400 0
401 6
402 3
403 0
404 4
405 0
406 0
407 4
408 0
409 0
410 0
411 -2
412 0
413 5
414 0
415 0
416 0
417 0
418 0
419 0
420 0
421 0
422 0
423 5
424 3
425 0
426 0
427 3
428 0
429 0
430 0
431 0
432 -1
433 0
434 0
435 0
436 5
437 0
438 1
439 0
440 0
441 0
442 0
443 0
444 0
445 0
446 0
447 0
448 0
449 1
450 0
451 0
452 0
453 0
454 0
455 1
456 0
457 0
458 0
459 0
460 0
461 0
462 1
463 0
464 7
465 0
466 4
467 0
468 1
469 1
470 0
471 0
472 0
473 2
474 0
475 0
476 1
477 0
478 0
479 0
480 0
481 0
482 0
483 0
484 0
485 4
486 0
487 0
488 0
489 0
490 0
491 3
492 0
493 0
494 0
495 0
496 0
497 0
498 0
499 5
500 1
501 1
502 3
503 0
504 0
505 0
506 0
507 5
508 0
509 0
510 0
511 0
512 1
513 0
514 2
515 0
516 0
517 0
518 0
519 0
520 0
521 0
522 0
523 0
524 0
525 0
526 0
527 0
528 0
529 5
530 2
531 0
532 0
533 0
534 2
535 0
536 2
537 0
538 0
539 0
540 0
541 1
542 0
543 0
544 0
545 2
546 0
547 0
548 0
549 0
550 0
551 2
552 -1
553 0
554 0
555 0
556 0
557 2
558 0
559 0
560 0
561 0
562 0
563 0
564 -1
565 0
566 0
567 0
568 0
569 0
570 0
571 0
572 4
573 0
574 0
575 0
576 0
577 0
578 4
579 0
580 0
581 0
582 0
583 0
584 0
585 0
586 0
587 1
588 0
589 0
590 0
591 0
592 0
593 0
594 0
595 3
596 0
597 0
598 0
599 0
600 0
601 0
602 0
603 0
604 0
605 0
606 0
607 0
608 1
609 0
610 4
611 0
612 0
613 0
614 0
615 0
616 0
617 0
618 0
619 0
620 0
621 0
622 2
623 0
624 0
625 0
626 0
627 0
628 5
629 0
630 0
631 0
632 3
633 0
634 0
635 2
636 0
637 1
638 -1
639 0
640 1
641 0
642 5
643 0
644 0
645 0
646 7
647 0
648 0
649 1
650 0
651 2
652 0
653 4
654 3
655 0
656 0
657 0
658 0
659 0
660 7
661 0
662 0
663 0
664 0
665 0
666 1
667 0
668 3
669 0
670 0
671 0
672 2
673 0
674 0
675 0
676 0
677 0
678 1
679 0
680 0
681 2
682 0
683 0
684 0
685 0
686 1
687 2
688 0
689 0
690 -2
691 0
692 0
693 0
694 -2
695 7
696 -2
697 0
698 0
699 0
700 0
701 3
702 6
703 4
704 0
705 3
706 0
707 0
708 0
709 0
710 7
711 3
712 0
713 1
714 0
715 0
716 0
717 0
718 1
719 0
720 0
721 1
722 0
723 0
724 0
725 0
726 0
727 0
728 0
729 0
730 0
731 0
732 4
733 0
734 0
735 0
736 0
737 0
738 0
739 0
740 4
741 0
742 4
743 0
744 0
745 0
746 0
747 0
748 1
749 2
750 0
751 0
752 0
753 0
754 0
755 0
756 0
757 4
758 0
759 0
760 0
761 1
762 0
763 0
764 3
765 0
766 0
767 0
768 2
769 0
770 0
771 1
772 0
773 0
774 0
775 0
776 0
777 0
778 0
779 3
780 0
781 0
782 0
783 0
784 0
785 0
786 0
787 2
788 0
789 7
790 0
791 3
792 0
793 0
794 0
795 0
796 0
797 0
798 0
799 0
800 0
801 4
802 1
803 0
804 2
805 -1
806 0
807 0
808 0
809 0
810 1
811 0
812 0
813 0
814 0
815 -1
816 2
817 4
818 4
819 0
820 2
821 0
822 2
823 0
824 0
825 0
826 0
827 0
828 1
829 0
830 0
831 0
832 0
833 0
834 2
835 0
836 5
837 0
838 1
839 0
840 0
841 0
842 -2
843 3
844 -2
845 0
846 0
847 0
848 0
849 0
850 0
851 0
852 0
853 0
854 0
855 0
856 0
857 0
858 0
859 0
860 2
861 2
862 0
863 0
864 1
865 1
866 0
867 1
868 0
869 1
870 3
871 0
872 0
873 0
874 4
875 7
876 0
877 0
878 0
879 1
880 4
881 0
882 0
883 4
884 0
885 4
886 0
887 0
888 0
889 0
890 0
891 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment