{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Plotting with Gadfly.jl\n", "\n", "* [GitHub Repository](https://github.com/GiovineItalia/Gadfly.jl)\n", "* [Documentation](http://gadflyjl.org/stable/)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Method definition describe(AbstractArray) in module StatsBase at /Users/mrestrep/.julia/v0.5/StatsBase/src/scalarstats.jl:573 overwritten in module DataFrames at /Users/mrestrep/.julia/v0.5/DataFrames/src/abstractdataframe/abstractdataframe.jl:407.\n" ] } ], "source": [ "using Gadfly\n", "using DataFrames, RDatasets" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Basics\n", "\n", "## Simple line scatter\n", "\n", "* Pass data keyword arguments inputs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ], "text/html": [ "\n", "\n" ], "text/plain": [ "Plot(...)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function linescatter()\n", " x = [1, 2, 3, 4]\n", " y = [10, 15, 13, 17]\n", " plot(x=x,y=y, Geom.line)\n", "end\n", "linescatter()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Customize and add multiple traces to one plot\n", "\n", "* Each trace is a layer with its own aesthetics\n", "\n", "**Challenge:** How to plot dashes?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ], "text/html": [ "\n", "\n" ], "text/plain": [ "Plot(...)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function multiple_scatter_traces()\n", " x=1:4\n", " l1 = layer(x = x, y = [10, 15, 13, 17], Geom.point)\n", " l2 = layer(x = x, y = [16, 5, 11, 9], Geom.line)\n", " l3 = layer(x = x, y = [12, 9, 15, 12], Geom.point, Geom.line)\n", " l4 = layer(x = x, y = [5, 10, 8, 12], Geom.line)\n", " plot(l1, l2, l3, l4)\n", "end\n", "multiple_scatter_traces()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Data labels and Figure Layout\n", "\n", "* Add the array of labels as an attribute to each trace\n", "* Layout modifies the \"Figure Pane Attributes\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ], "text/html": [ "\n", "\n" ], "text/plain": [ "Plot(...)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function data_labels()\n", " x = 1:5\n", " y1 = [1, 6, 3, 6, 1]\n", " y2 = [4, 1, 7, 1, 4]\n", " y1_labels = [\"A-1\", \"A-2\", \"A-3\", \"A-4\", \"A-5\"]\n", " y2_labels = [\"B-a\", \"B-b\", \"B-c\", \"B-d\", \"B-e\"]\n", "\n", " l1 = layer(x = x, y = y1, label=y1_labels, Geom.label, Geom.point)\n", " l2 = layer(x = x, y = y2, label=y2_labels, Geom.label, Geom.point) \n", " plot(l1,l2)\n", "end\n", "data_labels()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Area" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "function area1()\n", "end\n", "area1()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Other visual interpretations of Matrix Data and Subplots\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "function matrix_subplots()\n", "\n", "end\n", "\n", "matrix_subplots()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Advanced Layouts\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "function advanced_layouts()\n", "\n", "end\n", "advanced_layouts()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Stats Plots" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "function grouped_bar_example()\n", "\n", "end\n", "\n", "grouped_bar_example()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "function stacked_bar_example()\n", " \n", "end\n", "stacked_bar_example()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "function two_hists()\n", "\n", "end\n", "\n", "two_hists()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Box (Whisker) Plots" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "function box_plot()\n", " \n", "end\n", "box_plot()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Using DataFrames\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "
| SepalLength | SepalWidth | PetalLength | PetalWidth | Species | |
|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |