{ "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" }, { "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, "id": "selected-covering", "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, "id": "emotional-stage", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 12

\n", " \n", " \n", " " ] }, "execution_count": 3, "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": 4, "id": "central-heart", "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(mpg.toMap()) { y = \"hwy\" } + theme(axisTitleX=\"blank\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "original-textbook", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "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": 6, "id": "stainless-browser", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "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": 7, "id": "matched-cowboy", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "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": 8, "id": "conceptual-gender", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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": 9, "id": "graduate-porter", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 10, "id": "french-agent", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "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": 11, "id": "fabulous-northwest", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":11,\"columns\":[\"untitled\",\"carat\",\"cut\",\"color\",\"clarity\",\"depth\",\"table\",\"price\",\"x\",\"y\",\"z\"],\"kotlin_dataframe\":[{\"untitled\":1,\"carat\":0.23,\"cut\":\"Ideal\",\"color\":\"E\",\"clarity\":\"SI2\",\"depth\":61.5,\"table\":55.0,\"price\":326,\"x\":3.95,\"y\":3.98,\"z\":2.43},{\"untitled\":2,\"carat\":0.21,\"cut\":\"Premium\",\"color\":\"E\",\"clarity\":\"SI1\",\"depth\":59.8,\"table\":61.0,\"price\":326,\"x\":3.89,\"y\":3.84,\"z\":2.31},{\"untitled\":3,\"carat\":0.23,\"cut\":\"Good\",\"color\":\"E\",\"clarity\":\"VS1\",\"depth\":56.9,\"table\":65.0,\"price\":327,\"x\":4.05,\"y\":4.07,\"z\":2.31}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 11

\n", " \n", " \n", " " ] }, "execution_count": 11, "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": 12, "id": "charming-match", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "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.8.20" } }, "nbformat": 4, "nbformat_minor": 5 }