{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Categorical data visualization with GnuplotRB" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Object" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'gnuplotrb'\n", "require 'daru'\n", "include GnuplotRB" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting categorical vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bar graph of frequencies" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
0a
1a
2a
3b
4b
" ], "text/plain": [ "#\n", " 0 a\n", " 1 a\n", " 2 a\n", " 3 b\n", " 4 b" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new [:a, :a, :a, :b, :b], type: :category" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "Gnuplot\n", "Produced by GNUPLOT 5.0 patchlevel 3 \n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\n", "\n", "\n", "\n", "\t\t\n", "\t\t 0\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 2\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 4\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 6\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 8\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 10\n", "\t\n", "\n", "\n", "\t\t\n", "\t\ta\n", "\t\n", "\n", "\n", "\t\t\n", "\t\tb\n", "\t\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\tgnuplot_plot_1\n", "\n", "\t\t\n", "\t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\n" ], "text/plain": [ "# -1..2, :yrange => 0..10], @datasets=Hamster::Vector[#, @options=Hamster::Hash[:title => nil, :with => \"histogram\", :using => \"2:xtic(1)\"]>], @cmd=\"plot \">" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plot.new [dv.frequencies, with: 'histogram'], xrange: -1..2, yrange: 0..10" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Bar graph of percentage" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "Gnuplot\n", "Produced by GNUPLOT 5.0 patchlevel 3 \n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\n", "\n", "\n", "\n", "\t\t\n", "\t\t 0\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 20\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 40\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 60\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 80\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 100\n", "\t\n", "\n", "\n", "\t\t\n", "\t\ta\n", "\t\n", "\n", "\n", "\t\t\n", "\t\tb\n", "\t\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\tgnuplot_plot_1\n", "\n", "\t\t\n", "\t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\n" ], "text/plain": [ "# -1..2, :yrange => 0..100], @datasets=Hamster::Vector[#, @options=Hamster::Hash[:title => nil, :with => \"histogram\", :using => \"2:xtic(1)\"]>], @cmd=\"plot \">" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plot.new [dv.frequencies(:percentage), with: 'histogram'], xrange: -1..2, yrange: 0..100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting dataframes with categorical data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " x: [1, 2, 3, 4, 5, 6, 7, 8],\n", " y: [3, 2, 3, 5, 6, 1, 2, 3],\n", " cat: ['III', 'II', 'I', 'I', 'II', 'I', 'III', 'III']\n", "})\n", "df[:cat] = df[:cat].to_category categories: ['I', 'II', 'III']\n", "df[:cat].type" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "Gnuplot\n", "Produced by GNUPLOT 5.0 patchlevel 3 \n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\t\n", "\t \n", "\t \n", "\t\n", "\n", "\n", "\n", "\n", "\t\t\n", "\t\t 0\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 2\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 4\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 6\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 8\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 10\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 0\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 2\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 4\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 6\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 8\n", "\t\n", "\n", "\n", "\t\t\n", "\t\t 10\n", "\t\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\tI\n", "\n", "\t\n", "\t\tI\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "\t\n", "\tII\n", "\n", "\t\n", "\t\tII\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "\t\n", "\tIII\n", "\n", "\t\n", "\t\tIII\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "\t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\t\n", "\n", "\n", "\n" ], "text/plain": [ "# 0..10, :yrange => 0..10], @datasets=Hamster::Vector[#, @options=Hamster::Hash[:title => \"I\", :using => \"2:3\"]>, #, @options=Hamster::Hash[:title => \"II\", :using => \"2:3\"]>, #, @options=Hamster::Hash[:title => \"III\", :using => \"2:3\"]>], @cmd=\"plot \">" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dfs = df.split_by_category :cat\n", "# The line below is because of this issue: https://github.com/SciRuby/gnuplotrb/issues/8\n", "dfs.each { |df| df.index = df.nrows.times.to_a }\n", "###\n", "Plot.new(\n", " *dfs.map { |df| [df, using: 'x:y'] }, xrange: 0..10, yrange: 0..10\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.3.0", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.3.0" } }, "nbformat": 4, "nbformat_minor": 0 }