{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating multi-panel plots using `facets`.\n", "\n", "#### Problem\n", "\n", "You want to see more aspects of your data and it's not practcal to use the regular `aesthetics` approach for that.\n", "\n", "#### Solution - `facets`\n", "\n", "You can add one or more new dimentions to your plot using `faceting`.\n", "\n", "This approach allows you to split up your data by one or more variables and plot the subsets of data together.\n", "\n", "\n", "In this demo we will explore how various faceting functions work, as well as the built-in `sorting` and `formatting` options.\n", "\n", "To learn more about formatting templates see: [Formatting](https://github.com/JetBrains/lets-plot-kotlin/blob/master/docs/formats.md)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "miles per gallon | number of cylinders | engine displacement (cu. inches) | engine horsepower | vehicle weight (lbs.) | time to accelerate (sec.) | model year | origin of car | vehicle name |
---|---|---|---|---|---|---|---|---|
18.0 | 8 | 307.0 | 130 | 3504 | 12.0 | 70 | US | chevrolet chevelle malibu |
15.0 | 8 | 350.0 | 165 | 3693 | 11.5 | 70 | US | buick skylark 320 |
18.0 | 8 | 318.0 | 150 | 3436 | 11.0 | 70 | US | plymouth satellite |
Shape: 3 x 9. \n", "
" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var data = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg2.csv\")\n", "data.head(3)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### One plot\n", "\n", "Create a scatter plot to show how `mpg` is related to a car's `engine horsepower`.\n", "\n", "Also use the `color` aesthetic to vizualise the region where a car was designed." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(data.toMap()) {x=\"engine horsepower\"; y=\"miles per gallon\"} + \n", " geomPoint {color=\"origin of car\"} +\n", " themeGrey()\n", "p + ggsize(800, 350)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More dimentions\n", "\n", "There are two functions for faceting:\n", "\n", " - facetGrid()\n", " - facetWrap()\n", "\n", "The former creates 2-D matrix of plot panels and latter creates 1-D strip of plot panels.\n", "\n", "We'll be using the `number of cylinders` variable as 1st fatceting variable, and sometimes the `origin of car` as a 2nd fatceting variable. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### facetGrid()\n", "\n", "The data can be split up by one or two variables that vary on the X and/or Y direction." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### One facet\n", "\n", "Let's split up the data by `number of cylinders`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetGrid(x=\"number of cylinders\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Two facets\n", "\n", "Split up the data by two faceting variables: `number of cylinders` and `origin of car`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetGrid(x=\"number of cylinders\", y=\"origin of car\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Formatting and sorting.\n", "\n", "Apply a formatting template to the `number of cylinders` and\n", "sort the `origin of car` values in discending order.\n", "\n", "To learn more about formatting templates see: [Formatting](https://github.com/JetBrains/lets-plot-kotlin/blob/master/docs/formats.md). " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetGrid(x=\"number of cylinders\", y=\"origin of car\", xFormat=\"{d} cyl\", yOrder=-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### facetWrap()\n", "\n", "The data can be split up by one or more variables. \n", "The panels layout is flexible and controlled by `ncol`, `nrow` and `dir` options." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### One facet\n", "\n", "Split data by the `number of cylinders` variable and arrange tiles in two rows." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetWrap(facets=\"number of cylinders\", nrow=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Two facets\n", "\n", "Split data by `origin of car` and `number of cylinders` and arrange tiles in 5 columns." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetWrap(facets=listOf(\"origin of car\", \"number of cylinders\"), ncol=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Arrange panels vertically.\n", "\n", "Use the `dir` parameter to arrange tiles by columns, in 3 columns (the default tile arrangment is \"by row\").\n", "\n", "Also, format `number of cylinders` labels and reverse the sorting direction for this facetting variable." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facetWrap(facets=listOf(\"origin of car\", \"number of cylinders\"), \n", " ncol=3,\n", " format=listOf(null, \"{} cyl\"),\n", " order=listOf(1, -1),\n", " dir=\"v\")" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.7.20-dev-1299" } }, "nbformat": 4, "nbformat_minor": 4 }