{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e9cff123", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "f27ed1dd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.0.0.0-SNAPSHOT. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "markdown", "id": "22bed5b6", "metadata": {}, "source": [ "#### Set `themeGrey()` as default theme. It improves plots readability." ] }, { "cell_type": "code", "execution_count": 3, "id": "19e17aad", "metadata": {}, "outputs": [], "source": [ "LetsPlot.theme = themeGrey()" ] }, { "cell_type": "markdown", "id": "8493d965", "metadata": {}, "source": [ "#### Data" ] }, { "cell_type": "code", "execution_count": 4, "id": "58d3ca1e", "metadata": {}, "outputs": [], "source": [ "val labelsData = mapOf(\n", " \"x\" to listOf(0, 1, 2, 3, 4, 5, 6, 7, 8),\n", " \"y\" to listOf(0, 45, 90, 135, 180, 225, 270, 315, 360),\n", " \"r_y\" to listOf(360, 315, 270, 225, 180, 135, 90, 45, 0),\n", " \"l\" to listOf(\"l0\", \"l45\", \"l90\", \"l135\", \"l180\", \"l225\", \"l270\", \"l315\", \"l360\"),\n", " \"g\" to listOf(\"g1\", \"g1\", \"g1\", \"g2\", \"g2\", \"g2\", \"g3\", \"g3\", \"g3\")\n", ")\n", "\n", "val lollipopData = mapOf(\n", " \"c\" to listOf(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"),\n", " \"x\" to listOf(1, 2, 3, 4, 5, 6),\n", " \"y\" to listOf(1, 2, 3, 4, 5, 6)\n", ")\n", "\n", "val studentData = mapOf(\n", " \"subj\" to listOf(\"progr\", \"math\", \"physic\", \"chemistry\", \"biology\"),\n", " \"subjId\" to listOf(1, 2, 3, 4, 5),\n", " \"student\" to List(5) { \"John\" },\n", " \"score\" to listOf(19, 15, 18, 12, 9)\n", ")" ] }, { "cell_type": "markdown", "id": "d66230d8", "metadata": {}, "source": [ "# Geoms" ] }, { "cell_type": "markdown", "id": "1701efea", "metadata": {}, "source": [ "## `geomArea()`\n", "Line get transformed into a circle:" ] }, { "cell_type": "code", "execution_count": 5, "id": "0e96441f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() + geomArea() { x = listOf(0, 1); y = listOf(1, 1) }\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar()\n", "))" ] }, { "cell_type": "markdown", "id": "e71e86c1", "metadata": {}, "source": [ "### `flat = true`\n", "The plot can be transformed into a radar plot by using `flat = true` and a discrete x-scale." ] }, { "cell_type": "code", "execution_count": 6, "id": "310d15d7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(studentData) +\n", " geomArea(flat = true) { x = \"subjId\"; y = \"score\" } +\n", " geomPoint() { x = \"subjId\"; y = \"score\" }\n", "\n", "val labels = mapOf(1 to \"progr\", 2 to \"math\", 3 to \"physic\", 4 to \"chemistry\", 5 to \"biology\")\n", "\n", "val continuous = scaleXContinuous(labels = labels)\n", "val discrete = scaleXDiscrete(labels = labels)\n", "\n", "gggrid(listOf(\n", " p + continuous,\n", " p + continuous + coordPolar() + ggtitle(\"scaleXContinuous\"),\n", " p + discrete + coordPolar() + ggtitle(\"scaleXDiscrete\"),\n", "))" ] }, { "cell_type": "markdown", "id": "302a6755", "metadata": {}, "source": [ "## `geomSegment()`" ] }, { "cell_type": "code", "execution_count": 7, "id": "2631a836", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() +\n", " geomSegment(x = 0, y = 0, xend = 4, yend = 4, arrow = arrow(), size = 1) +\n", " geomSegment(x = 8, y = 0, xend = 4, yend = 4, arrow = arrow(), size = 1)\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar()\n", "))" ] }, { "cell_type": "markdown", "id": "905a37de", "metadata": {}, "source": [ "`sizeEnd`/`strokeEnd` precision length adjustment parameters:" ] }, { "cell_type": "code", "execution_count": 8, "id": "adcba1b0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// known problem - zero-length segment because of second datapoint.\n", "// this is a temp workaround to sync stroke/stroke_end and size/size_ens domains\n", "val d = mapOf( \n", " \"x\" to listOf(0, 1),\n", " \"y\" to listOf(0, 0),\n", " \"size\" to listOf(8, 10), \n", " \"stroke\" to listOf(1, 2),\n", " \"size_end\" to listOf(10, 0), \n", " \"stroke_end\" to listOf(2, 0)\n", ")\n", "\n", "val p = letsPlot(d) { x = \"x\"; y = \"y\" } +\n", " geomPoint(shape = 21, alpha = 0.5, color = \"red\", showLegend = false) { size = \"size\"; stroke = \"stroke\" } +\n", " geomSegment(xend = 1, yend = 0, size = 2,\n", " arrow = arrow(ends = \"both\", type = \"open\", length = 22, angle = 30)) {\n", " sizeStart = \"size\" \n", " strokeStart = \"stroke\";\n", " sizeEnd = \"size_end\"\n", " strokeEnd = \"stroke_end\"\n", " } +\n", " scaleSizeIdentity()\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar(xlim = -0.35 to 1.35, ylim = -2 to 2) // lims are only to make the figure smiling\n", "))" ] }, { "cell_type": "markdown", "id": "b6cdd2cb", "metadata": {}, "source": [ "## `geomLabel()`\n", "Regular scatter plot." ] }, { "cell_type": "code", "execution_count": 9, "id": "3a5077c9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(labelsData) { x = \"x\"; y = \"y\"; label = \"l\" } + geomLabel()\n", "\n", "gggrid(listOf(\n", " p, \n", " p + coordPolar() + ggtitle(\"coordPolar()\"),\n", " p + coordPolar(theta = \"y\") + ggtitle(\"theta=y\"),\n", "))" ] }, { "cell_type": "markdown", "id": "7bd3a25e", "metadata": {}, "source": [ "## `geomPath()`\n", "The transform resamples path data by converting straight segments into curves. The `flat` parameter controls this behaviour." ] }, { "cell_type": "code", "execution_count": 10, "id": "7b3acdd4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(labelsData) { x = \"x\"; y = \"y\"; color = \"y\" } + scaleColorBrewer(palette = \"GnBu\")\n", "\n", "gggrid(listOf(\n", " p + geomPath(size = 3) + coordPolar() + ggtitle(\"coordPolar()\"),\n", " p + geomPath(size = 3, flat = true) + coordPolar(theta = \"x\") + ggtitle(\"coordPolar(), flat=true\")\n", "), ncol=2)" ] }, { "cell_type": "markdown", "id": "b92bad8b", "metadata": {}, "source": [ "### Autoclose on a discrete x-scale" ] }, { "cell_type": "code", "execution_count": 11, "id": "82e42a31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(studentData) + geomPath(flat = true) { x = \"subj\"; y = \"score\" } + coordPolar(ylim = 0 to 20)" ] }, { "cell_type": "markdown", "id": "a5a9f07e", "metadata": {}, "source": [ "## `geomLollipop()`\n", "See the `Params` section for details on using the `ylim` parameters." ] }, { "cell_type": "code", "execution_count": 12, "id": "97bbaa08", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(lollipopData) { x = \"c\"; y = \"y\" } + geomLollipop()\n", "\n", "gggrid(listOf(\n", " p, \n", " p + coordPolar()\n", "))" ] }, { "cell_type": "markdown", "id": "151574dc", "metadata": {}, "source": [ "## `geomBar()` \n", "This works similarly to rects, but with the addition of tooltips." ] }, { "cell_type": "markdown", "id": "d7e89e70", "metadata": {}, "source": [ "### `position='stack'`" ] }, { "cell_type": "code", "execution_count": 13, "id": "37245cb7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val barData = mapOf(\"foo\" to listOf(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3))\n", "val p = letsPlot(barData) + geomBar(size = 0) { fill = asDiscrete(\"foo\", order = 1) }\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar(theta=\"y\") + ggtitle(\"position=stack, coord_polar(theta=y)\"),\n", " p + coordPolar(theta=\"x\") + ggtitle(\"position=stack, coord_polar(theta=x)\"),\n", "))" ] }, { "cell_type": "markdown", "id": "56d2d740", "metadata": {}, "source": [ "### `position='dodge'`" ] }, { "cell_type": "code", "execution_count": 14, "id": "79aeaf6f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(barData) + geomBar(size = 0, position = positionDodge()) { fill = asDiscrete(\"foo\", order = 1) }\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar(theta=\"y\") + ggtitle(\"position=dodge, coord_polar(theta=y)\"),\n", " p + coordPolar(theta=\"x\") + ggtitle(\"position=dodge, coord_polar(theta=x)\"),\n", "))" ] }, { "cell_type": "markdown", "id": "d076e78b", "metadata": {}, "source": [ "### `stat='identity'`\n", "On a continuous x-scale the first and last bars stuck. The `expand` parameter can be used to fix this." ] }, { "cell_type": "code", "execution_count": 15, "id": "1fce12df", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val data = mapOf(\n", " \"x\" to listOf(1, 2, 3),\n", " \"y\" to listOf(5, 3, 4)\n", ")\n", "\n", "val barId = letsPlot(data) + geomBar(stat = Stat.identity, width = 0.8) { x = \"x\"; y = \"y\" } + coordPolar()\n", "\n", "gggrid(listOf(\n", " barId + ggtitle(\"Continuous x\"),\n", " barId + scaleXContinuous(expand = listOf(0, 0.1)) + ggtitle(\"scaleXContinuous(expand=[0, 0.1))\")\n", "))" ] }, { "cell_type": "code", "execution_count": 16, "id": "1372b27c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "barId + scaleXDiscrete() + ggtitle(\"Discrete x\")" ] }, { "cell_type": "markdown", "id": "a7084648", "metadata": {}, "source": [ "## `geomHLine()`/`geomVLine()`" ] }, { "cell_type": "code", "execution_count": 17, "id": "4506efab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() +\n", " geomHLine(yintercept = 5, color = \"red\") +\n", " geomHLine(yintercept = 10, color = \"green\") +\n", " geomHLine(yintercept = 15, color = \"blue\") +\n", " geomHLine(yintercept = 20, color = \"orange\") +\n", " geomVLine(xintercept = 10, color = \"pink\") +\n", " geomVLine(xintercept = 20, color = \"magenta\") +\n", " geomVLine(xintercept = 30, color = \"dark_green\") +\n", " xlim(0 to 30) +\n", " ylim(0 to 20)\n", " \n", "gggrid(listOf(p, p + coordPolar()))" ] }, { "cell_type": "markdown", "id": "dc107ee4", "metadata": {}, "source": [ "## `geomTile()`" ] }, { "cell_type": "code", "execution_count": 18, "id": "4a7349b8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fun meshgridPoints(x:List, y:List):List> {\n", " val xSer = y.flatMap { x }\n", " var yInd = -1\n", " val ySer = y.flatMap { \n", " yInd++\n", " List(x.size) {y[yInd]} \n", " }\n", " return xSer.zip(ySer)\n", "}\n", "\n", "val gridPoints = meshgridPoints(\n", " generateSequence(0.0, {it + 6.0/50} ).takeWhile { it <= 1.0 }.toList(),\n", " generateSequence(0.0, {it + 6.0/40} ).takeWhile { it <= 1.0 }.toList())\n", "fun f(x:Double, y:Double): Double {\n", " return sin(x).pow(10) + cos(10 + y * x) * cos(x)\n", "}\n", "\n", "val X = gridPoints.map { it.first }\n", "val Y = gridPoints.map { it.second }\n", "val Z = gridPoints.map { f(it.first, it.second) }\n", "\n", "val p = letsPlot {x=X; y=Y; fill=Z} + geomTile() + scaleFillHue()\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar(ylim = -0.5 to null) // ylim to make a hole\n", "\n", "))" ] }, { "cell_type": "markdown", "id": "3a56f044", "metadata": {}, "source": [ "## `geomRect()`" ] }, { "cell_type": "markdown", "id": "da9e662a", "metadata": {}, "source": [ "### Stacked bars\n", "are transformed into a pie chart" ] }, { "cell_type": "code", "execution_count": 19, "id": "0debeeb6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "val c1 = \"#66c2a5\"\n", "val c2 = \"#fc8d62\"\n", "val c3 = \"#8da0cb\"\n", "val p = ggplot() + \n", " geomRect(xmin=0, xmax=5, ymin=0, ymax=7, fill=c1, size=0) +\n", " geomRect(xmin=0, xmax=5, ymin=7, ymax=11, fill=c2, size=0) +\n", " geomRect(xmin=0, xmax=5, ymin=11, ymax=14, fill=c3, size=0) \n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar() + ggtitle(\"coordPolar()\"),\n", " p + coordPolar(theta = \"y\") + ggtitle(\"coordPolar(theta=y)\"),\n", ")).show()\n", "\n", "gggrid(listOf(\n", " p + coordPolar(theta = \"y\", direction = -1) + ggtitle(\"coordPolar(theta=y, dir=-1)\"),\n", " p + coordPolar(theta = \"y\", direction = -1, start = 3.14/2) + \n", " ggtitle(\"coordPolar(theta=y, dir=-1, start=PI/2)\"),\n", ")).show()" ] }, { "cell_type": "markdown", "id": "c3ddc05c", "metadata": {}, "source": [ "### Dodged bars" ] }, { "cell_type": "code", "execution_count": 20, "id": "4c7e6d24", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() +\n", " geomRect(xmin=0, xmax=1, ymin=0, ymax=7, fill=c1, size=0) +\n", " geomRect(xmin=1, xmax=2, ymin=0, ymax=4, fill=c2, size=0) +\n", " geomRect(xmin=2, xmax=3, ymin=0, ymax=3, fill=c3, size=0)\n", "\n", "gggrid(listOf(\n", " p, \n", " p + coordPolar(theta = \"y\") + ggtitle(\"coordPolar(theta=y)\"),\n", " p + coordPolar(theta = \"x\") + ggtitle(\"coordPolar(theta=x)\"),\n", "))" ] }, { "cell_type": "markdown", "id": "1cdc326c", "metadata": {}, "source": [ "### Horizontal bars" ] }, { "cell_type": "code", "execution_count": 21, "id": "20755414", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot() +\n", " geomRect(ymin=0, ymax=1, xmin=0, xmax=7, fill=c1, size=0) +\n", " geomRect(ymin=1, ymax=2, xmin=0, xmax=4, fill=c2, size=0) +\n", " geomRect(ymin=2, ymax=3, xmin=0, xmax=3, fill=c3, size=0)\n", "\n", "gggrid(listOf(\n", " p, \n", " p + coordPolar(theta = \"y\") + ggtitle(\"coordPolar(theta=y)\"),\n", " p + coordPolar(theta = \"x\") + ggtitle(\"coordPolar(theta=x)\"),\n", "))" ] }, { "cell_type": "markdown", "id": "7377309b", "metadata": {}, "source": [ "# Params" ] }, { "cell_type": "code", "execution_count": 22, "id": "a72b8335", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(labelsData) { x = \"x\"; y = \"y\"; color = \"y\" } + \n", " geomPath(size = 3, showLegend = false) + \n", " scaleColorBrewer(palette = \"GnBu\")\n", "\n", "\n", "p + coordPolar() + ggtitle(\"Default plot with coordPolar()\")" ] }, { "cell_type": "markdown", "id": "666e916a", "metadata": {}, "source": [ "### `transformBkgr` \n", "\n", "When using the `transformBkgr` parameter, the panel is not transformed into a circle, but remains a rectangle. This behaviour is similar to `ggplot2`." ] }, { "cell_type": "code", "execution_count": 23, "id": "2d5572bd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + coordPolar(transformBkgr = false) + ggtitle(\"coordPolar(transformBkgr=false)\")" ] }, { "cell_type": "markdown", "id": "6443e8f5", "metadata": {}, "source": [ "### `direction`" ] }, { "cell_type": "code", "execution_count": 24, "id": "66cbe28c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + coordPolar(direction = -1) + ggtitle(\"coordPolar(direction=-1)\")" ] }, { "cell_type": "markdown", "id": "7dff898f", "metadata": {}, "source": [ "### `start`" ] }, { "cell_type": "code", "execution_count": 25, "id": "f5619d05", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " p + coordPolar(start = 3.14 / 2) + ggtitle(\"start=PI/2\"),\n", " p + coordPolar(start = -3.14 / 2) + ggtitle(\"start=-PI/2\"),\n", "))" ] }, { "cell_type": "markdown", "id": "e3b202e6", "metadata": {}, "source": [ "### `direction` + `start`" ] }, { "cell_type": "code", "execution_count": 26, "id": "c21555ea", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " p + coordPolar(start = 3.14 / 2, direction=-1) + ggtitle(\"dir=-1, start=PI/2\"),\n", " p + coordPolar(start = -3.14 / 2, direction=-1) + ggtitle(\"dir=-1, start=-PI/2\"),\n", "))\n" ] }, { "cell_type": "markdown", "id": "03db5bf4", "metadata": {}, "source": [ "### `xlim` and `ylim`\n", "The `xlim` parameter can be used to prevent overlap between the first and last values. \n", "The `ylim` parameter can be used to shift data away from the centre or the outer circle.\n", "\n", "To prevent overlap between `6` and `1`, we adjust the `xlim` to `[null, 7]` while keeping the default minimum limit as it is not relevant. \n", "In addition, we change `ylim` to `[null, 6.5]` to prevent the lollipop's top from overlapping with the outer circle." ] }, { "cell_type": "code", "execution_count": 27, "id": "3f4cf47a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(lollipopData) { x = \"x\"; y = \"y\" } + geomLollipop()\n", "\n", "gggrid(listOf(\n", " p + coordPolar(),\n", " p + coordPolar(xlim = null to 7, ylim = null to 6.5)\n", "))" ] }, { "cell_type": "markdown", "id": "501d6bb5", "metadata": {}, "source": [ "# Scales \n", "Interaction between scales and polar coordinate system." ] }, { "cell_type": "code", "execution_count": 28, "id": "4ac54b1f", "metadata": {}, "outputs": [], "source": [ "val pie = letsPlot() +\n", " geomRect(xmin=0, xmax=1, ymin=0, ymax=7, fill=\"red\", size=0) +\n", " geomRect(xmin=1, xmax=2, ymin=0, ymax=4, fill=\"blue\", size=0) +\n", " geomRect(xmin=2, xmax=3, ymin=0, ymax=3, fill=\"green\", size=0) +\n", " coordPolar()\n", "\n", "val sticksData = mapOf(\n", " 'x' to listOf(0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5),\n", " 'y' to listOf(0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6),\n", " 'g' to listOf(5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0)\n", ")\n", "\n", "val sticks = letsPlot(sticksData) + geomPath{ x='x'; y='y'; group='g'; size='g' } + coordPolar()" ] }, { "cell_type": "markdown", "id": "82bc6009", "metadata": {}, "source": [ "## Limit\n", "The `limits` parameter sets the lower and upper limits individually, but expects absolute values. \n", "`x=[null, 6]` limit to make the first and last elements not overlap. \n", "`y=[-2, 7]` limit to make a medium sized centre hole and a small outer buffer (with a length of 1)." ] }, { "cell_type": "code", "execution_count": 29, "id": "c3759240", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " sticks + ggtitle(\"No limits\"),\n", " sticks + lims(x = null to 6, y = -2 to 7) + ggtitle(\"lims(x=[null, 6], y=[-2, 7])\"),\n", "))" ] }, { "cell_type": "markdown", "id": "750b185e", "metadata": {}, "source": [ "## Discrete x-scale\n", "\n", "If the x-scale is discrete, the coordinate system will automatically adjust domain so that the first and last values don't overlap." ] }, { "cell_type": "code", "execution_count": 30, "id": "f1899ecf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " sticks + ggtitle(\"Continuous\"), \n", " sticks + scaleXDiscrete() + ggtitle(\"scaleXDiscrete()\")\n", "))" ] }, { "cell_type": "markdown", "id": "38f9d4dc", "metadata": {}, "source": [ "## clip-path\n", "\n", "Segments should not get rendered outside the panel boundaries." ] }, { "cell_type": "code", "execution_count": 31, "id": "cbc4db9e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " sticks,\n", " sticks + coordPolar(ylim = 0 to 1.5)\n", "))" ] }, { "cell_type": "markdown", "id": "6c436bf7", "metadata": {}, "source": [ "## Expand\n", "By default `coordPolar()` resets the expansion to zero, but it can still be set explicitly. \n", "Horizontal non-zero expand will produce a gap between first and last sectors, so the plot will never become a circle. \n", "Vertical non-zero expand creates a central hole (expand for the bottom of the domain) and a buffer between the plot and the axis (expand for the top of the domain). \n", "\n", "`expand` is symmetric, so it can't be used to adjust only the bottom or only the top." ] }, { "cell_type": "code", "execution_count": 32, "id": "a6c0d738", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " sticks + ggtitle(\"No expand\"),\n", " sticks + scaleXContinuous(expand = listOf(0, 2.5)) + \n", " scaleYContinuous(expand = listOf(0, 2)) + \n", " ggtitle(\"scale_XY_continuous(expand=...)\")\n", "))" ] }, { "cell_type": "markdown", "id": "3dc9bef7", "metadata": {}, "source": [ "## `scaleYLog10()` \n", "Log-scale works fine." ] }, { "cell_type": "code", "execution_count": 33, "id": "8996ddb9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val d = mapOf(\n", " 'x' to listOf(1, 2, 3, 4, 5, 6, 7, 8),\n", " 'y' to listOf(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000),\n", ")\n", "val p = letsPlot(d) + geomPath(flat = true) { x='x'; y='y'}\n", "p\n", "\n", "gggrid(listOf(\n", " p,\n", " p + coordPolar(),\n", " p + scaleYLog10(),\n", " p + scaleYLog10(format=\".1~e\") + coordPolar(),\n", "), ncol = 2)" ] }, { "cell_type": "markdown", "id": "57de96bf", "metadata": {}, "source": [ "# Theme" ] }, { "cell_type": "code", "execution_count": 34, "id": "28b82be5", "metadata": {}, "outputs": [], "source": [ "val p = letsPlot(labelsData) { x = \"x\"; y = \"y\"; color = \"y\" } + \n", " scaleColorBrewer(palette = \"GnBu\") +\n", " geomPath(size = 3)\n", "\n", "val polar_p = p + coordPolar()" ] }, { "cell_type": "code", "execution_count": 35, "id": "abca1ba2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(listOf(\n", " p,\n", " polar_p + themeMinimal2() + ggtitle(\"minimal2\"),\n", " polar_p + themeBW() + ggtitle(\"bw\"),\n", " polar_p + themeClassic() + ggtitle(\"classic\"),\n", " polar_p + themeGrey() + ggtitle(\"grey\"),\n", " polar_p + themeLight() + ggtitle(\"light\"),\n", " polar_p + themeMinimal() + ggtitle(\"minimal\"),\n", " polar_p + themeNone() + ggtitle(\"none\"),\n", " polar_p + themeVoid() + ggtitle(\"void\"),\n", "), ncol = 3)" ] }, { "cell_type": "markdown", "id": "2638d700", "metadata": {}, "source": [ "## Axis configuration" ] }, { "cell_type": "code", "execution_count": 36, "id": "6621bf5e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p_tmp = p + theme(\n", " axisLineY = elementLine(color=\"red\", size=2),\n", " axisLineX = elementLine(color=\"blue\", size=2),\n", " axisTicksLengthY = 5,\n", " axisTicksLengthX = 10,\n", " axisTicksY = elementLine(size=5, color=\"red\"), \n", " axisTicksX = elementLine(size=3, color=\"blue\"),\n", " axisTextX = elementText(color=\"blue\"),\n", " axisTextY = elementText(color=\"red\"),\n", ")\n", "\n", "gggrid(listOf(\n", " p_tmp,\n", " p_tmp + coordPolar()\n", "))" ] }, { "cell_type": "markdown", "id": "e92a5688", "metadata": {}, "source": [ "## panelInset\n", "The `panelInset` parameter can be used to create space between the axis and the panel content:" ] }, { "cell_type": "code", "execution_count": 37, "id": "33536bf1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p_themed = p_tmp + theme(\n", " plotBackground = elementRect(fill=\"pink\"),\n", " panelBackground = elementRect(fill=\"grey\"),\n", " panelBorder = elementRect(color=\"green\", size=1),\n", " panelInset = 10\n", ")\n", "\n", "gggrid(listOf(\n", " p_themed,\n", " p_themed + coordPolar()\n", "))" ] } ], "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.0-dev-3517" } }, "nbformat": 4, "nbformat_minor": 5 }