{ "cells": [ { "cell_type": "markdown", "id": "proud-butler", "metadata": {}, "source": [ "### The layer `orientation` property\n", "\n", "Some geoms treat each axis differently and, thus, can thus have two orientations.\n", "\n", "The `orientation` parameter specifies the axis that the layer' stat and geom should run along (x-axis by default). " ] }, { "cell_type": "code", "execution_count": 1, "id": "humanitarian-german", "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": "registered-chance", "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": "thousand-filter", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "id": "guilty-indian", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
manufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.819994auto(l5)f1829pcompact
2audia41.819994manual(m5)f2129pcompact
3audia42.020084manual(m6)f2031pcompact

Shape: 3 x 12. \n", "

" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var 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": "ordinary-friend", "metadata": {}, "outputs": [], "source": [ "val base = letsPlot(mpg.toMap()) + ggsize(800, 300)\n", "val manufacturerMapping = asDiscrete(\"manufacturer\", orderBy = \"..count..\")\n", "val hideXLabel = theme(axisTitleX = \"blank\")\n", "val hideYLabel = theme(axisTitleY = \"blank\")" ] }, { "cell_type": "markdown", "id": "pursuant-stamp", "metadata": {}, "source": [ "### geomBar()" ] }, { "cell_type": "code", "execution_count": 6, "id": "cleared-brooklyn", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// orientation \"x\" (default) : \"manufacturer\" is mapped to the x-axis.\n", "\n", "val orientationX = base + geomBar(color = \"white\") {\n", " x = manufacturerMapping\n", " fill = \"class\"\n", "}\n", "orientationX + hideXLabel" ] }, { "cell_type": "code", "execution_count": 7, "id": "experimental-storm", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Same but flipped coordinate system.\n", "\n", "orientationX + coordFlip() + hideXLabel" ] }, { "cell_type": "code", "execution_count": 8, "id": "referenced-cliff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Orientation \"y\".\n", "// The \"manufacturer\" is mapped to the y-axis.\n", "\n", "val baseY = base + hideYLabel\n", "baseY + geomBar(color = \"white\", orientation=\"y\") {\n", " y = manufacturerMapping\n", " fill = \"class\"\n", "}" ] }, { "cell_type": "markdown", "id": "prime-looking", "metadata": {}, "source": [ "### geomBoxplot()" ] }, { "cell_type": "code", "execution_count": 9, "id": "combined-kingston", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "baseY + geomBoxplot(width = 0.7, orientation = \"y\") {\n", " y = \"manufacturer\"\n", " x = \"cty\" \n", "}" ] }, { "cell_type": "markdown", "id": "interpreted-mileage", "metadata": {}, "source": [ "### `geomViolin()`" ] }, { "cell_type": "code", "execution_count": 10, "id": "elementary-dutch", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg.toMap()) + geomViolin(trim = false, orientation = \"y\") { y=\"drv\"; x = \"cty\"; fill = \"drv\"} +\n", " geomBoxplot(fill = \"white\", alpha = 0.5, width = 0.3, orientation = \"y\") { \n", " y = \"drv\"\n", " x = \"cty\" \n", " }\n", " " ] }, { "cell_type": "markdown", "id": "blond-gallery", "metadata": {}, "source": [ "## Continuous variables" ] }, { "cell_type": "code", "execution_count": 11, "id": "homeless-madagascar", "metadata": {}, "outputs": [], "source": [ "@file:DependsOn(\"org.apache.commons:commons-math3:3.6.1\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "virtual-minimum", "metadata": {}, "outputs": [], "source": [ "import org.apache.commons.math3.distribution.MultivariateNormalDistribution" ] }, { "cell_type": "code", "execution_count": 13, "id": "secret-vaccine", "metadata": {}, "outputs": [], "source": [ "val cov0 : Array = arrayOf(doubleArrayOf(1.0, -.8),\n", " doubleArrayOf(-.8, 1.0))\n", "\n", "val cov1 : Array = arrayOf(doubleArrayOf(10.0, .1),\n", " doubleArrayOf(.1, .1))\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "basic-teaching", "metadata": {}, "outputs": [], "source": [ "val n = 200\n", "\n", "val means0 : DoubleArray = doubleArrayOf(-2.0, 0.0)\n", "val means1 : DoubleArray = doubleArrayOf(0.0, 1.0)\n", "\n", "val xy0 = MultivariateNormalDistribution(means0, cov0).sample(n)\n", "val xy1 = MultivariateNormalDistribution(means1, cov1).sample(n)\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "hearing-prerequisite", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val dat = mapOf(\n", " \"x\" to (xy0.map { it[0] } + xy1.map { it[0] }).toList(),\n", " \"y\" to (xy0.map { it[1] } + xy1.map { it[1] }).toList(),\n", " \"c\" to List(n){\"A\"} + List(n){\"B\"},\n", ")\n", "\n", "val p = letsPlot(dat) {x = \"x\"; y = \"y\"; color = \"c\"} + ggsize(600,300) +\n", " geomPoint()\n", "p" ] }, { "cell_type": "markdown", "id": "photographic-monster", "metadata": {}, "source": [ "### `geomDensity()`" ] }, { "cell_type": "code", "execution_count": 16, "id": "numeric-turtle", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomDensity(size = 2)" ] }, { "cell_type": "code", "execution_count": 17, "id": "opened-glance", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomDensity(size = 2, orientation = \"y\")" ] }, { "cell_type": "markdown", "id": "banner-mustang", "metadata": {}, "source": [ "### `geomHistogram()`" ] }, { "cell_type": "code", "execution_count": 18, "id": "jewish-fluid", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomHistogram(fill = \"rgba(0,0,0,0)\")" ] }, { "cell_type": "code", "execution_count": 19, "id": "neural-saint", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomHistogram(fill = \"rgba(0,0,0,0)\", orientation = \"y\")" ] }, { "cell_type": "markdown", "id": "controversial-james", "metadata": {}, "source": [ "### `geomFreqpoly()`" ] }, { "cell_type": "code", "execution_count": 20, "id": "fifteen-latin", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomFreqpoly(size = 2)" ] }, { "cell_type": "code", "execution_count": 21, "id": "brief-tolerance", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomFreqpoly(size = 2, orientation = \"y\")" ] }, { "cell_type": "markdown", "id": "answering-friendly", "metadata": {}, "source": [ "### `geomSmooth()`" ] }, { "cell_type": "code", "execution_count": 22, "id": "unique-consciousness", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomSmooth()" ] }, { "cell_type": "code", "execution_count": 23, "id": "individual-selling", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomSmooth(orientation = \"y\")" ] }, { "cell_type": "markdown", "id": "alternate-violence", "metadata": {}, "source": [ "### `geomBoxplot()`" ] }, { "cell_type": "code", "execution_count": 24, "id": "certain-olive", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomBoxplot(x = -10, color = \"black\") +\n", " geomBoxplot(y = -3, color = \"black\", orientation = \"y\")" ] }, { "cell_type": "markdown", "id": "european-forty", "metadata": {}, "source": [ "### `geomViolin()`" ] }, { "cell_type": "code", "execution_count": 25, "id": "expanded-protection", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomViolin(x = -10, color = \"black\", alpha = 0) +\n", " geomViolin(y = -3, color = \"black\", alpha = 0, orientation = \"y\")" ] }, { "cell_type": "code", "execution_count": 26, "id": "split-rwanda", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomViolin(mapping = {fill = \"c\"}, y = -2, color = \"black\", \n", " alpha = 0.3, position = positionIdentity, width = 3, showLegend = false,\n", " orientation=\"y\", trim = false)" ] } ], "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 }