{ "cells": [ { "cell_type": "markdown", "id": "respective-majority", "metadata": {}, "source": [ "## Ordering Discrete Values" ] }, { "cell_type": "code", "execution_count": 1, "id": "protected-butter", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "// %use krangl" ] }, { "cell_type": "code", "execution_count": 2, "id": "selected-covering", "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, "id": "concrete-library", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "id": "emotional-stage", "metadata": {}, "outputs": [ { "data": { "text/html": [ "manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
Shape: 3 x 12. \n", "
" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpg = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpg.head(3)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "central-heart", "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(mpg.toMap()) { y = \"hwy\" } + theme(axisTitleX=\"blank\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "original-textbook", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p1 = p + ggtitle(\"default\") + geomBoxplot{ x = \"class\" }\n", "val p2 = p + ggtitle(\"alphabetically\") + geomBoxplot{ x = asDiscrete(\"class\", order = 1) }\n", "val p3 = p + ggtitle(\"by 'middle' ↑\") + geomBoxplot{ x = asDiscrete(\"class\", orderBy = \"..middle..\", order = 1) }\n", "val p4 = p + ggtitle(\"by 'middle' ↓\") + geomBoxplot{ x = asDiscrete(\"class\", orderBy = \"..middle..\", order = -1) }\n", "val p5 = p + ggtitle(\"by 'ymax' ↑\") + geomBoxplot{ x = asDiscrete(\"class\", orderBy = \"..ymax..\", order = 1) }\n", "val p6 = p + ggtitle(\"by 'ymax' ↓\") + geomBoxplot{ x = asDiscrete(\"class\", orderBy = \"..ymax..\", order = -1) }\n", " \n", "val w = 470 \n", "val h = 300\n", "\n", "val bunch = GGBunch()\n", "bunch.addPlot(p1, 0, 0, w, h)\n", "bunch.addPlot(p2, w, 0, w, h)\n", "bunch.addPlot(p3, 0, h, w, h)\n", "bunch.addPlot(p4, w, h, w, h)\n", "bunch.addPlot(p5, 0, h*2, w, h)\n", "bunch.addPlot(p6, w, h*2, w, h)\n", "\n", "bunch" ] }, { "cell_type": "code", "execution_count": 7, "id": "stainless-browser", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val classOrdered = asDiscrete(\"class\", order=1)\n", "\n", "val p10 = p + ggtitle(\"x='class', color='class'\") +\n", " geomBoxplot{ x = \"class\"; color = \"class\" }\n", " \n", "val p11 = p + ggtitle(\"x=asDiscrete('class', order=1), color='class'\") +\n", " geomBoxplot{ x = classOrdered; color = \"class\" } \n", " \n", "val p12 = p + ggtitle(\"x=asDiscrete('class', order=1),\\ncolor=asDiscrete('class')\") +\n", " geomBoxplot{ x = classOrdered; color = asDiscrete(\"class\") }\n", " \n", "val p13 = p + ggtitle(\"x='class', color='drv'\") +\n", " geomBoxplot{ x = \"class\"; color = \"drv\" }\n", " \n", "val p14 = p + ggtitle(\"x=asDiscrete('class', order=1),\\ncolor=asDiscrete('drv', order=1)\") +\n", " geomBoxplot{ x = classOrdered; color = asDiscrete(\"drv\", order=1) }\n", " \n", "val p15 = p + ggtitle(\"x=asDiscrete('class', orderBy='..middle..')\") +\n", " geomBoxplot{ x = asDiscrete(\"class\", orderBy=\"..middle..\"); color = \"drv\" }\n", " \n", "\n", "val bunch1 = GGBunch()\n", "bunch1.addPlot(p10, 0, 0, w, h)\n", "bunch1.addPlot(p11, 0, h, w, h)\n", "bunch1.addPlot(p12, w, h, w, h)\n", "bunch1.addPlot(p13, 0, h*2, w, h)\n", "bunch1.addPlot(p14, 0, h*3, w, h)\n", "bunch1.addPlot(p15, w, h*3, w, h)\n", "\n", "bunch1" ] }, { "cell_type": "code", "execution_count": 8, "id": "matched-cowboy", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p21 = p +\n", " geomCrossbar(stat = Stat.boxplot()) { x = \"class\"; color = \"class\"} +\n", " scaleColorBrewer(palette = \"Dark2\") +\n", " ggtitle(\"default\")\n", " \n", "val p22 = p +\n", " geomCrossbar(stat = Stat.boxplot()) { \n", " x = asDiscrete(\"class\", orderBy = \"..middle..\", order = 1); \n", " color = \"class\"} +\n", " scaleColorBrewer(palette = \"Dark2\") +\n", " ggtitle(\"order by 'middle' ↑, also applies to 'color'\") \n", "val p23 = p +\n", " geomCrossbar(stat = Stat.boxplot()) { \n", " x = asDiscrete(\"class\", orderBy = \"..middle..\"); \n", " color = asDiscrete(\"class\", order = 1)} +\n", " scaleColorBrewer(palette = \"Dark2\") +\n", " ggtitle(\"'class' and 'orderBy' + 'class' and 'order':\\noptions are combined\") \n", " \n", "val bunch2 = GGBunch()\n", "bunch2.addPlot(p21, 0, 0, w, h)\n", "bunch2.addPlot(p22, w, 0, w, h)\n", "bunch2.addPlot(p23, 0, h, w, h)\n", "bunch2" ] }, { "cell_type": "code", "execution_count": 9, "id": "conceptual-gender", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p31 = p + ggtitle(\"default\") + geomBar { x = \"class\" }\n", "val p32 = p + ggtitle(\"alphabetically\") + geomBar { x = asDiscrete(\"class\", order = 1) }\n", "val p33 = p + ggtitle(\"by 'hwy' ↓\") + geomBar { x = asDiscrete(\"class\", orderBy = \"hwy\") }\n", "val p34 = p + ggtitle(\"by 'hwy' ↑\") + geomBar { x = asDiscrete(\"class\", orderBy = \"hwy\", order = 1) }\n", "\n", "val bunch3 = GGBunch()\n", "bunch3.addPlot(p31, 0, 0, w, h)\n", "bunch3.addPlot(p32, w, 0, w, h)\n", "bunch3.addPlot(p33, 0, h, w, h)\n", "bunch3.addPlot(p34, w, h, w, h)\n", "bunch3" ] }, { "cell_type": "code", "execution_count": 10, "id": "graduate-porter", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p41 = p + ggtitle(\"default\") + geomBar { x = \"manufacturer\"; fill = \"class\" }\n", "val p42 = p + ggtitle(\"'x' alphabetically\") + geomBar { x = asDiscrete(\"manufacturer\", order = 1); fill = \"class\" }\n", "val p43 = p + ggtitle(\"'x' alphabetically + samplingPick(4)\") + \n", " geomBar(sampling = samplingPick(4)) { \n", " x = asDiscrete(\"manufacturer\", order = 1); \n", " fill = \"class\" \n", " }\n", "val p44 = p + ggtitle(\"'fill' alphabetically\") + geomBar { x = \"manufacturer\"; fill = asDiscrete(\"class\", order=1) }\n", "val p45 = p + ggtitle(\"'x' & 'fill' alphabetically\") + \n", " geomBar { x = asDiscrete(\"manufacturer\", order=1); fill = asDiscrete(\"class\", order=1) }\n", "val p46 = p + ggtitle(\"'x' by count ('sum' aggregation)\") + \n", " geomBar { x = asDiscrete(\"manufacturer\", orderBy=\"..count..\"); fill = \"class\" }\n", "val p47 = p + ggtitle(\"'x' by count + samplingPick(4)\") + \n", " geomBar(sampling = samplingPick(4)) { \n", " x = asDiscrete(\"manufacturer\", orderBy=\"..count..\"); \n", " fill = \"class\" \n", " }\n", "\n", "val bunch4 = GGBunch()\n", "bunch4.addPlot(p41, 0, 0, w, h)\n", "bunch4.addPlot(p42, 0, h, w, h)\n", "bunch4.addPlot(p43, w, h, w, h)\n", "bunch4.addPlot(p44, 0, h*2, w, h)\n", "bunch4.addPlot(p45, w, h*2, w, h)\n", "bunch4.addPlot(p46, 0, h*3, w, h)\n", "bunch4.addPlot(p47, w, h*3, w, h)\n", "bunch4" ] }, { "cell_type": "code", "execution_count": 11, "id": "french-agent", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val pp = letsPlot(mpg.toMap()) { x = \"fl\"; fill = \"drv\" }\n", "val p51 = pp + geomBar() + ggtitle(\"Default\")\n", "val p52 = pp + geomBar(position=positionDodge()) + ggtitle(\"With Dodge Position\")\n", "\n", "val pOrder1 = ggplot(mpg.toMap()) { x = asDiscrete(\"fl\", order=1); fill = asDiscrete(\"drv\", order=1) }\n", "val title1 = ggtitle(\"'x' and 'fill' alphabetically\")\n", "\n", "val p53 = pOrder1 + geomBar() + title1\n", "val p54 = pOrder1 + geomBar(position=positionDodge()) + title1\n", "\n", "val pOrder2 = ggplot(mpg.toMap()) { x = asDiscrete(\"fl\", orderBy=\"..count..\"); fill = \"drv\" }\n", "val title2 = ggtitle(\"'x' by 'count'\")\n", "\n", "val p55 = pOrder2 + geomBar() + title2\n", "val p56 = pOrder2 + geomBar(position=positionDodge()) + title2\n", "\n", "\n", "val bunch5 = GGBunch()\n", "bunch5.addPlot(p51, 0, 0, w, h)\n", "bunch5.addPlot(p52, w, 0, w, h)\n", "bunch5.addPlot(p53, 0, h, w, h)\n", "bunch5.addPlot(p54, w, h, w, h)\n", "bunch5.addPlot(p55, 0, h*2, w, h)\n", "bunch5.addPlot(p56, w, h*2, w, h)\n", "bunch5" ] }, { "cell_type": "code", "execution_count": 12, "id": "fabulous-northwest", "metadata": {}, "outputs": [ { "data": { "text/html": [ "carat | cut | color | clarity | depth | table | price | x | y | z | |
---|---|---|---|---|---|---|---|---|---|---|
1 | 0.23 | Ideal | E | SI2 | 61.5 | 55.0 | 326 | 3.95 | 3.98 | 2.43 |
2 | 0.21 | Premium | E | SI1 | 59.8 | 61.0 | 326 | 3.89 | 3.84 | 2.31 |
3 | 0.23 | Good | E | VS1 | 56.9 | 65.0 | 327 | 4.05 | 4.07 | 2.31 |
Shape: 3 x 11. \n", "
" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// diamonds\n", "\n", "val diamonds = DataFrame.readCSV(\"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/diamonds.csv\")\n", "diamonds.head(3)" ] }, { "cell_type": "code", "execution_count": 13, "id": "charming-match", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val pd = letsPlot(diamonds.toMap())\n", "\n", "val p61 = pd + geomBar { x = \"cut\"; fill = \"clarity\" } + ggtitle(\"Default\")\n", "\n", "val p62 = pd + geomBar(position=positionFill()) { x = \"cut\"; fill = \"clarity\" } + ggtitle(\"position='fill'\")\n", "val p63 = pd + ggtitle(\"'x' and 'fill' alphabetically\") +\n", " geomBar(position=positionFill()) { x = asDiscrete(\"cut\", order=1); \n", " fill = asDiscrete(\"clarity\", order=1) }\n", "val p64 = pd + ggtitle(\"'x' order by 'count'\") +\n", " geomBar(position=positionFill()) { x = asDiscrete(\"cut\", orderBy=\"..count..\"); \n", " fill = asDiscrete(\"clarity\", order=1) }\n", "\n", "val p65 = pd + geomBar(position=positionDodge()) { x = \"cut\"; fill = \"clarity\" } + ggtitle(\"position='dodge'\")\n", "val p66 = pd + ggtitle(\"'x' order by 'count' and 'fill' - alphabetically\") +\n", " geomBar(position=positionDodge()) { x = asDiscrete(\"cut\", orderBy=\"..count..\"); \n", " fill = asDiscrete(\"clarity\", order=1) }\n", "\n", "\n", "val bunch6 = GGBunch()\n", "bunch6.addPlot(p61, 0, 0, w, h)\n", "bunch6.addPlot(p62, w, 0, w, h)\n", "bunch6.addPlot(p63, 0, h, w, h)\n", "bunch6.addPlot(p64, w, h, w, h)\n", "bunch6.addPlot(p65, 0, h*2, w, h)\n", "bunch6.addPlot(p66, w, h*2, w, h)\n", "bunch6" ] } ], "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": 5 }