{ "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" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.0.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":9,\"columns\":[\"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\"],\"kotlin_dataframe\":[{\"miles per gallon\":18.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":307.0,\"engine horsepower\":130,\"vehicle weight (lbs.)\":3504,\"time to accelerate (sec.)\":12.0,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"chevrolet chevelle malibu\"},{\"miles per gallon\":15.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":350.0,\"engine horsepower\":165,\"vehicle weight (lbs.)\":3693,\"time to accelerate (sec.)\":11.5,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"buick skylark 320\"},{\"miles per gallon\":18.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":318.0,\"engine horsepower\":150,\"vehicle weight (lbs.)\":3436,\"time to accelerate (sec.)\":11.0,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"plymouth satellite\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 9

\n", " \n", " \n", " " ] }, "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "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": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "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": 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\", 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": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 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\"), \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.8.20" } }, "nbformat": 4, "nbformat_minor": 4 }