{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "gem 'nyaplot', '0.1.5'\n", "require 'nyaplot'\n", "require 'mapnya'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Case 1: The bubble chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mapnya bundles [countries](https://github.com/mledoze/countries) as its default map and dataset.It prepares Nyaplot::Countries as the usefult wraper for countries. Nyaplot::Countries.df shows its default dataframe. The dataframe countains information of each country, like name, cca3, location(lat and lng), region, and area." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "raw_df = Nyaplot::Countries.df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pick up some columns from the dataframe." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "hash = [:name, :lat, :lng, :area].map{|label| {label => raw_df.column(label).to_a}}.reduce({}){|memo, hash| memo.merge(hash)}\n", "df = Nyaplot::DataFrame.new(hash)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, let's make a bubble chart to visualize the area of each country.\n", "First, choose colorset in which each bubble will be filled." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "color = Nyaplot::Colors.Reds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then instantiate Nyaplot::MapPlot and add scatter. The result is shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot = Nyaplot::MapPlot.new\n", "sc = plot.add_with_df(df, :scatter, :lng, :lat) # x->:lng, y->lat\n", "sc.configure do\n", " tooltip_contents([:capital, :name, :area])\n", " color(color)\n", " size([10, 100000])\n", " size_by(:area)\n", " fill_by(:area)\n", "end\n", "plot.show" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Case2 :Fill countries in different colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Footnote: Figures are for estimated total military expenditure. China has published its own military expenditure data. See: www.stats.gov.cn. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "path = File.expand_path(\"../data/UNdata_Export_20140813_215958400.csv\", __FILE__)\n", "df = Nyaplot::DataFrame.from_csv(path)\n", "df.filter! {|row| !row[:value].nil?}\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "hash = [:name, :lat, :lng, :cca3].map{|label| {label => raw_df.column(label).to_a}}.reduce({}){|memo, hash| memo.merge(hash)}\n", "new_df = Nyaplot::DataFrame.new(hash)\n", "countries = new_df.column(:name).to_a\n", "df.filter!{|row| !countries.index(row[:country_or_area]).nil?}\n", "df.filter!{|row| row[:year]==2012}\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "countries = df.column(:country_or_area).to_a; value = []\n", "new_df.each_row do |row|\n", " if countries.index(row[:name]).nil?\n", " value.push(-1)\n", " elsif\n", " name = row[:name]\n", " value.push(df.filter{|row| row[:country_or_area] == name}.column(:value).to_a[0])\n", " end\n", "end\n", "new_df.value = value\n", "new_df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "color = Nyaplot::Colors.Blues" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sc_df = new_df.filter{|row| row[:value]>0}\n", "\n", "plot = Nyaplot::MapPlot.new\n", "plot.fill_map_with_df(new_df, :cca3, :value)\n", "sc = plot.add_with_df(sc_df, :scatter, :lng, :lat)\n", "sc.tooltip_contents([:name, :cca3, :value])\n", "sc.color(\"#fff\")\n", " plot.color(color)\n", "plot.show" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby", "language": "ruby", "name": "ruby" }, "language_info": { "name": "ruby", "version": "2.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }